How can I properly detect my cars collisions using hitboxes?

I have created two car objects and their current speed is represented as the len variable.

This is the code:

Under Blue Car Collision Event:

if (place_meeting(x,y,obj_CarRed)) { // Checking Collision
len = 0 //Current speed
if (len < obj_carRed.len) { // Blue Car's current speed is greater than Red's
myHP -= obj_carRed.len/2
}
}


So I copied the code to the other object and replaced Red with Blue. When the Red car crashes into the Blue car, the Blue car takes damage because the Red car is traveling faster than the Blue car. But when the Blue car crashes into the Red car, the Red car won't take any damage.

Later, I thought about using a Hitbox and have it as a parent for both cars, but I don't know if it would mess up the collisions, and I don't know how to tell the hitbox that it's been created by Blue Car, or by Red Car. I don't know if I should make a new hitbox for each car or one for both.

The Game Maker engine executes every object event in order, and if more objects have the same event the overall execution order is due to the object's depth.

The reason Red Car is never damaged is likely because Blue Car executes its collision code first, and it sets its own velocity to 0 in line 2:

len = 0 //Current speed


So, when it's up to Red Car executing the collision script, when computing the amount of damage:

if (len < obj_carBlue.len) { // My current speed is greater than Blue
myHP -= obj_carBlue.len/2
}


there will be always no damage for Red, because obj_carBlue.len is already set to 0 before this code is executed.

Proposals
To solve this problem, you must set len to 0 after computing amount of damage for both cars. A better option is create another object in charge of computing damage for each car and setting their speeds to 0 after updating their hp values; most game engines rely upon a collision response separated from the detection, and acting stricly after the detection has happened.

A simpler fix
On the other hand, if you want to stick with the Collision Event alone you can change your code as follows:

obj_CarBlue's Collision Event with obj_CarRed:

if (len < other.len) { // My current speed is greater than Red
myHP -= other.len/2
} else {
other.myHP -= len/2
}
len = 0
other.len = 0


First, we get rid of place_meeting() because we are inside the Collision Event and the collision is certaintly happening. Then, we use the special keyword other to make a reference to the very Red Car we are colliding with instead of a generic one: if we reference obj_RedCar.something we are trying to read the variable something of any instance among all Red Cars. Inside the Collision Event the keyword other tells us what is the particular instance the collision occured with.

The code above deals with the collision at once for both cars, you can copy/paste it for Blue and Red cars and it will still work because after the first execution both cars' velocities will be set to 0, so that the code is duplicated but its effects aren't.

• Thank you so much! I had thought about setting "len = 0" as the last thing executed, but my problem was leaving it in the if event. So I was on the right track and again thank you for clarifying it to me. – Rubbish Ambush Apr 10 '18 at 17:12