X hits on this document

334 views

0 shares

0 downloads

0 comments

53 / 102

   var previousXPosition = projectilePosition.X;

   var previousYPosition = projectilePosition.Y;

   projectilePosition.X = projectileStartPosition.X +

       (direction * projectileVelocity.X * flightTime) +

       0.5f * (8 * wind * (float)Math.Pow(flightTime, 2));

   projectilePosition.Y = projectileStartPosition.Y -

       (projectileVelocity.Y * flightTime) +

       0.5f * (gravity * (float)Math.Pow(flightTime, 2));

   // Calculate the projectile rotation

projectileRotation += MathHelper.ToRadians(projectileVelocity.X * 0.5f);

   // Check if projectile hit the ground or even passed it

   // (could happen during normal calculation)

   if (projectilePosition.Y >= 332 + hitOffset)

   {

       projectilePosition.X = previousXPosition;

       projectilePosition.Y = previousYPosition;

       ProjectileHitPosition = new Vector2(previousXPosition, 332);

       groundHit = true;

   }

   else

   {

       groundHit = false;

   }

}

Let us review the above function. First we keep track of the projectile’s total flight time by incrementing the value we have previously stored with the time elapsed since this method was last invoked (provided by the caller using the gameTime parameter). Next, we calculate the projectile’s new position according to its initial velocity, the wind and the effects of gravity. After calculating the projectile’s new position, we rotate it according to how fast it is travelling and check whether or not it has hit the ground. If the projectile has hit the ground, we alter its position slightly so that it does not appear as if it has entered the ground and store the hit position for later use.

125.

Now add one last method to the Projectile class:

C#

public void Fire(float velocityX, float velocityY)

{

Page | 53

Document info
Document views334
Page views334
Page last viewedWed Jan 18 17:07:19 UTC 2017
Pages102
Paragraphs3115
Words17333

Comments