By adding the velocity for both of the differences in the X and Y axis, we get the relative velocity for 2 objects moving on a 2d plane.
Mass and the everyday C2 Object.
The Physics behavior has a way to specify the mass of an object or it can do a psudo calculation based on the total area of the object’s collision polygon along with the density you select. This allows you to give some objects more momentum without making them take up more space on screen. If you were to throw a sponge at your TV as hard as you can, it’ll probably just bounce off harmlessly while if you were to throw a brick of the same height, width and depth as the sponge as hard as you can at your TV, you’d probably have to buy a new TV. This is because the brick is more dense than the sponge.
Thanks to all of this, you can use the Physics.Mass expression to get the mass of your object once you’ve set it up correctly. Armed with this and your relative velocity, you can now begin to figure out how much damage one object does to another when they slam together.
Putting it all together
Ok, so we have Velocity and we have Mass…how do we use this information to figure out how much chaos ensues when two object in our game collide?
Going back to our formula: p=mv we can see that we simply need to multiply the relative velocity with the mass of the object to figure out how much momentum it struck the other object with.
It’s up to you to figure out how damage scales in your game, but an example of putting this into practice would be:
I’ll break down each line in the event.
Event: Rock -> On collision with Rock
This event is fired when our two rocks collide. It will pick the two instances of our rock sprite that have hit each other. Each of these instances can be accessed with the System -> pick nth instance event later.
Action: Rock -> spawn particles on layer 0 (image point 0)
This is just to give some visual feedback that the two rocks have collided with each other.
Local variables: DeltaX and DeltaY
Since we have to pick each instance separately we need a way to know the difference (or delta) of their velocities. We will use these variables to store this information.
Local variables: Rock0Mass and Rock1Mass
As above, these variables will store the mass of each rock so that we can apply the momentum formula later.
Event: System -> Pick Rock instance 0
This event selects the first rock sprite in our collision event.
#Action: System -> Add Rock.Physics.VelocityX to DeltaX
Action: System -> Add Rock.Physics.VelocityY to DeltaY
Action: System -> set Rock0Mass to Rock.Physics.Mass#
Here we take our first rock and set its mass variable. We add the X and Y velocity to our Delta variables.
Event: System -> Pick Rock instance 1
This event selects the second rock sprite in our collision event.
#Action: System -> Subtract Rock.Physics.VelocityX from DeltaX
Action: System -> Subtract Rock.Physics.VelocityY from DeltaY
Action: System -> set Rock1Mass to Rock.Physics.Mass#
Here we’re setting the mass of the second rock and subtracting the velocity from our Delta Variable (remember, we want the difference in their velocities to account for moving in different/same directions)
Finally, we apply the damage.
Event: System -> Pick Rock instance 0
Select our first rock again
Action: Rock -> Subtract (abs(DeltaX) + abs(Delta)Y)
[*] Rock1Mass)/20 from Health
Here we’re putting into practice the formula we described before, and finally we’re taking the momentum that Rock 2 has and dividing it by 20. We remove that result from the rock’s health.
(Why divide by 20? In this case, through trial and error and observing how much damage the rocks in the demo did running into each other, I settled on give out 1/20 points of damage per unit of momentum. This number will vary based on your own implementation of your health system.)
Event: System -> pick Rock instance 1
Here we select the second rock so we can apply the momentum of the first rock to it.
Action: Rock -> Subtract (abs(DeltaX) + abs(Delta)Y)
[*] Rock0Mass)/20 from Health
And here we calculate and subtract the damage from the second rock.
Show me the money!
To see this demo in action, click here.
To get the capx (R120.2) simply click here.