Game Development Reference
In-Depth Information
Listing 8-8. DynamicGameObject.java; Extending the GameObject with a Velocity and Acceleration Vector
package com.badlogic.androidgames.framework;
import com.badlogic.androidgames.framework.math.Vector2;
public class DynamicGameObject extends GameObject {
public final Vector2 velocity;
public final Vector2 accel;
public DynamicGameObject( float x, float y, float width, float height) {
super (x, y, width, height);
velocity = new Vector2();
accel = new Vector2();
}
}
We extend the GameObject class to inherit the position and bounds members. Additionally, we
create vectors for the velocity and acceleration. A new dynamic game object will have zero
velocity and acceleration after it has been initialized.
In our cannonball example, we have the cannon, the cannonball, and the targets. The cannonball
is a DynamicGameObject , as it moves according to our simple physics model. The targets
are static and can be implemented using the standard GameObject . The cannon can also be
implemented via the GameObject class. We will derive a Cannon class from the GameObject class
and add a field storing the cannon's current angle. Listing 8-9 shows the code.
Listing 8-9. Cannon.java; Extending the GameObject with an Angle
package com.badlogic.androidgames.gamedev2d;
public class Cannon extends GameObject {
public float angle;
public Cannon( float x, float y, float width, float height) {
super (x, y, width, height);
angle = 0;
}
}
This nicely encapsulates all the data needed to represent an object in our cannon world.
Every time we need a special kind of object, like the cannon, you can simply derive one from
GameObject , if it is a static object, or from DynamicGameObject , if it has a velocity and acceleration.
 
Search WWH ::




Custom Search