Game Development Reference
In-Depth Information
3.
Then the location is clamped to the minimum location value in Min by taking
the greater value between Min and the clamped location from step 2.
4.
The final clamped location from step 3 is returned.
Listing 10-24. Generating a Random Grid Location Within Boundaries
Vector3 GenerateGridLocationRestricted(Vector3 Max, Vector3 Min)
{
Vector3 ClampedLocation = new Vector3(0,0,0);
Vector3 OriginalLocation = null;
OriginalLocation = GenerateRandomGridLocation();
ClampedLocation.x = Math.min(OriginalLocation.x, Max.x);
ClampedLocation.y = Math.min(OriginalLocation.y, Max.y);
ClampedLocation.z = Math.min(OriginalLocation.z, Max.z);
ClampedLocation.x = Math.max(ClampedLocation.x, Min.x);
ClampedLocation.y = Math.max(ClampedLocation.y, Min.y);
ClampedLocation.z = Math.max(ClampedLocation.z, Min.z);
return ClampedLocation;
}
The GenerateRandomVelocityArenaObjects() function generates a random velocity for an arena
object on the xz plane. (See Listing 10-25.)
The function does the following:
1.
Generates a random speed along the x axis by multiplying the maximum
speed for an arena object by a randomly generated number within the range
of 0 to 1
2.
Generates a random speed along the z axis by multiplying the maximum
speed for an arena object by a randomly generated number within the range
of 0 to 1
3.
Creates the final velocity by using the x and z values from steps 1 and 2 with
a y value of 0
4.
Returns the final random velocity
Listing 10-25. Generating a Random Velocity
Vector3 GenerateRandomVelocityArenaObjects()
{
Vector3 Velocity = new Vector3(0,0,0);
float VelX = m_MaxSpeedArenaObjects * m_RandNumber.nextFloat();
float VelZ = m_MaxSpeedArenaObjects * m_RandNumber.nextFloat();
 
Search WWH ::




Custom Search