• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

collisions

pgvago

Member
Happy New Year from Milano!

As always, I promise that I did some search before, but I was unable to find what I was looking for.

I don't undertsand the logic behind collisions.
If I have 2 objects colliding, does GameMaker understand which one is colliding into the other?
Does GameMaker assume that the moving object is the one that is colliding, while the one that is stationary is the object that is being collided into?

I am unsure where to put my collision code. I was under the assumption that I could put it on any of the 2 objects but I get different results so I am starting to believe that GameMaker is able to understand which is the colliding object.

Of course, I could be compltely wrong.

Thanks, Pietro.
 

Rob

Member
If you understand how to get the id of an instance of an object and you know what other is used for then it will probably clear most things up.

If you have:
Code:
id_of_instance_im_colliding_width = collision_point(x, y, obj_enemy, false, true);
Then you can say:
Code:
//Create event
id_of_instance_im_colliding_width = noone


//Step event
id_of_instance_im_colliding_width = collision_point(x, y, obj_enemy, false, true);

if (id_of_instance_im_colliding_width != noone)
id_of_instance_im_colliding_width.health -= 10
This only works because collision_point returns an instance id or noone. Lots of other collision functions also do this but some do not (which is why using the manual to check what a function does, what it returns and understanding it will help you advance further.

[Also note that I used "obj_enemy" in the collision function, so it only checks for a collision with instances of that object type. You could make a parent object and use that for collisions with multiple different objects, this also means that those child objects would need to work with any subsequent code you try to use (eg if you're removing health from an instance but that particular object didnt get a "health" variable, you'll get an error)]

If you're using the "Collision Event" then the keyword other will hold the id of the instance that's being collided with of the particular object that the "Collision Event" is set for

If you want an answer that's more specific to your code then show your code and explain what's going on.
 
Top