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

Time for action – position it!

To practice positioning objects, we create a second cube and place it next to the first one. Open the BasicGame template and look at the simpleInitApp() method.

  1. Create a com.jme3.math.Vector3f object. This is the target location for the second cube:
    Vector3f v = new Vector3f(2.0f , 1.0f , -3.0f);
  2. Copy the code snippet that creates the blue cube and adjust it to create a second, yellow cube.
  3. Name the new Box object b2, the geometry geom2, and the material mat2. (Make sure to rename all instances!)
  4. Change the Color parameter in the material mat2 to ColorRGBA.Yellow.
  5. Move the second box to its new location v:
    geom2.setLocalTranslation(v);
  6. Attach geom2 to the rootNode to add it to the scene.
  7. Clean and build the BasicGame template, and right-click the class to run this file.

You should see a yellow cube behind the blue cube, but positioned a bit higher (y=1), a bit to the right (x=2), and further in the back (z=-3).

What just happened?

Positioning an object, such as our cubes in the 3D scene, is called translation. You create geometries at the origin, then translate them to their intended position, and then attach them to the rootNode.

Tip

Typically, you never create Box() objects and so on with an offset in the constructor. The offset will just get you into trouble later when the geometry does not move as expected. Always start at the origin, as in Box b = new Box(Vector3f.ZERO, 1, 1, 1);.

In Java, you can choose between two methods when you position objects:

If you start out with both boxes at the origin, both methods result in the same target location. If you use the move() method several times, the offsets add up. If you use the setLocalTranslation() method several times, you simply place the cube at a new absolute location. Try these methods several times on each cube, with different values, and try to predict what happens.

主站蜘蛛池模板: 禹州市| 五华县| 乐陵市| 平定县| 高青县| 原阳县| 唐河县| 日照市| 松溪县| 郑州市| 枣庄市| 资溪县| 庐江县| 岢岚县| 唐海县| 门头沟区| 龙游县| 扬州市| 天长市| 盐亭县| 玉山县| 阿瓦提县| 资源县| 海城市| 思南县| 梁山县| 同仁县| 乌拉特中旗| 淮滨县| 互助| 普格县| 临潭县| 绩溪县| 宜昌市| 英吉沙县| 手游| 杭锦后旗| 徐汇区| 龙泉市| 普陀区| 万安县|