
Time for action – rotate it!
Your cubes are at the right location and have the right size. Now you want to turn them to face in a specific direction. In an actual game, for example, you may want to turn characters to face an interesting part of the scene. Again, you can either use absolute or relative rotation.
For every rotation, you have to specify three float values—x
, y
, and z
, one for each axis, around which you want to rotate. In contrast to translation and scale, rotations are not expressed in world units, but in radians. Don't worry—to convert between the familiar 360 degrees to radians, simply multiply the degree value by the built-in FastMath.DEG_TO_RAD
constant, or FastMath.RAD_TO_DEG
to convert it back. You can find this constant in the com.jme3.math.FastMath
package.
As an example, let's rotate the two cubes by 45 degrees around the x and y axes, respectively:
- Convert 45 degrees to radians:
float r = FastMath.DEG_TO_RAD * 45f;
- To rotate
geom2
around the x axis, add the following call at the end of thesimpleInitApp()
method:geom2.rotate(r, 0.0f, 0.0f);
- To rotate
geom
around the y axis, add the following call below:geom.rotate(0.0f, r, 0.0f);
- Clean and build the
BasicGame
template, and right-click on it to run the file.
What just happened?
The yellow cube should be pitched forward around the x axis, facing you with a horizontal edge. The blue cube should have turned around its y axis, facing you with one of its vertical edges. Using the x, y, and z axes for the three directions should look familiar to you by now.

Use the method rotate()
for relative rotation around the three axes.
Absolute rotation in 3D space, however, is a bit more sophisticated. Absolute rotations use a special data type called quaternion.