Use Forces in 2D Physics
-
Introduction
After adding the physics body of a sprite, the 2D physics engine will take care of this sprite to make it move and turn according to the laws of physics. To influence the sprite’s behaviors, you can apply some force to the object, and the physics engine will calculate how that object will be affected by this force.
Add a Force
To apply a force to an object, you can use the following block:
This block takes 2 input parameters: the strength of the force, and in which direction to apply it. The direction has the same meaning as the “point in direction” block: the 0-degree direction points up, the 90-degree direction points to the right, etc.Note that the force will keep pushing the object after it has been added, so it has a long-lasting effect.
Example for Adding Force
In this program, we add a basketball, which falls down because of gravity. If we add a force of 5000 in the up direction, the ball starts to float up because this force is stronger than the gravity that pulls the ball down:
Prevent Movement/Rotation from Forces
When a force is applied to an object, it can make the object move or turn. If we do not want the movement or rotation to occur, we can give the physics engine some restrictions. For example, we can just tell the physics engine “this object can not move even if it is pushed”, then the physics engine will do its calculations assuming this object will stay where it is.
You can use the following 2 blocks to add or remove such restrictions:
If “Yes” is selected, then the restriction will be applied; if “No” is selected, the restriction will be removed.Note that these restrictions are only about how the object responds to forces. You can still set the object’s movement or rotation speed after adding these restrictions.
Add Force at a Fixed Position
The “add force” block above would apply the force at the center of the object, even if the object is moving, so it should not cause the object to rotate.
Sometimes we need to add a force as a fixed position in the world. For example, if we are simulating a fan that’s blowing air, then this air will push other objects from a particular position.
You can use the following block to specify the force and its position:
The 2 additional input parameters are the X and Y positions where the force is applied.
Example for Adding Force at Position
In this example, we place a tall stick in the world, and prevent it from movement. If we add a force that pushes the stick to the right at X = 0 and Y = 50 (above the center point), the stick will start to rotate clockwise. If we apply the same force at Y of -50 (below the center point), the stick will rotate in the opposite direction.
Remove All Forces
You can add more than one force to an object. To remove all of them, you can use the following block:
Add Impulses
An impulse is a force that’s applied in a very short time, while a force is applied continuously over time. For example, when we kick a soccer ball, our foot only touches the ball for a very short time, so it should be represented by an impulse; on the other hand, to launch a rocket, we need to keep pushing it higher and higher, so that force should be represented by a force.
Here are some more examples:
- Simulated by a force: car engine, wind, helicopter propellers;
- Simulated by an impulse: jumping up from the ground, racket hitting a tennis ball, shooting out a bullet;
To add an impulse, you can use the following 2 blocks, which are very similar to the blocks for adding forces:
Note that since an impulse is only applied in a very short time, you often need to use a much larger strength value to see its effect.In addition, we never need to “remove an impulse”, because the impulse would disappear right after the moment it is applied.