M3G should not be mistaken for Java 3D, which extends the capabilities of the Java SE. Java 3D is designed for PCs that have more memory and greater processing power than mobile devices. M3G and Java 3D are two separate and incompatible APIs designed for different purposes.
3D co-ordinate system
Coordinate systems are used to describe the locations of points in space.
In m3g the orthographic coordinate system is used. In an orthographic coordinate system, the X, Y and Z axis are perpendicular to each other. The right-hand rule is used, with +Y (index finger) being up, +X (thumb) horizontal to the right, and +Z (middle) directed toward the viewer.
In m3g, three coordinate systems plays an important role:
* World coordinate system.
Is the base on which all other coordinates are being defined.
* Camera coordinate system.
This coordinate system is aligned with the eye or camera. The +Y axis is up, and the -Z axis points out off the camera. In order to render the world, everything in the scene must be transformed to camera coordinates.
* Local (or object) coordinate system.
Each object in the scene is defined in its own local coordinate system. By using a local coordinate system you can create many copies of the object, in different positions and orientations.

Translation by a constant offset [tx ty tz]T moves a vertex a to [ax+tx ay+ty az+tz]T and is expressed by the matrix

The rotation matrices around X-axis is given below:

The scaling Matrix is

Shearing is the last type of transformation and is shown by

M3G Immediate mode - Cube example
Figure:Sample cube: a) Front view with vertex indices, b) Side view with clipping planes (Front, Side)
As you can see in Figure 1, the camera that shoots the 3D scene looks toward the negative z axis, facing the cube. The camera's position and properties define what the screen later displays. Figure b shows a side view of the same scene so you can easily see what part of the 3D world is visible to the camera. One limiting factor is the viewing angle, which is comparable to using a camera lens: a telephoto lens has a narrower view than a wide-angle lens. The viewing angle thus determines what you can see to the sides. Unlike the real world, 3D computing gives you two more view boundaries: near and far clipping planes. Together, the viewing angle and the clipping planes define what is called the view frustum. Everything inside the view frustum is visible, everything outside is not.
This is all implemented in the VerticesSample class, whose members you can see in code
As you can see in Figure 1, the camera that shoots the 3D scene looks toward the negative z axis, facing the cube. The camera's position and properties define what the screen later displays. Figure 1b shows a side view of the same scene so you can easily see what part of the 3D world is visible to the camera. One limiting factor is the viewing angle, which is comparable to using a camera lens: a telephoto lens has a narrower view than a wide-angle lens. The viewing angle thus determines what you can see to the sides. Unlike the real world, 3D computing gives you two more view boundaries: near and far clipping planes. Together, the viewing angle and the clipping planes define what is called the view frustum. Everything inside the view frustum is visible, everything outside is not.
This is all implemented in the VerticesSample class, whose members you can see in code
As you can see in Figure 1, the camera that shoots the 3D scene looks toward the negative z axis, facing the cube. The camera's position and properties define what the screen later displays. Figure 1b shows a side view of the same scene so you can easily see what part of the 3D world is visible to the camera. One limiting factor is the viewing angle, which is comparable to using a camera lens: a telephoto lens has a narrower view than a wide-angle lens. The viewing angle thus determines what you can see to the sides. Unlike the real world, 3D computing gives you two more view boundaries: near and far clipping planes. Together, the viewing angle and the clipping planes define what is called the view frustum. Everything inside the view frustum is visible, everything outside is not.
This is all implemented in the VerticesSample class, whose members you can see in code
package m3gsamples1;
import javax.microedition.lcdui.*;
import javax.microedition.m3g.*;
/**
* Sample displaying a cube defined by eight vertices, which are connected
* by triangles.
*
* @author Claus Hoefele
*/
public class VerticesSample extends Canvas implements Sample
{
/** The cube's vertex positions (x, y, z). */
private static final byte[] VERTEX_POSITIONS = {
-1, -1, 1, 1, -1, 1, -1, 1, 1, 1, 1, 1,
-1, -1, -1, 1, -1, -1, -1, 1, -1, 1, 1, -1
};
/** Indices that define how to connect the vertices to build
* triangles. */
private static int[] TRIANGLE_INDICES = {
0, 1, 2, 3, 7, 1, 5, 4, 7, 6, 2, 4, 0, 1
};
/** The cube's vertex data. */
private VertexBuffer _cubeVertexData;
/** The cube's triangles defined as triangle strips. */
private TriangleStripArray _cubeTriangles;
/** Graphics singleton used for rendering. */
private Graphics3D _graphics3d;
vertex 0 in the figure a is defined as position (-1, -1, 1). I placed the cube's center at the coordinate system's origin.
M3G poses one restriction though: you must build the geometry from triangles. Triangles are popular with 3D implementations because you can define any polygon as a set of triangles. A triangle is a basic drawing operation on which you can build more abstract operations.
Unfortunately, if you had to describe the cube with triangles alone, you would need 6 sides * 2 triangles * 3 vertices = 36 vertices; this would be a waste of memory as many vertices are duplicated. To reduce memory, you first separate the vertices from their triangle definitions. TRIANGLE_INDICES defines the geometry using the indices of the VERTEX_POSITIONS array and can thus reuse vertices.
Code for initializatoin
/**
* Called when this sample is displayed.
*/
public void showNotify()
{
init();
}
/**
* Initializes the sample.
*/
protected void init()
{
// Get the singleton for 3D rendering.
_graphics3d = Graphics3D.getInstance();
// Create vertex data.
_cubeVertexData = new VertexBuffer();
VertexArray vertexPositions =
new VertexArray(VERTEX_POSITIONS.length/3, 3, 1);
vertexPositions.set(0, VERTEX_POSITIONS.length/3, VERTEX_POSITIONS);
_cubeVertexData.setPositions(vertexPositions, 1.0f, null);
// Create the triangles that define the cube; the indices point to
// vertices in VERTEX_POSITIONS.
_cubeTriangles = new TriangleStripArray(TRIANGLE_INDICES,
new int[] {TRIANGLE_INDICES.length});
// Create a camera with perspective projection.
Camera camera = new Camera();
float aspect = (float) getWidth() / (float) getHeight();
camera.setPerspective(30.0f, aspect, 1.0f, 1000.0f);
Transform cameraTransform = new Transform();
cameraTransform.postTranslate(0.0f, 0.0f, 10.0f);
_graphics3d.setCamera(camera, cameraTransform);
}
After the initialization, you render the scene to the screen. code 3 shows this.
/**
* Renders the sample on the screen.
*
* @param graphics the graphics object to draw on.
*/
protected void paint(Graphics graphics)
{
_graphics3d.bindTarget(graphics);
_graphics3d.clear(null);
_graphics3d.render(_cubeVertexData, _cubeTriangles,
new Appearance(), null);
_graphics3d.releaseTarget();
}
M3G's Retained mode
In retained mode, you define and display an entire world of 3D objects, including information on their appearance. Imagine retained mode as a more abstract, but also more comfortable, way of displaying 3D graphics.
Cube Example in retained mopde
protected void init(){
_graphics3d = Graphics3D.getInstance();
_world = new World();
VertexBuffer cubeVertexData = new VertexBuffer();// Create vertex data.
VertexArray vertexPositions = new VertexArray(VERTEX_POSITIONS.length/3, 3, 1);
vertexPositions.set(0, VERTEX_POSITIONS.length/3, VERTEX_POSITIONS);
cubeVertexData.setPositions(vertexPositions, 1.0f, null);
// Create the triangles that define the cube; the indices point to vertices in VERTEX_POSITIONS.
TriangleStripArray cubeTriangles = new TriangleStripArray(
TRIANGLE_INDICES, new int[] {TRIANGLE_INDICES.length});
// Create a Mesh that represents the cube.
Mesh cubeMesh = new Mesh(cubeVertexData, cubeTriangles, new Appearance());
_world.addChild(cubeMesh);
// Create a camera with perspective projection.
Camera camera = new Camera(); float aspect = (float) getWidth() / (float) getHeight();
camera.setPerspective(30.0f, aspect, 1.0f, 1000.0f); camera.setTranslation(0.0f, 0.0f, 10.0f);
_world.addChild(camera); _world.setActiveCamera(camera);
}
protected void paint(Graphics graphics) {
_graphics3d.bindTarget(graphics);
_graphics3d.render(_world);
_graphics3d.releaseTarget();
}
Collision Detection
Collision detection is done in order to determine if two objects collide. Collision detection is an important aspect, as a signifi cant component of the players'interaction in the game is determined by collision queries, and collision response. Performing collision detection is often not possible to do in real-time if certain acceleration algorithms aren't used. Collision handling involves three separate stages when working with object interaction. These stages are collision detection, collision determination and collision response. Collision detection tells us if the objects collide. Collision determination tells us how the objects collide. Lastly the collision response determines how objects are affected by each other after a collision has been detected, such as a change of momentum. Most importantly, one should distinguish between collision detection and collision response, whereas the latter dictates the response which occurs after an actual collision has been detected.
M3G does not support collision detection, but it does provide for simple
picking—that is, shooting a ray into the scene to see which object and triangle it first intersects.
This can be used as a replacement to proper collision detection in some cases.
A method that is used for precise collision detection for convex polyhedral objects is the Voronoi-clip algorithm also called V-clip. The V-clip algorithm is a feature-
based algorithm. The features of a polyhedron are the vertices, edges and faces of the object.
