In the previous section, you learned how you could rotate an object via keyboard commands. What if you wanted to just have an object rotate on its own? To do this, you just have to slightly modify the code you used in the last section. Here is the code for a program that contains a single cube that rotates on its own:
;demo06-03.bb - Rotating a cube on its own ; –––––––– Graphics3D 640,480 SetBuffer BackBuffer() ;Key constants Const ESC_KEY = 1 ; Create camera camera=CreateCamera() ; Create a light light=CreateLight() ;Create cube cube = CreateCube() PositionEntity cube,0,0,5 ; This following code rotates our cube: While Not KeyDown(ESC_KEY) pitch# = pitch# - 1 ;Rotate RotateEntity cube,pitch#,yaw#,roll# RenderWorld Flip Wend End
Basically, the program we just created is a simple cube with one element of code that makes it rotate:
While Not KeyDown(ESC_KEY) pitch# = pitch# - 3 ;Rotate RotateEntity cube,pitch#,yaw#,roll#
The While Not KeyDown(ESC_KEY), as usual, says that as long as the Esc key isn’t pressed, do the following. The code pitch#=pitch#-1 controls the speed, direction, and angle of rotation for your cube. The value of the pitch will always change because we entered this code within the game loop. This way, it will continue to rotate on its own. Because we didn’t define a starting speed, the pitch will start at 0. After the first loop of the game, the pitch will be –1, after the second loop, it will be –2, and so on. To slow down the rotation, you would enter an integer between 0 and 1, and to speed it up, enter a number greater than 1. For example, if you used pitch#=pitch#=+0.2, the rotation would be extremely slow, while using pitch#=pitch#=+5 would create an extremely fast rotation. If the number you enter is negative, the rotation will be toward you, and a positive number means the rotation will be away from you.
Note: Don’t Forget the + and -
You have to enter a + or - sign before the speed of rotation number or the code won’t work. For example, pitch#=pitch#1 wouldn’t work because there is no plus or minus sign in front of the 1. It would have to be either pitch#=pitch#-1 or pitch#=pitch#+1 because we are decreasing or increasing it by one unit.
In this example, we rotated the cube along the x axis. If we wanted to rotate the cube on the y or z axis, we would have entered yaw#=yaw#-1 or roll#=roll#-1 instead of the pitch#=pitch#-1.
Note: Rotate Around Multiple Axes
You are not restricted to rotating around one axis at a time. You can have your objects rotate around the x, y, and z axes individually or with any combination of the three. For example, if I wanted the cube in the last section to rotate around all three axes, I would have entered the following code in the run section:
While Not KeyDown( 1 ) pitch#=pitch#-1 yaw#=yaw#=+1 roll#=roll#-1 RotateEntity cube,pitch#,yaw#,roll#
Note: Now You Try
Experiment with different rotation speeds, angles, and directions until you’re comfortable with it. It’s always a good idea to apply a texture to your object before you start rotating it so that you can really see the effects of the rotation.
18.225.209.250