Island War Day 6

During the past week, I got missiles working to a point where they will launch, gain altitude, head towards their target and dive for the attack. They will damage the island (which means scraping off texture layers and possibly alter the terrain, not sure whether I want this, however). Buildings in the vicinity are then destroyed.

For the missile trajectory, I went for the simplest thing that could possibly work:

  • Until the missile has reached its cruise altitude, it will ascend at a 45 degree angle. If during this phase, missile closes in to the target so much that it needs to start diving, the diving phase will be entered.

  • Once at its cruise altitude, the missile will simply fly in a horizontal line towards its target. If the missile gets closes enough to its target, it will enter the diving phase.

  • In the diving phase, the missile accelerates to maximum speed and descends towards its target at a 45 degree angle. Once it hits the ground, it explodes.

That’s the basic algorithm. Instead of using a state machine or something, I fixed it up into this little piece of code:

// Two-dimensional position of the missile and its target
Vector2 flatPosition = new Vector2(this.transform.M41, this.transform.M43);
Vector2 flatTarget = new Vector2(this.target.X, this.target.Z);

// Find out how far the missile is from its target location in flat map space
float flatDistanceToTarget = Vector2.Distance(flatPosition, flatTarget);
if(flatDistanceToTarget < TravelingSpeed) {

  explode();
  return;

}

// Current height of the missile above the target
float heightAboveTarget = Math.Max(0, this.transform.M42 - this.target.Y);

// When the missile needs to start diving
float diveStartTime = flatDistanceToTarget - heightAboveTarget;

// If the missile has not reached its nosedive point yet, it will gain height
// until it reaches its traveling height. Otherwise, it will dive towards its target
if(diveStartTime > 0.0f) {
  this.velocity.Y = Math.Min(TravelingHeight - this.transform.M42, TravelingSpeed);
} else {
  this.velocity.Y = -TravelingSpeed;
}

// Flat map direction the missile has to fly to
Vector2 targetHeading = Vector2.Normalize(flatTarget - flatPosition);
this.velocity.X = targetHeading.X * TravelingSpeed;
this.velocity.Z = targetHeading.Y * TravelingSpeed;

// Update the missile's position
incrementPositionByVelocity();

Sweet, isn’t it?

Nukes will follow a similar scheme, but raise vertically into the air, travel at a much greater height and then dive down vertically as well. This allows nukes to be used for hitting targets behind the enemies air defense lines.

Leave a Reply

Your email address will not be published. Required fields are marked *

Please copy the string RsKdRn to the field below:

This site uses Akismet to reduce spam. Learn how your comment data is processed.