Checking For Collisions
#1
Posted 27 December 2008 - 09:56 PM
How do I simply check collision between two objects in GML?
Sahaqiel
#2
Posted 27 December 2008 - 10:16 PM
#3
Posted 27 December 2008 - 10:48 PM
place_meeting() then what? Where do I put the names of the objects?
Sahaqiel
#4
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.
#5
Posted 27 December 2008 - 10:59 PM
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?
#6
Posted 27 December 2008 - 11:03 PM
Edited by davve941018, 27 December 2008 - 11:03 PM.
#7
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.
#8
Posted 28 December 2008 - 09:30 AM
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.
#9
Posted 28 December 2008 - 11:20 AM
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
#10
Posted 28 December 2008 - 11:56 AM
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.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.
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
#11
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?
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.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
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 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users











