Movement and Rotation Speed for 2D Physics
-
Introduction
After a sprite has been added to the physics world, the physics engine will take care of its movement and rotation. To make a sprite move or turn, you should apply some force to it, and let the physics engine calculate how that sprite should react to this force.
However, you can also set the sprite’s movement and rotation speed if you want to. This is often more convenient if you want to set an object’s speed exactly. It’s like telling the physics engine “I know you are handling this sprite for me, but I really need this sprite to move at exactly 30 units per second right now, so please make it happen right now!”.
Setting X and Y Speeds
You can use the following blocks to set the sprite’s speed in the X and Y directions directly:
The x speed is how many units the sprite will move to the right each second. If it is smaller than 0, then the object will move to the left. For example, the stage’s width is 480, so if a sprite’s x speed is 100, it will take 4.8 seconds for it to move from the left edge of the stage to the right edge.The y speed is how fast the sprite moves up. And if it is smaller than 0, the sprite will move down.
Note that you can only set a sprite’s speed if it is configured to be a “dynamic” or “movable” object. In other words, a “fixed” object will ignore these 2 blocks.
In addition, setting an object’s X speed will not affect its Y speed. This is useful for platformer games: you can set the X speed to make a character move left or right, but use the physics engine to simulate the effects of gravity on its Y speed.
Speed May Change After You Set It
For a “dynamic” object, its speed may change after you set it due to other forces or collision.
For example, we can set the circle to a y speed of 200 to make it go up, but it will be pulled down to a negative y speed due to gravity after a while.
To keep an object moving at the speed you specify, you should repeatedly set that speed, or use a “movable” object type instead.
Speed in A Direction
Sometimes you just want an object to move in a specific direction. Instead of setting X and Y speeds separately, you can specify a speed and a direction using the following block:
The speed value is still how far the sprite will travel each second. The direction value is between 0 and 360, and it has the same meaning as the “point in direction” block. For example, if the direction is 90 degrees, the sprite will move to the right.
Rotation Speed
Besides setting movement speed, you can also make the object rotate around its own center using the following block:
The input value is how many degrees the sprite will rotate per second. For example, if you set it to 180, the sprite will make a full turn in 2 seconds.If this value is positive, the sprite will rotate clockwise; if it is negative, the sprite will rotate counter-clockwise.
Point in Direction of Speed
If you need the sprite to be heading in the direction it is moving, you can use the following block:
For example, for a top-down game where you drive a car, after setting the X and Y speed of the car, you can use this block to set its facing direction. -
interesting information