Game Development Reference
In-Depth Information
The function does the following:
Creates an integer array View that holds the current screen view parameters of
the Android device.
1.
Calls MapWindowCoordsToWorldCoords() function with the view parameters,
along with the x and y locations that the user has touched and a z value of
1. The 3D homogeneous world coordinates are returned in the WorldCoords
float array.
2.
3.
Converts the homogeneous coordinates to Cartesian coordinates by dividing
WorldCoords by the w value or fourth element in the WorldCoords array. The
result is stored in TargetLocation .
Defines the WeaponLocation variable as the location of the camera or viewer.
4.
Defines the Direction variable, which is the direction the weapon is to be
fired, as a vector going from the WeaponLocation to the TargetLocation .
5.
Fires the player's weapon with the projectile starting at the WeaponLocation
location and in the direction Direction .
6.
7.
Plays the player's weapon sound effect.
Listing 7-62. Checking the User's Touch for Firing the Weapon
void CheckTouch()
{
// Player Weapon Firing
int[] View = new int[4];
View[0] = 0;
View[1] = 0;
View[2] = m_ViewPortWidth;
View[3] = m_ViewPortHeight;
float[] WorldCoords = MapWindowCoordsToWorldCoords(View, m_TouchX, m_TouchY, 1);
// 1 = far clipping plane
Vector3 TargetLocation = new Vector3(WorldCoords[0]/WorldCoords[3],
WorldCoords[1]/WorldCoords[3], WorldCoords[2]/WorldCoords[3]);
Vector3 WeaponLocation = m_Camera.GetCameraEye();
Vector3 Direction = Vector3.Subtract(TargetLocation, WeaponLocation);
if ((Direction.x == 0) && (Direction.y == 0) && (Direction.z == 0))
{
return;
}
if (m_Weapon.Fire(Direction, WeaponLocation) == true)
{
// WeaponFired
PlayPlayerWeaponSound();
}
}
Search WWH ::




Custom Search