Java Tower Defense Missile Calculation

Viewed 1753

I am trying to develop a small java 2d tower defense game and I have run into problems trying to calculate how to do my missiles. After hours of searching and testing I am even more confused.

What I have so far is:

  1. 4 cases depending on where the tower is located according to the unit it is firing at (NW, NE, SW, SE)
  2. I need to calculate the distance between the current target and the tower the missile is coming from using the Math.sqrt(x2, x1, y2, y1).
  3. Scaling the x and y of the missile.

Now the problem I am running into is how to scale the incrementing x and y of the missile towards the target so it seems realistic. Math is not my strong suit and it is showing here. Below I have show what I have for the SE quadrant from the tower.

public int distanceX, distanceY;
public double sep, scale;

if(xBullet < Screen.mobs[shotMob].x && yBullet < Screen.mobs[shotMob].y){
        distanceX = Screen.mobs[shotMob].x- xBullet;
        distanceY = Screen.mobs[shotMob].y - yBullet;
        sep = Math.sqrt( (distanceX * distanceX) + (distanceY * distanceY));
        scale = // This is the part I am confused about.
        xBullet += distanceX * scale;
        yBullet += distanceY * scale;
5 Answers
Related