3D Directions and Rotations
-
Prerequisite
Introduction
Many things around us rotate. When we describe a rotation, we need to focus on 3 questions:
1. Axis: the object is rotating around which axis?
For example, this umbrella is spinning with its shaft pointing up, which is the same as the Z-axis. Therefore, we say this umbrella is rotating around the Z-axis.
For another example, this wheel is rotating around its central hub, which is aligned with the Y-axis.
If the tire is rotating around the Z-axis, it would look like this:
2. Direction: the object is rotating clockwise or counter-clockwise?
To determine the direction of a rotation, we need to move the camera to look from the top arrow of the rotating axis.
For example, for the umbrella, since it is rotating around the Z-axis, we need to look from the top of the Z-axis, and we find the umbrella is rotating clockwise.
On the other hand, for this tire, since it is rotating around the Y-axis, we need to view the rotation from the arrow of the Y-axis, and we find the wheel is rotating counter-clockwise.
3. Speed: how fast is the object rotating?
Sometimes we want the object to keep spinning in an animation, as opposed to rotating to the target angle instantly. The easiest way to specify the rotation speed is the number of degrees to turn per second.
For example, for this airplane, it takes about 4 seconds for it to turn a full circle, which is 360 degrees. So we say it is rotating 90 degrees per second.
Rotating an Object Manually
To figure out how many degrees to turn an object, you can use the direction inputs in the sprite pane to rotate an object manually. For example, the “Direction Z” input controls the rotation angle around the Z-axis.
Note that the directions you manually specify will not be saved when you reopen the project. You need to use one of the code blocks below to set the directions as part of your program.
The Turn-Left and Turn-Right Blocks
Let’s start with these 2 motions blocks you already know. They allow us to turn a 3D object around the Z-axis, relative to its current direction.
The “Point in Direction” Block
This block allows you to turn the object to a target direction around the Z-axis. Note that it does not matter what’s the current direction of the object. If that object is already pointing in this direction, then this block will have no effect.
The “Point Towards X/Y” Block
Another motion block that can be used to rotate around the Z-axis is the “point towards x/y” block. It is most useful when you know a target point’s X position and Y position. It saves you from calculating the direction value.
Turning Around Any Axis
The motion blocks introduced above can only rotate an object around the Z-axis. To rotate around any of the 3 axes, you can use this block:
Parameters
-
Degrees to Turn: How many degrees to turn relative to the current direction. A positive number will turn the object clockwise (when viewed from the top of the axis arrow). For example, a full circle is 360 degrees, and turning to the opposite direction is 180 degrees.
-
Axis: can be X or Y or Z.
-
Animation Time Span: If the time span is 0, then the object will be rotated instantly. If the time span is not 0, then the object will rotate gradually in an animation.
-
Blocking Type: If “Blocking”, then the program will keep running this block until it completes; if “Non-Blocking”, then the program will continue to run the next block below this block right away. This is useful when you want to keep rotating an object in a long animation, but also run the code below right away.
Demo
Rotating to a Direction Around An Axis
Similar to the “point in direction” block, this new block can be used to rotate the object in any direction around any axis:
Parameters
-
Target Direction: Target angle to turn to (regardless of the current direction).
-
Axis: can be X or Y or Z.
-
Animation Time Span: If the time span is 0, then the object will be rotated instantly. If the time span is not 0, then the object will rotate gradually in an animation.
-
Blocking Type: If “Blocking”, then the program will keep running this block until it completes; if “Non-Blocking”, then the program will continue to run the next block below this block right away. This is useful when you want to rotate an object slowly through animation, but also run the code below right away.
Demo
Rotating to Target Directions Around All 3 Axes
If you need to rotate an object around all 3 axes, then it is easier to use the following block:
The first 3 inputs are the angles around the X/Y/Z axis, and the last input is the animation time length.
For example, the following 3 “rotate to angle” blocks have the same result as the one “rotate to direction” block:
Setting A Rotation Speed
If you would like an object to keep rotating at a target speed, you can use the “set speed” block, which allows you to set the rotation speed around each of the 3 axes. Note that a positive number will make the object rotate clockwise (viewed from the top of the rotation axis arrow).
To stop rotation, you just need to set the speed back to 0.
The “Last Object Added” Rule
When there are multiple objects, the rotation operation would be applied to the last object added right below it.
For example, the first “rotate to” block will be applied to the red box that’s added right before it, and the second “rotate to” block will be applied to the yellow box instead.
-
-