Game Development Reference
In-Depth Information
Listing 5-27. Accessing Key Grid Data
int GetMaxMasses(){return MAX_MASSES;}
int GetNumberMassesOnGrid(){return m_NumberMasses;}
float GetXMinBoundary(){return m_XMinBoundary;}
float GetXMaxBoundary() {return m_XMaxBoundary;}
float GetZMinBoundary(){return m_ZMinBoundary;}
float GetZMaxBoundary(){return m_ZMaxBoundary;}
The function
AddMass()
in Listing 5-28 adds an object to the gravity grid. This function has to be
used with the
ClearMasses()
function, in order to make sure all the masses currently on the gravity
grid are up to date.
The function does the following:
1.
Calculates the indices for the location and spotlight arrays for this new object
2.
Checks to see if the gravity grid is already full. If it is, then it returns with a
false value
Places the value of the new object's mass in the
m_MassValues
array
3.
Places the x, y, z position of the new object in the
m_MassLocations
array
using the index calculated from step 1
4.
Places the radius of the spotlight for the object in the
m_MassEffectiveRadius
array
5.
Places the spotlight color for the object in the
m_MassSpotLightColor
array
using the index calculated in step 1
6.
Listing 5-28. Adding a Mass to the Gravity Grid
boolean AddMass(Object3d Mass)
{
boolean result = true;
int MassLocationIndex = MassesIndex * 3; // each mass has 3 components x,y,z
int SpotLightLocationIndex = MassesIndex * 3; // each spotlight has 3 components r,g,b
if (m_NumberMasses >= MAX_MASSES)
{
result = false;
return result;
}
float[] Color;
// Add Value of the Mass
m_MassValues[MassesIndex] = Mass.GetObjectPhysics().GetMass();
// Add the x,y,z location of the Mass
m_MassLocations[MassLocationIndex] = Mass.m_Orientation.GetPosition().x;
m_MassLocations[MassLocationIndex + 1]= Mass.m_Orientation.GetPosition().y;