Game Development Reference
In-Depth Information
The Physics Class
Our Physics class contains code related to updating the object's position and rotation, based on
linear and rotational forces that are applied to the object. The Object3d class contains a Physics
class variable called m_Physics . All of the object's physics-related data, such as velocity and
acceleration, and physics functions, such as applying a force to the 3D object, are contained in this
variable. This section covers our Physics class.
Listing 5-1 shows some useful constants that are used in our Physics class. PI is defined in radians
and is equivalent to 180 degrees, or a half circle. TWO_PI is defined as twice the value of PI in radians
and is 360 degrees, or a full circle. HALF_PI is defined as PI/2 , which is 90 degrees, or a right angle.
A QUARTER_PI is defined as PI/4 , which is 45 degrees.
Listing 5-1. Static Physics Constants
static float PI = (float)(3.14159265358979323846264338327950288419716939937511);
static float TWO_PI = (float)(2.0*PI);
static float HALF_PI = (float)(PI/2.0);
static float QUARTER_PI = (float)(PI/4.0);
In Listing 5-2, I define variables related to linear velocity and acceleration. The variable m_Velocity
holds the object's linear velocity in the x, y, and z directions and is created and initialized to (0,0,0) .
The variable m_Acceleration holds the object's linear acceleration in the x, y, and z directions and
is created and initialized to (0,0,0) . The variable m_MaxVelocity holds the maximum absolute linear
velocity in the x, y, z directions that the object can achieve. The m_MaxAcceleration variable holds
the maximum absolute linear acceleration in the x, y, and z directions that the object can achieve.
Listing 5-2. Linear-Related Physics Variables
private Vector3 m_Velocity = new Vector3(0,0,0);
private Vector3 m_Acceleration = new Vector3(0,0,0);
private Vector3 m_MaxVelocity = new Vector3(1.25f, 1.25f, 1.25f);
private Vector3 m_MaxAcceleration = new Vector3(1.0f,1.0f,1.0f);
The code in Listing 5-3 deals with angular velocity and acceleration. The m_AngularVelocity variable
holds the angular velocity of an object around its rotational axis. The m_AngularAcceleration variable
holds the angular acceleration of an object around its rotational axis. The m_MaxAngularVelocity
variable holds the maximum absolute value of the angular velocity. The m_MaxAngularAcceleration
variable holds the maximum absolute value of the angular acceleration for the object.
Listing 5-3. Angular Velocity and Acceleration Variables
private float m_AngularVelocity = 0;
private float m_AngularAcceleration = 0;
private float m_MaxAngularVelocity = 4 * PI;
private float m_MaxAngularAcceleration = HALF_PI;
Listing 5-4 shows the gravity-related variables in the Physics class. The m_ApplyGravity variable is
true if gravity is to be applied to the object, and by default it is false. The variable m_Gravity specifies
the gravitational acceleration acting on the object. The m_GroundLevel variable specifies the height
of the ground. The m_JustHitGround variable is true if the object has just hit the ground level. The
m_Mass variable holds the mass of the object.
 
Search WWH ::




Custom Search