Rocket calculation for defense tower java

I am trying to develop a small tower defense game java 2d and I am having problems trying to figure out how to make my rockets. After hours of searching and testing, I got even more confused.

What I still have:

  • 4 cases, depending on where the tower is located, in accordance with the unit in which it shoots (NW, NE, SW, SE).
  • I need to calculate the distance between the current target and the tower that the rocket is coming from using Math.sqrt(x2, x1, y2, y1) .
  • Scaling x and rocket.

Now the problem I am facing is how to scale the x and y increment of the rocket to the target so that it looks realistic. Math is not my strong suit, and it shows here. Below I show what I have for the quadrant SE 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; 
+6
source share
5 answers

If you need a fixed speed, just use:

 scale = someConstant; 

To move a bullet, you can use the direction vector that you already found, but you must normalize it by dividing the distance:

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

Basically, you get a velocity unit vector with the direction you want to go, and multiply it by speed to get the actual speed vector. However, your xBullet and yBullet should have floating point values ​​(like double), not integer ones.

+3
source

I am not sure to get your thought. Anyway, you could not just do something like.

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

this is an easy way to get a destination through a segment in a given number of steps

+1
source

Today I am going to make Tower Defense. I thought about it, and this is how I will shoot from my towers, etc.

How to calculate the distance to the target: Pythagorean theorem. x target - x tower for one length and y target - y tower for another length. Sqr are both of them and, add them together and sqr root, the number you get. It's useless. Although it will take some other code to work correctly (you can get the length in negative values, so you will need to switch to positive again)

condition: if the distance to the target is less than the attack range of the tower: action: shoots of the tower answer: create a bullet with a target fixed on it

: Compare x and y between the target and the rocket and increase x or y accordingly.

action: x and y rockets are the same as x and y targets answer: damage the target and remove the rockets

+1
source

Now the problem I am facing is how to scale the x and y increment of the rocket to the target so that it looks realistic.

How realistic is this?

I am not a rocket expert, but I would suggest that once launched simple rockets can be approximated by a fixed direction and speed. Then they just keep going in that direction until they hit something, run out of fuel, or gravity dumps them. When a rocket leaves, you can save its direction and speed and transfer the radio to the appropriate constant distance at each step (you can use trigonometric formulas to convert to x and y coordinates).

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

As stated in the comments, you can use the velocity vector instead of separately storing the angle and speed.

Minor note: constant speed is only an approximation for a few reasons. When the first rocket starts, it should accelerate until the thrust is equal to the resistance from air resistance. In addition, when the fuel burns, they can actually travel faster due to the fact that they have the same power, but less mass. But this level of detail is not needed for the tower defense game. Constant speed is a reasonable simplification.

I don’t think this algorithm will work well for you if your targets can move and change quickly because missiles can often miss their targets.

More advanced heat-seeking missiles will be able to fix the target and follow it. They should be able to change direction when their target moves, but I think they probably don't adjust their speed. They will also be limited by how quickly they can change direction due to physical limitations, so you can also simulate this. Otherwise, you may have missiles flying past the target, and then suddenly flipping over and immediately flying in the opposite direction without first slowing down or turning - and this is not very realistic.

Complex missiles may try to predict where their target is moving based on their current speed. They would calculate such a path so that the current trajectory of the target collides with the calculated trajectory of the rocket. This calculation should be updated if the target movement changes. Obviously, this is a little harder to implement, but remember that your players want to have fun with explosive monsters. They will not have much fun if all the missiles do not miss their targets. So a little extra work to ensure that your missiles are realistic and effective will make your game better.

0
source

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

0
source

Source: https://habr.com/ru/post/921623/


All Articles