This tutorial hasn't been translated.
This tutorial is licensed under CC BY 4.0. Please refer to the license text if you wish to reuse, share or remix the content contained within this tutorial.
Thanks to the beauty of the physics behavior in Construct 2, we can create some spectacular effects that simulate real world physics. One of the questions I’ve seen most asked on the forums is “how can one use momentum to assign damage to in game objects?”
The equation to figure out momentum is p=mv where p is the momentum, m is the mass of the object, and v is the velocity. This equation by itself can tell you how much momentum a particular object has provided you know how fast it’s moving and its mass.
This is great if you have a stationary object being struck by a moving object, but what if both objects are moving? For that, we’re going to need to know the relative velocity of the two objects…that is to say if both objects are moving very fast but are moving almost in the same direction and happen to bump into each other they are not going to transfer as much momentum to each other as they would if they were moving directly towards each other.
To figure out the relative velocity of two objects, we need to find the difference in their velocity. This is easy if we are restricted to only one direction…Say just the x or the y axis. You would simply subtract object 1’s velocity from object 2’s velocity and then take the absolute value, or in Construct 2 terms:
Abs(Object1.Physics.VelocityX – Object2.Physics.VelocityX)
Why would we want to use the absolute value? Velocity not only indicates speed but direction as well. An object moving at a velocity of 4 on the x axis is moving to the right, while one moving at -4 would be moving left. This is great, except if you were to plug -4 into your momentum equation, you’re going to end up with a negative value for momentum. If you’re using that as a basis for damage you’ll end up healing your player or enemy whenever there is a collision where the objects are moving to the left!
The absolute value will allow us to say “I don’t care what direction it’s going, I just want to know how fast it’s moving.”
Since most games are played on both the X and Y axis, rather than just on one, things get a little more tricky. To find our relative velocity for two objects that are moving on a 2d plane, we need to expand our earlier equation to add the velocity on the Y axis, like so:
Abs(Object1.Physics.VelocityX – Object2.Physics.VelocityX) + Abs(Object1.Physics.VelocityY – Object2.Physics.VelocityY)
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.
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.
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.
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.
This is just to give some visual feedback that the two rocks have collided with each other.
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.
As above, these variables will store the mass of each rock so that we can apply the momentum formula later.
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.
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.
Select our first rock again
[*] 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.)
Here we select the second rock so we can apply the momentum of the first rock to it.
[*] Rock0Mass)/20 from Health
And here we calculate and subtract the damage from the second rock.
To see this demo in action, click here.
To get the capx (R120.2) simply click here.