Apply Force to Physics Bodies
CreatiCode last edited by info-creaticode
In the real world, an object’s movement is the result of all the forces applied to it. For example, when you throw a basketball, 2 forces are applied to it: the force from your push, and the force of gravity.
In CreatiCode, you can also use forces to change the movement of an object’s physics body.
The “Apply Impulse” Block
This block will apply a one-time force to the physics body. For example, when you kick a ball or fire a bullet in a game, the impulse is applied to the object in a very short moment.
This block accepts 8 input parameters.
This input controls how strong the impulse is. The higher the value, the stronger the impulse applied, and so the bigger change in the object’s motion.
Every impulse is applied in a certain direction, which is represented by an arrow from the origin point of (0, 0, 0) to the point specified by these 3 inputs. For example, if we want to push the object up (opposite to gravity), then the direction should be x = 0, y = 0 and z = 1.
Relative Position of the Point of Application
This is the point at which the impulse is applied. For example, if you kick a soccer ball, then this is the position of the contact point between your foot and the soccer ball.
Note that this point’s position is relative to the position of the target object, which is usually the center of that object. If X = Y = Z = 0, that means the impulse is applied at the center of that object. If Z = 50, that means the impulse is applied at a point that’s 50 units above the position of the target object.
The name of the target object. If this is left blank, then the current sprite object will be the target.
The “Apply Force” Block
This block will keep applying some force to the physics body. For example, if you want to simulate gravity or wind, you will need to apply some force continuously to the object, no matter where the object moves to.
This block also takes 8 input parameters. The key difference from “apply impulse” is that the force will be spread out over 60 frames in each second. For example, if we are applying a force of 120, then it has the same effect as applying an impulse with a strength of 2 for 60 times per second.
Also, when you apply an impulse, it completes immediately. However, when you apply a force, it will keep taking effect forever. You can not cancel a force, but you can apply another force in the opposite direction to offset an existing force.
Demo 1 - Apply Impulse to Make the Box Flip
In this program, we place a 6-colored box on a big plane:
If we apply an impulse right below the box, the box will rise up and then fall back to the same position. If the impulse is applied at the right edge of the box, then the box will flip.
Demo 2 - Apply Force to Keep the Box On the Slope
In this program, we place a 6-colored box on a pink slope, and both are rotated by 45 degrees. The friction of both of them is 0, so the box will start to slide down right away.
However, if we apply a force in the up-slope direction, then we can make the box stay on the slope. The strength of this force is also less than the gravity of 100, because the slope is carrying part of the weight of the box. It turns out the strength of the force only needs to be about 70.