Jump to content


Photo

Checking For Collisions


  • Please log in to reply
10 replies to this topic

#1 Sahaqiel

Sahaqiel

    GMC Member

  • New Member
  • 211 posts

Posted 27 December 2008 - 09:56 PM

I tried collision with rectangle, but the GM help manual doesn't say what to put in all the things, and I don't even think that would really work from the results I got.

How do I simply check collision between two objects in GML?
Sahaqiel
  • 0

#2 Khelder

Khelder

    GMC Member

  • New Member
  • 2018 posts

Posted 27 December 2008 - 10:16 PM

place_meeting()
  • 0

#3 Sahaqiel

Sahaqiel

    GMC Member

  • New Member
  • 211 posts

Posted 27 December 2008 - 10:48 PM

Please explain.
place_meeting() then what? Where do I put the names of the objects?
Sahaqiel
  • 0

#4 Davve

Davve

    Procrastinator

  • GMC Member
  • 3665 posts
  • Version:GM8.1

Posted 27 December 2008 - 10:51 PM

Please explain.
place_meeting() then what? Where do I put the names of the objects?
Sahaqiel

{
if place_meeting(x,y,OBJECTNAME)
-ACTION-
}

Edited by davve941018, 27 December 2008 - 10:51 PM.

  • 0

#5 Sahaqiel

Sahaqiel

    GMC Member

  • New Member
  • 211 posts

Posted 27 December 2008 - 10:59 PM

If I use that, it'll only be the collision between two points, though.
Like if I had place_meeting(thing.x,thing.y,otherThing) it would only allow for checking collision with the top left corner pixel, right?
  • 0

#6 Davve

Davve

    Procrastinator

  • GMC Member
  • 3665 posts
  • Version:GM8.1

Posted 27 December 2008 - 11:03 PM

Nope, I think thing.x and thing.y means the WHOLE object. :)

Edited by davve941018, 27 December 2008 - 11:03 PM.

  • 0

#7 Nexer

Nexer

    GMC Member

  • New Member
  • 18 posts

Posted 27 December 2008 - 11:34 PM

Nope, I think thing.x and thing.y means the WHOLE object. :)


I think I know what he needs

if you havn't centered your sprite you can use this code. Anyway, if you does have centered your sprite, you can use it too. I'll show an example further down.

uncentered:
collision_rectangle(x,y,x+sprite_width,y+sprite_height,<obj_name>,true,false)
With this you only have to change the object name, then it should work. but don't blame me!

Centered is a bit different:
collision_rectangle(x-sprite_width/2,y-sprite_height/2,x+sprite_width/2,y+sprite_height/2,<obj_name>,true,false)
This should work for a centered sprite (centering comes handy when you need you sprite to rotate and is done by clicking "Center" in "Sprite properties")

sprite_width and sprite_height is simply variables holding the width and the height of the sprite that is bound to the calling object.


You might wonder what the "true" and "false" are for:

collision_rectangle(x1,y1,x2,y2,obj,prec,notme)

prec is shorting for precise, meaning if it is a "pixelwise" collision checking.
to make it work, make sure you have checked the "Precise collision checking" in the sprite properties of the corresponding sprite.
If precis replaced by "true" (as I've have done) it should "wrap" the object in the rectangle so it checks if there's a collision anywhere on the sprite's edges.

the notme is shorting for well not me. It simply ask if the calling object should be checked by the function. (the calling object is the object which execute the function, in this case "collision_rectangle()")
So if notme is replaced by true (I've set it to false, just to mention) the calling object can be part of that collision too, which is EXACTLY what we want as the object who calls collision_rectangle should be checked if it's colliding with the specified object.

okay? haha.. I know it was a lot.
Hope it helps you and other who my be doupted ;)

Edited by Nexer, 27 December 2008 - 11:47 PM.

  • 0

#8 Sahaqiel

Sahaqiel

    GMC Member

  • New Member
  • 211 posts

Posted 28 December 2008 - 09:30 AM

Do I put an if in front of that to test? 'Cause I did and it's not working.
See, I'm trying to get my character to push a block. I do this by having a collision event with the character object with the block setting alarm[0] to 20. In alarm[0], if the character object is still colliding with it, I want it to move according to what global.facing is.
  • 0

#9 T. Westendorp

T. Westendorp

    GMC Member

  • New Member
  • 1270 posts

Posted 28 December 2008 - 11:20 AM

place_meeting(x,y,object)
Checks if the object that executes the check would be colliding with object with index <object> if its x and y would be equal to <x> and <y>.

THATS how place_meeting (and place_free and place_empty) work.




T
  • 0

#10 hotshotscott

hotshotscott

    GMC Member

  • New Member
  • 3483 posts

Posted 28 December 2008 - 11:56 AM

Do I put an if in front of that to test? 'Cause I did and it's not working.
See, I'm trying to get my character to push a block. I do this by having a collision event with the character object with the block setting alarm[0] to 20. In alarm[0], if the character object is still colliding with it, I want it to move according to what global.facing is.

Well for all you and I know, the colliding script might be working. If you're setting an alarm when you collide, if he's still colliding them the alarm will keep being reset. You'll need another variable which decides when to set that alarm, to make sure it isn't continously being reset.

To see if the collision is working, put;
if //collision code
{
show_message("It collided")
game_end()
}

THen you'll know whether it's colliding or not..

Scotty
  • 0

#11 Khelder

Khelder

    GMC Member

  • New Member
  • 2018 posts

Posted 28 December 2008 - 04:07 PM

If I use that, it'll only be the collision between two points, though.
Like if I had place_meeting(thing.x,thing.y,otherThing) it would only allow for checking collision with the top left corner pixel, right?

place_meeting(x,y,object)
Checks if the object that executes the check would be colliding with object with index <object> if its x and y would be equal to <x> and <y>.

THATS how place_meeting (and place_free and place_empty) work.




T

Westendorp is correct. There is also position_meeting(x,y,obj), which checks if obj overlaps (x,y), and can be used for more precise collision checks.

However, a quick glance at the manual, putting "place_meeting" into the index tab, would have told you all that.

if place_meeting(x,y,obj) {
	show_message("There is a collision between this object, and obj!")
	game_end()
}

Edited by Khelder, 28 December 2008 - 04:08 PM.

  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users