Game Development Reference
In-Depth Information
Listing 7-19. Updating the Ammunition
void UpdateAmmunition()
{
// 1. Update Ammunition Physics, Position, Rotation
UpdateObject3d();
}
The
Fire()
function is the key function in terms of actually firing the ammunition this class
represents.
The
Fire()
function (see Listing 7-20) does the following:
Sets the
m_FireStatus
to true to indicate that this piece of ammunition has
been fired and is currently moving through the 3D world
1.
Calculates the velocity of the ammunition based on the input
Direction
vector, the
m_AmmoSpeed
ammunition speed, and the
OffsetVelocity
vector,
which represents the movement of the object firing the ammunition
2.
Sets the position of the ammunition based on the input
AmmoPosition
parameter
3.
Sets the
m_AmmunitionStartPosition
class member variable
4.
Listing 7-20. Firing the Ammunition
void Fire(Vector3 Direction,
Vector3 AmmoPosition,
Vector3 OffSetVelocity)
{
// 1. Set Fire Status to true
m_FireStatus = true;
// 2. Set direction and speed of Ammunition
// Velocity of Ammo
Vector3 DirectionAmmo = new Vector3(Direction.x, Direction.y, Direction.z);
DirectionAmmo.Normalize();
Vector3 VelocityAmmo = Vector3.Multiply(m_AmmoSpeed, DirectionAmmo);
// Velocity of Object with Weapon that has fired Ammo
// Total Velocity
Vector3 VelocityTotal = Vector3.Add(OffSetVelocity , VelocityAmmo);
GetObjectPhysics().SetVelocity(VelocityTotal);
m_Orientation.GetPosition().Set(AmmoPosition.x, AmmoPosition.y, AmmoPosition.z);
// 3. Set Ammunition Initial World Position
m_AmmunitionStartPosition.Set(AmmoPosition.x, AmmoPosition.y, AmmoPosition.z);
}