Physics: The Rest of the Manual

2

Stats

3,331 visits, 4,727 views

Tools

Translations

This tutorial hasn't been translated.

License

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.

Published on 23 Aug, 2014. Last updated 19 Feb, 2019

Welcome!

This tutorial is for anyone who really wants to understand how physics works in Construct 2. A lot of this information should be in the manual that comes with Construct 2 and takes a fair bit of time and patience to dig up. This knowledge was necessary for my current game project and if you play with physics enough and really want control over what is happening you will need this information as well.

Before You Begin

You need to have read the construct 2 manual and you need to be able to put that knowledge to use. I will also assume you have basic knowledge of math - though, if you are like me and have never taken a physics class or only a few math classes in high school that is okay. Basic working knowledge is enough. The application of this knowledge is the hard part (;

Also, while some portions of what I tell you may be true in other game engines using box2d, I am specifically covering how box2d works within Construct 2.

Physics and Construct 2

:

The order of the events

The following thing happens every fame in a game with physics. The physics runs its update logic (kind of like an event sheet, but just for physics). Afterwards, your event sheet is permitted to run. Any forces or impulses you apply to physics objects will not have any effect on the object until the next physics update. In other words: your manipulations of an object won't take effect unit the frame after you call them.

This has some interesting implications for how you need to structure your events. Consider the following events:

Normally, one would reason that this event would cause an impulse to be applied to an object, then that objects velocity would be checked to make sure the impulse didn't make it go faster than 100 pixels per second. However, the impulse will not be applied until the next frame during the physics update. The velocity check, on the other hand, will occur this frame. Meaning regardless of how fast the object will be moving, due to the impulse, it will never have its velocity checked until space is pressed again.

The following events provide a further demonstration.

In the frame that "space" is pressed, SomeObject will have an impulse applied and then have its velocity set to 0. One would expect to never see the object move, but that is not the case. Instead, the frame after "space" is pressed, during the physics update, the effects of the impulse become active.

Setting a physics velocity directly changes the objects property right away. That property can be changed and then tested all in the same frame:

The events above add to the velocity of an object if space is pressed, so long as that objects speed is under 100. If adding to the velocity causes the objects velocity to exceed 100 then it is capped at 100.

Applied Forces and impulses will always become active on the next frame. If you need to test the effects of those changes then you must wait until the next frame, bearing the consequences of exceeding bounds for one frame. This could be okay for some games, but not okay for others. The result of ignoring this problem and trying to cap a velocity can result in a body moving always one impulse or force faster than the Maximum velocity that you are testing against.

How do I use Force and impulse then?

If you are going to use forces and impulses with a physics game and you want precise control then you need to know what is happening when you use them. Get out your math cap because this part is going to use it.

All the following equations can be used to predict the result of a force, impulse, etc. It can be used in reverse to determine how much of a force to add to get an object to position X in t time.

The result of using an impulse on an object can be calculated as follows:

Velocity = Impulse/Mass (2500)

If you apply an impulse of 1 to an object with a mass of 1 then its velocity will be 2500 pixels per second. 2500 seems to be the arbitrary constant that Construct 2 uses to determine pixel distances

The result of force is calculated as follows:

Velocity = Force/Mass (2500 )(time)

During a physics update, if the time stepping mode is set to frame rate dependent, then time = 1/60 regardless of framerate. Velocities are also calculated using this constant. If Time Step mode is set to framerate independent, then time = dt.

When using timeframe independent physics, you do not need to apply dt to the forces you add. It is applied internally. Just remember though, dt varries from frame to frame. meaning that the force you apply this frame will have the dt of the next frame applied to it. In order to counter this effect, events must be arranged in a particular order. Observe below:

The dt timer must reflect the same dt being used by physics during physics update. If you don't do this, fluctuations in physics as a result of dt will be higher (we are talking fractions of fractions here, but that could be important). For example, if you were to subtract dt from timer after applying the force then the following would happen internally:

During the frame that space was pressed the timer would be set to 1. Then a force would be added the the object (to be calculated next frame). then, the current frame's dt would be subtracted from 1. The following frame would add the force but that force could be scaled by a different dt than was subtracted from the timer during the previous frame. This results in a difference in total dt by the difference of the first and last frame of dt applied to the timer and to the physics object. This can be no difference in the best of situations, but in the worst situation it could be a difference of 1/60 - 1/30, which can play havoc in very tight physics systems.

Torque

Torque affects the angular velocity of an object.

AngularVelocity = Torque/Mass (648.78892733564) (time)

If you want to create a torque impulse then don't scale by time. Applying 1 torque to an object of 1 mass over the span of 1 second results in an angular velocity of 648.78892733564. The Construct 2 constant here seems a bit weird, and I want to look further into it. But for now that's all the math you need for torque.

Damping

Linear damping affects the velocity of an object by the following amount:

Change in velocity = -Current Velocity (Linear Damping Coefficient) (time)

Linear damping is calculated after forces and impulses have been calculated. You can use this math to figure out how much damping to add to an object to get a desired effect. You can also use it to predict the outcome of damping on a force applied to an object.

Angular damping is resolved in the same way.

Friction

This is more complicated than damping as normal forces have to be calculated. As far as I know, currently there is no way within construct 2 to obtain surface normals of a colliding object. This is available in box2d and can be obtained in several ways. Demand Ashley for raycasting and linecasting via box2d in the physics behavior and you can calculate friction.

While you are demanding more physics features be sure to ask for kinematic bodies (bodies than can be moved but forces do not act on them - great for moving platforms) and prismatic joints (think sliding doors, rising platforms, anything that moves on one axis)

(btw - I and others have already asked for these things, and Ashley has given good reasons why we don't have them in construct and why we may not ever have them).

Other Math

The following is some handy maths that you may need; They are easy to figure out but I included them none the less.

Construct doesn't expose volume but you can figure it out as follows:

Volume = Density/Mass

Volume is handy for figuring out what to set density of an object to to get a particular mass... Construct2 doesn't allow mass to be set directly (a bit weird since that is how it is done in box2d, I think)

Density = Desired Mass / Volume

How do you know how fast to spin a wheel to travel n distance?

AngularVelocity = Velocity / Diameter (pi)

Part 2: Applications

-Predicting a target's future movement and position.

-Calculating torque needed to rotate to angle A

-Calculating the force needed to slowly stop an object from moving /breaking force

All these are possible using the math provided above and should be straight forward to solve. I will possibly add some steps later: Good Luck!

Any Questions? PM me or add a comment below!

  • 0 Comments

Want to leave a comment? Login or Register an account!