官术网_书友最值得收藏!

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:

  1. Convert 45 degrees to radians:
    float r = FastMath.DEG_TO_RAD * 45f;
  2. To rotate geom2 around the x axis, add the following call at the end of the simpleInitApp() method:
    geom2.rotate(r, 0.0f, 0.0f);
    
  3. To rotate geom around the y axis, add the following call below:
    geom.rotate(0.0f, r, 0.0f);
    
  4. 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.

主站蜘蛛池模板: 新野县| 乐昌市| 深水埗区| 周宁县| 漳州市| 余干县| 金堂县| 施甸县| 三穗县| 永平县| 万宁市| 吉林省| 明星| 易门县| 北安市| 临高县| 霍州市| 台北县| 墨脱县| 垦利县| 福泉市| 花莲县| 漯河市| 黄龙县| 怀宁县| 自贡市| 绵阳市| 青龙| 长海县| 临湘市| 农安县| 池州市| 庆城县| 长乐市| 南昌市| 开化县| 安乡县| 孟津县| 卢氏县| 丹东市| 洛川县|