Set a Physics Body's Moving Speed
-
Introduction
After you have added a physics body to an object, you can make that physics body move by setting a speed, and that object will move in the same way as its physics body.
You can use the following block to set the speed of an object’s physics body:
The first 3 inputs are the speed value in the X, Y and Z directions. They can be positive or negative numbers.
The last input is optional. If you leave it blank, the current sprite object’s physics body will move; if you specify the name of any object created in the current sprite, then that object’s physics body will move.
Set Speed for Object vs Its Physics Body
Before we enable physics, you already know how to set an object’s speed using the “set speed” block:
There are a few key differences when we set the speed of an object’s physics body:
-
The “set speed” block above will no longer work when it carries a physics body. That is because the object’s movement is tied to its physics body, so if its physics body is not moving, that object will not move.
-
When we set the speed of a physics body, that body will move at that speed initially, then that speed might change over time due to other factors, such as gravity or friction.
Example 1 - Move a Box
In this example, a large plane is linked with a box-shaped physics body. It will serve as the ground that will not move. The second object is a box, and it is also equipped with a physics body.
Now, this scene is ready for testing. Here is what happens when you try to set the box’s speed in each direction.X direction:
Y direction: Note that if the speed value is negative, the object will move in the opposite direction.
Z direction:
Note that for the X and Y directions, the box will keep moving at the given speed. However, for the Z direction, since the box is also being pulled down by gravity, so it will rise at a speed of 50 initially, then its rising speed will slow down, and then it will start to fall back to the ground.
X and Y directions:You can set the speed in more than one direction. For example, you can set the box to move at speed of 100 in the X direction and 100 in the Y direction. The box will move diagonally as a result, and its overall speed will be around 141.
Example 2 - Move a Physics Body with Friction
In the previous example, the ground and the box both have friction of 0, which means they are both extremely smooth, as if they are both made of ice.
In this example, we will set the ground at friction of 100, and try different values for the box’s friction:
Box friction = 0:If the box’s friction is 0, then the box will not slow down at all. In other words, for 2 physics bodies to be affected by friction, both of them need to have friction values greater than 0.
Box friction = 50:The box will glide a bit and then stop when its physics friction is 50.
Box friction = 100:The box will glide just a little bit and then stop when its physics friction is 100.
Box friction = 1000:The maximum value of friction is 100 for normal physics objects. When we use a friction value of 1000, we are simulating an object that is extremely rough (made of some alien material). In this case, the box can’t even glide forward, but it will tilt up a bit before falling back. If we set a faster speed, it will start to roll forward in the air.
Move a Ball
When we set the speed of a ball’s physics body, we get a slightly different result. In this example, we use a ball instead of a box, and try 2 different friction values for the ball’s physics body: 0 vs 100.
Ball friction = 0:When the ball’s friction value is 0, it will keep moving at the same speed, which is the same result as the box.
Ball friction = 100:When the ball’s friction value is 100, it will start to roll forward right away, and also slow down at the same time. After a while, it will stabilize at a new speed that is slower.
Damping
As shown in the previous example, when we move a ball, even if its friction is very high, it will still end up moving forever. This is not desirable for most games, since in reality all objects should slow down and stop.
To make an object gradually slow down, you can use the following block to set its “damping” factor.
The first input is the damping factor for moving speed. It is a value between 0 and 100. The speed of the object will be slowed down by this percentage value in each update (there are about 60 updates each second).
The second input is the damping factor for rotation speed. We’ll discuss it on the wiki page on a physics body’s rotation speed.The last input is the name of the object to set the damping speeds for, if that object is not the selected sprite object.
For example, with a 0 friction, if the damping value is 50, then the ball will quickly come to a stop. If damping is 100, then it will not move at all, since its speed will drop by 100% immediately.
Speed Towards a Target Point
Very often, we need to set a physics body’s speed towards a target point from its current position. To save the trouble of calculating the speed in each of the X/Y/Z directions, you can use the following block, which allows you to set the speed towards that target directly:
- The first input is the overall speed the physics body will be moving at.
- The next 3 inputs are the X/Y/Z positions of the target point.
- The last input is still the optional name of the object you want to move.
For example, suppose the ball starts at the position of (x=0, y=0, z=50). If we set its target point to (x=100, y=0, z=50), it will move to the right. After it pa*ses the target point, it will continue moving to the right. If we set its speed to the same target point again, it will start to move to the left instead. In other words, the moving direction of the ball depends not only on the target position, but also on the current position of the ball. -