Friday, June 1, 2007

Mobile Java 3D Tips, Tricks & Code

JSR 184: 3D co-ordinates and orientation overview



This article gives a brief introduction to the 3D co-ordinate system and orientation in the 3D world using the JSR 184 API.

Download MIDlet and source code>>


Figure 1.

The co-ordinate system in 3D space is shown in the image to the left (Figure 1). This is called world co-ordinates and all objects in the 3D scene can be moved around and rotated in this co-ordinate system.

This co-ordinate system is the same for all objects in the scene and it will never change. The default view for the user is down the negative z-axis.

In addition to the world co-ordinates, each object also has a local co-ordinate system and the figures can also be moved within the local co-ordinate system (Figure 2).
The big difference between the co-ordinate systems is that the local co-ordinate system's orientation may change; the local co-ordinate system is actually rotated with the figure if the figure is rotated (Figure 3).

Figure 2.

Figure 3.

This can sometimes be confusing. For example, if the figure is first rotated 90 degrees on the z-axis and then translated on the y-axis in the local coordinate system the visible effect will be a translation on the x-axis in the world.

When rotating an object, the result may be different depending on the method that is used for the rotation. The mesh can be translated and rotated around its local center or it can be translated and rotated around the world center. (Figure 4).

Translation and rotation of an object can be made on all Transformable objects in JSR 184 and that includes: Mesh, Light, camera, Group and Sprite3D. Translation and rotation can also be set to a Transformable using the Transform class.

Figure 4.

In JSR 184 it is also possible to add a mesh to a group. For example if a group is added to the world and then a mesh is added to the group, the translation of the node will be in the co-ordinate system of the parent.
The basic functions are:
Transformable:
- postRotate(float angle, float ax, float ay, float az)
- translate(float tx, float ty, float tz)
Transform:
- postRotate(float angle, float ax, float ay, float az)
- postTranslate(float tx, float ty, float tz)
mesh.postRotate(3.0f, 1.0f, 0.0f, 0.0f);
When rotation is made directly on the transformable object the rotation will be made around the origo of the local co-ordinate system.
transform.postRotate(3.0f, 1.0f, 0.0f, 0.0f);
mesh.setTransform(transform);
When the rotation is made using the transform class the object will rotate around its center.
mesh.translate(0.0f, 0.0f, 5.0f);
When the object is moved using methods in the transformable class the object is moved in the co-ordinate system of the parent node.
transform.postTranslate(0.0f, 0.0f, 5.0f);
mesh.setTransform(transform)
When the object is moved using the transform class the object is moved in the local co-ordinate system.

To get a visual effect of the different methods download the source code and example MIDlet here >>

No comments: