Game Object3D and Dynamic GameObject3D

Now that we have a nice bounding shape for our 3D objects, we can easily write the equivalent of the GameObject and DynamicGameObject classes we used in 2D. We just replace any Vector2 with a Vector3 instance and use the Sphere class instead of the Rectangle class. Listing 11-15 shows you the GameObject3D class.

Listing 11-15. GameObject3D, Representing a Simple Object with a Position and Bounds package com.badlogic.androidgames.framework;

import com.badlogic.androidgames.framework.math.Sphere; import com.badlogic.androidgames.framework.math.Vector3;

public class GameObject3D {

public final Vector3 position; public final Sphere bounds;

public GameObject3D(float x, float y, float z, float radius) { this.position = new Vector3(x,y,z); this.bounds = new Sphere(x, y, z, radius);

This code is so trivial, you probably don't need any explanation. The only hitch is that we have to store the same position twice: once as the position member in the GameObject3D class, and again within the position member of the Sphere instance that's contained in the GameObject3D class. That's a tiny bit ugly, but for the sake of clarity we'll stick to this.

Deriving a DynamicGameObject3D class from this class is simple as well. Listing 11-16 shows you the code.

Listing 11-16., the Dynamic Equivalent to GameObject3D package com.badlogic.androidgames.framework;

import com.badlogic.androidgames.framework.math.Vector3;

public class DynamicGameObject3D extends GameObject { public final Vector3 velocity; public final Vector3 accel;

public DynamicGameObject3D(float x, float y, float z, float radius) { super(x, y, z, radius); velocity = new Vector3(); accel = new Vector3();

We again just replace any Vector2 with a Vector3 and smile happily.

In 2D we had to think hard about the relationship between the graphical representation of our objects (given in pixels) and the units used within the model of our world. In 3D we can break free from this! The vertices of our 3D models that we load from, say, an OBJ file can be defined in whatever unit system we want. We no longer need to transform pixels to world units and vice versa. This makes working in 3D a little. We just need to train our artist so that she provides us with models that are properly scaled to the unit system of our world.

Was this article helpful?

0 0

Post a comment