Solving Missile Movement Problem in 2D Game Development

  • Thread starter Jaeya
  • Start date
  • Tags
    Missile
In summary, the conversation is about a Spanish pre-university student programming a 2D game similar to "Worms" and facing a problem with a missile weapon. The weapon has three accelerations acting on it - gravity, wind, and the missile's engine. The student needs to change the differential equations into parametric equations depending on time to determine the missile's movement. The conversation also discusses the effects of wind on the projectile and provides a possible solution by using the sign function and iterative calculations to determine the missile's position over time.
  • #1
Jaeya
3
0
Hi, firstly, sorry about my bad english... I am an Spanish preuniversitary student who's programing a game in the style of "Worms" in 2D.
My problem comes about a weapon that acts like a missile. I'll explain it (it's just an cinematic study, the game doesn't care about mass and dynamics).
The weapon is a mobile object. There are three accelerations acting over it:
- The constant acceleration of gravity, directed to "down" which value is g.
- The constant acceleration of wind, directed to "right", which value is w.
- The acceleration of the engine of the missile, which magnitude is p. The direction of this acceleration is variable: the missile accelerates in the same direction of its velocity. In consequence, the equations of this accelerations will be something like this:

a_x = w + p cos #
a_y = g + p sen #
where # is the angle of the direction of the missile.
If we look on that cos # = dv_x / dv , sen # = dv_y / dv , the equations will be:

a_x = w + p (dv_x / dv)
a_y = g + p (dv_y / dv)

My question is, how can I change this differential equations into parametric equations depending on time? I mean, x=f(t), y=g(t)
The movement will have an initial velocity, v_x0, v_y0; and you can supose the movement begins in x=0, y=0.

Thanks in advance! :)
 
Physics news on Phys.org
  • #2
I think yo need to know the function of the angle in time, let's say #(t), and then simply integrate two times over each aceleration to have x and y in function of time.

For example, if #=90º, then you have a_x=w, a_y=p-g, making some integration...

v_x=v_x0+w*t, x=x_0+v_x0*t+0'5*w*t^2. Knowing that x_0=0, then you have that:

x=v_x0*t+0'5*w*t^2

And the same for y:

y=v_y0*t+0'5*(p-g)*t^2.

You need to know some of the dependence with time of the angle.
 
Last edited:
  • #3
A normal "dumb" rocket is "positively stable" (center of cross pressure behind center of mass), and a cross-component of wind will just cause the rocket to "weather vane" into the wind, until there is no cross component, so it would "lean" into the wind, and end up accelerating into the wind. This is assuming that the rocket doesn't have any "intelligent" stability control, such as gyro sensors and side thrust control.

If the rocket were "neutrally stable", so that a cross wind didn't result in a yaw reaction, the the rocket would end up with no acceleration due to the wind, just a velocity component equal to the wind. If the initial condition was vertical only velocity, the rocket would remain pointed vertically, but would travel horizontally with the speed of the wind (but no horizontal acceleration).

It would probably be eaiser to ignore the initial effects of the wind and just add a horizontal component of velocity that is the same as the wind after the rest of the calculations are done. The rocket's orientation should be in the direction of travel relative to the air it's flowing through.

In real life, when aerodynamic drag is factored in, the differential equations get too complicated to solve directly, so acceleration is calculated from velocity and position (thinner air, weaker gravity at altitude), then numerical integration is done in a small step of time to predict new velocity (and orientation) and position, and this cycle is repeated in small steps.
 
Last edited:
  • #4
Thank you for the answers, but I'm afraid you haven't understood what I tried to say.
To Coren: That's why I am asking for help! :P Let's see:
The angle, #, is not dependable on time. It depends on the velocity, more exactly, on the direction of the velocity. When I made the engine of the game, I supossed that the missile was always pointing at the place where the missile would go if it went on with inercial movement (without acceleration). In fact, you obtain the differential equations I wrote before... and that's where I don't know how to continue. Thank you!
To Jeff Reid: Thank you for your explanation, it's very good! But what I am trying to make is a very simply game, not a real rocket ;) Anywhere, I keep needing answers to my problem...
 
  • #5
Jaeya said:
But what I am trying to make is a very simply game, not a real rocket.
Which is why I suggested ignoring any acceleration due to wind, and just add a velocity component for the wind after all the other calculations are done.
 
  • #6
I can't ignore it! :P

OK, I understand what you say. But, anyway, I can't ignore the wind acceleration. If you've ever played a "Worms" game, you'll understand why. In that game, rockets are very affected by wind (sometimes even more than by gravity), so wind has a great influence over the angle # of the missile.
 
  • #7
Here is what you want.

Sign(z) must return the algebraic sign of Z. You can code it with something like z/(Abs(z). This is used to ensure that the air resistance only slows the projectile, regardless of the direction it is traveling.

The parameter A will determine the strength of the air resistance, and you should just play with values until the animation looks right.

a_x = -Sign(v_x-v_w) A*(v_x-v_w)^2 + Sign(v_x-v_w) p_x (notice the v_w part goes to zero at v_x = v_w)

a_y = -g - Sign(v_y) A*(v_y)^2 + Sign (v_y) p_y

where v_w is the wind speed (positive if it goes in the positive x direction), and _x and _y indicate multiplication by the proper trig ratios.

It is not so easy to just turn these into (analytical) functions of time, but it is quite simple to do it iteratively. Define initial values for your velocity, and a time step (dt) (to coincide with the simulation time beween frames), then evaluate a_x. Set v_x to v_x + a_x *dt, and x = x + v_x * dt. (and the same for y). Draw your frame, and then run this sequence again as a loop until impact or whichever exit event.
 
Last edited:

1. How do you calculate the trajectory of a missile in a 2D game?

The trajectory of a missile in a 2D game can be calculated using basic physics equations, such as the projectile motion equation. This equation takes into account the initial velocity, angle of launch, and acceleration due to gravity to determine the path of the missile.

2. What factors should be considered when designing a missile movement system in a 2D game?

When designing a missile movement system, factors such as the speed and agility of the missile, the environment in which it will be moving, and the desired level of realism should be considered. Additionally, the game's physics engine and the capabilities of the game engine should also be taken into account.

3. How can you ensure that the missile movement in a 2D game is smooth and realistic?

To ensure smooth and realistic missile movement in a 2D game, it is important to accurately calculate the trajectory using physics equations and to consider factors such as gravity and air resistance. Additionally, adjusting the speed and acceleration of the missile can also help to create a more realistic movement.

4. Can the missile movement in a 2D game be controlled by the player?

Yes, the missile movement in a 2D game can be controlled by the player through various means, such as using a mouse or keyboard input. The game developer can also implement different control options, such as adjusting the angle and speed of the missile, to give the player more control over its movement.

5. Are there any limitations to consider when implementing missile movement in a 2D game?

There may be limitations to consider when implementing missile movement in a 2D game depending on the capabilities of the game engine and the desired level of realism. For example, the missile's movement may be limited by the game's physics engine or the overall performance of the game. Additionally, the game developer may need to balance the realism of the missile's movement with the overall gameplay experience.

Similar threads

  • Classical Physics
Replies
4
Views
1K
Replies
17
Views
1K
  • Introductory Physics Homework Help
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Introductory Physics Homework Help
Replies
2
Views
646
  • Introductory Physics Homework Help
Replies
2
Views
1K
  • Introductory Physics Homework Help
Replies
2
Views
2K
  • Introductory Physics Homework Help
Replies
4
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • Introductory Physics Homework Help
Replies
14
Views
2K
Back
Top