6

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;
pb2q
  • 58,613
  • 19
  • 146
  • 147
cjr
  • 520
  • 2
  • 7
  • 22
  • You should consider using / writing a Vector2D class simplifying vector calculations. You will need them alot in game development. Implementing such a class should be easy. – Sebastian Hoffmann Jul 29 '12 at 22:06
  • 1
    *"Math is not my strong suit"* Seems writing your own game physics might be a bit of a problem then. Why not use an existing game framework? – Andrew Thompson Jul 30 '12 at 01:03
  • @cjr, how did you end up solve your problem? – Bruno Bieri Aug 05 '14 at 12:47
  • This was a couple years ago and the project ended up falling and don't have the code/remember what I did to solve the issue :D – cjr Sep 20 '14 at 19:41

5 Answers5

3

If you want a fixed velocity, simply use:

scale = someConstant;

To move the bullet, you can use the direction vector you already found, but you have to normalize it by dividing by distance:

xBullet += (distanceX / sep) * scale;
yBullet += (distanceY / sep) * scale; 

Basically you get the velocity unit vector with the direction you want to go, and you multiply it against speed to get the actual velocity vector. Your xBullet and yBullet fields should be floating pointing values (eg. doubles), not integers, though.

Zong
  • 6,160
  • 5
  • 32
  • 46
  • There is nothing wrong in number being integer :) As long as it can be big enough. – dantuch Jul 29 '12 at 21:27
  • If the bullet is travelling anywhere close to the horizontal/vertical, then you will see problems with using integers. – Zong Jul 29 '12 at 21:28
1

I'm not sure to get your point..apologize. Anyway couldn't you just do something like..

xMissile += (xTarget-xOrigin)/numberOfSteps
yMissile += (yTarget-yOrigin)/numberOfSteps

that's just an easy way to get a point to destination through a segment in the given number of steps

Diego D
  • 6,156
  • 2
  • 17
  • 30
1

I'm going to make a Tower Defence today. I have thought about it and this is how I will shoot from my towers etc.

How to calc distance to target: Pythagorean theorem. x target - x tower for one length of it and y target - y tower for other length of it. Sqr both of them and, add them together and sqr root the number you get. Thats the disatance to target. Although it would need some other code to work properly (you might get lengths in negative values, so you would need to change to to positive again)

condition: if distance to target is less than attack range of tower: action: tower shoots response : create a bullet that has a target fixed on it

loop : compare x and y between target and missile and increase x or y accordingly.

action : missile x and y are same as target x and y response : deal damage to target and remove missile

Kristupas A.
  • 304
  • 5
  • 15
0

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.

How realistic does this need to be?

I'm no missile expert but I'd guess that once fired simple missiles can be approximated by a fixed direction and speed. Then they will just keep going in that direction until they hit something, run out of fuel, or gravity brings them down. When the missile is fired you can store its direction and speed, and move the missile the appropriate constant distance at each step (you can use the trigonometric formulae to convert to x and y co-ordinates).

double delta_distance = speed * delta_time;
double delta_x = Math.cos(angle) * delta_distance;
double delta_y = Math.sin(angle) * delta_distance;

As pointed out in the comments, you could choose to use a velocity vector instead of separately storing the angle and speed.

Minor note: The constant speed is only an approximation for several reasons. When the missile is first fired it will have to accelerate until the propulsion force equals the drag from air resistance. Also as the fuel burns up they may actually be able to travel faster due to having the same force but less mass. But this level of detail is not necessary for a tower defence game. A constant speed is a reasonable simplification.

I don't think this algorithm will work well for you if your targets might be moving quickly and changing direction, because the missiles could often miss their targets.

More advanced heat-seeking missiles would be able to lock on to a target and follow it. They need to be able to change direction when their target moves, but I guess they probably don't adjust their speed. They would also be limited on how fast they can change direction due to physical constraints, so you might want to model that too. Otherwise you could have missiles flying past a target then suddenly flipping around and immediately flying in the opposite direction without first slowing down or turning - and that's not very realistic.

Sophisticated missiles might try to predict where their target is moving to based on their current velocity. They would calculate a path such that the current trajectory of the target collides with the calculated trajectory of the missile. This calculation would have to be updated if the target's movement changes. Obviously this is a little more difficult to implement, but remember that your players want to have fun blasting monsters. They won't be having much fun if all the missiles keep missing their targets. So a little extra work on making sure that your missiles are both realistic and effective will make your game better.

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
  • Well, what I tried my first attempt was not even using the distance formula and simply increment x and y by 1 until the reach the target but of course what happens is what value reaches before the other making it look blocky. I hadn't thought of making such a missile to aim for a location it would be at, that sounds amazing but might be over my head for my level of programming knowledge. – cjr Jul 29 '12 at 21:08
  • You can just use a direction vector, which is both simpler and faster. – Zong Jul 29 '12 at 21:29
  • Using vectors isn't a premature optimization; they express concepts such as velocity very naturally. I think using angles for rotations would actually be a case of premature optimization, since he didn't mention a need. – Zong Jul 29 '12 at 21:46
  • @Zongli: Well thank you for your explaining comments. I hope that the OP can see that the two methods give the same result in the simple case. For the more complicated case, then obviously he's going to need to work with angles to keep the realism so I'm keeping angles in my answer to keep it consistent. It also means that he can start simple and transition from simple to gradually more complex as easily as possible without requiring him to completely rewrite everything. – Mark Byers Jul 29 '12 at 21:54
  • Well, I think we should end this lengthy discussion already. :) I agree than angles are useful, but vectors are the standard way for representing these types of quantities (in both programming and physics). – Zong Jul 29 '12 at 22:30
  • +1 for parametric form. [`AnimationTest`](http://stackoverflow.com/a/3256941/230513) is an example. – trashgod Jul 29 '12 at 23:37
0

You will need a separate x scale and y scale. The x scale should be the horizontal distance from gun to target divided by the total frames of animation before it hits and the y scale would be the same but vertical distance

ghostbust555
  • 2,040
  • 16
  • 29
  • So the total frames before could be the distance I calculate, giving me something like xscale = (xbullet - xmob)/distance ? – cjr Jul 29 '12 at 21:18
  • I believe so. You could even add a variable speed by changing it to xscale = (xbullet - xmob)/(distance * speed); – ghostbust555 Jul 29 '12 at 21:23
  • +1 The [parametric form of a parabola](http://en.wikipedia.org/wiki/Parabola#Equations) fits here nicely. – trashgod Jul 29 '12 at 23:35