Physics in Construct: Forces, impulses, torque and joints

31

Stats

15,239 visits, 20,924 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 3 Oct, 2017. Last updated 14 Feb, 2019

If you haven't already, check out the Physics in Construct: The Basics tutorial. That'll cover the basics you need to know before moving on to forces, impulses and joints.

The main fun of using Construct's Physics behavior is in manipulating objects. You can accelerate, hit or constrain objects using forces, impulses and joints. Here's how they work.

What are forces, impulses and joints?

A force is "any influence that causes an object to undergo a change in speed or direction" (according to Wikipedia). Usually you apply a force to an object over a long period of time to cause it to accelerate in a direction. For example, gravity is a constant downwards force.

An impulse is like a sudden impact on an object. If a ball is hit with a bat in your game, you'd apply an impulse rather than a force, for a sudden one-off strike. The important distinction is that forces are usually applied over time, whereas an impulse is a one-off hit. Forces make objects gradually accelerate, and impulses send them speeding off right away.

Torque is a rotational force. Instead of accelerating the object towards something, torque gradually speeds up the rate of rotation.

A joint constrains the motion between two objects. In other words, it connects two objects in some way. For example, you can "hinge" two objects together. The objects are still free to rotate independently of each other - but their position is constrained, because they are joined at a point.

How to use forces, impulses, torque and joints

The Physics behavior adds some actions to the object it was added to. You can find these in the event sheet view along with the object's ordinary actions like Set Position.

Physics actions

Normally you'd apply forces and torque in a continuously true condition like is left mouse button down, impulses in triggers like on collision with an object, and create joints in on start of layout or after creating a new object.

Forces

When applying a force, you can specify the strength and direction of the force, as well as the point on the object it applies.

There are three actions to apply a force. They all essentially do the same thing, but some do a bit of math behind the scenes to make it easier to specify the force you want. Apply force can apply a force on the X and Y axes separately. Apply force at angle applies a force of a particular strength at an angle (and works out the X and Y components for you). Apply force towards position applies a force of a particular strength towards a position (and works out the angle, and then the X and Y components for you).

Here's an image to demonstrate a force on Mr. Piggy.

A force vector

The force (the blue arrow) can be specified in three ways: the X and Y components, the angle and strength (length of the arrow), or the strength and a position somewhere the arrow is pointing to. All three specify the same force, but each are convenient in different situations.

Point of application

You can also supply an image point when applying a force. This allows you to apply the force from a different position on the object. By default, it's applied from the object's origin, normally in the middle of the object. Notice the blue spot where the arrow starts in the image above is in the middle of Mr. Piggy.

Suppose we want to pull Mr. Piggy by the top of their helment instead of from the origin. In the image editor, an image point can be placed on the helmet. Then, we can provide that image point's name or number in the force-applying action. The force is then pulled from that point. See the image below: now the force is applied from a different position.

Applying a force from a point

Click here to open demo project showing the difference. When you apply a force at the edge of an object, it's more likely to rotate, and move with that point in front. Forces from the middle tend to be more "floaty", as if they're orbiting.

Impulses

The Apply Impulse actions are very similar in that you can apply an impulse with X and Y components, towards an angle, or towards a position. You can also specify the impulse point from an image point on the object. Again, applying impulses from the edge of the object are more likely to send the object off spinning.

Remember impulses are one-off hits - you probably only want to use them in triggered events like on collision with an object.

Torque

Torque is a rotational force. Instead of giving an X and Y component, you simply enter a single value. A positive torque accelerates the object in a clockwise direction, and a negative torque accelerates the object in an anticlockwise direction.

Torque affects the object's angular velocity. That's how many degrees per second it is rotating. Again, a positive value is clockwise rotation, and negative is anticlockwise.

You can apply torque towards an angle. This applies the rotation force either clockwise or anticlockwise towards the given angle - whichever's shortest. You can also apply torque towards a position. While that might sound like an odd thing to do, it simply calculates the angle to that position, then applies the torque towards that angle.

You can't apply torque from a position. Rotation affects the whole object!

Joints

There are two kinds of joints available to the Physics behavior.

Revolute joint

A revolute joint is like a hinge: an object is pinned on to another object. The objects are free to rotate, but are joined together at that point. Click here to open an example of a revolute joint. Revolute joints are created with the Create revolute joint action.

Revolute joint

You only need to create the joint once, either on start of layout or when the objects are created or need to be attached, then it applies to the objects forever. You can join the objects from an image point, so the other object doesn't have to be attached to the origin. You don't need to apply an image point for the other object - it's attached from its current position, so make sure it's in the right place before the Create revolute joint action.

Distance joint

A distance joint makes two objects act as if they are joined by a pole. They always stay exactly the same distance apart, although they can rotate about their attachment points. Click here to open an example of a distance joint. Distance joints are created with the Create distance joint action.

Distance joint

Note the connecting pole is not shown by the action itself! If you use this action on its own, there is nothing visible joining the objects, although they act as if there was a pole joining them. The above picture and the distance joint demo use a sprite to display the pole, so it's easier to see how the joint works. You might want to do something similar in your games, since it can look odd having objects joined by an invisible pole.

You can specify image points for both objects being attached. In the action's parameters, This image point refers to a point on the object the physics behavior is added to, and That image point refers to an image point on the other object. This allows you to connect two objects by their edges, for example.

You can also specify the damping ratio and spring frequency. The spring frequency makes the connecting pole act more like a large spring. If you set it to 4 Hz and one of the objects is struck, the connection between the two objects will "bounce" a little at about 4 oscillations a second. The rate the oscillations diminish is set by the damping ratio. A damping ratio of 0 means the spring will keep oscillating forever, and 1 means the spring will stop oscillating almost immediately.

Don't forget...

Newton's second law says "The relationship between an object's mass m, its acceleration a, and the applied force F is F = ma". In other words, if you apply a force of 100 to a small object with a low mass, you get a lot more acceleration (a) than if you applied the same force to a big object with a large mass. It should be obvious that big, heavy objects are harder to move and tend to move slower! Therefore, you may find yourself needing to use smaller forces for small objects, and larger forces for big objects. The same is true of impulses and torque.

Conclusion

Forces, impulses and torque allow you to throw objects around in your game, setting them accelerating, rotating and flying. Joints allow you to connect objects up in fun ways. You can even make strange contraptions! Check out the Physics vehicle example for one idea. You could try connecting a whole series of objects up by distance joints and see what happens. The possibilities are endless - and it's great fun to play with, too!

Course Completed!

Congratulations! You've finished all the tutorials in this course!

  • 1 Comments

  • Order by
Want to leave a comment? Login or Register an account!
  • I love the fact that the tutorials linked directly to a project and I could test and learn from the code. Thank you!