Note that there are some explanatory texts on larger screens.

plurals
  1. POFinding initial speed and angle to hit a known position (parabolic trajectory)
    text
    copied!<p>I am currently doing a small turn based cannon game with XNA 4.0. The game is very simple: the player chooses the speed and angle at which he desires to shoot his rocket in order to hit another player. There is also a randomly generated wind vector that affects the X trajectory of the rocket. I would like to add an AI so that the player could play against the computer in a single player mode.</p> <p>The way I would like to implement the AI is very simple: find the velocity and angle that would make the rocket hit the player directly, and add a random modifier to those fields so that the AI doesn't hit another player each time.</p> <p>This is the code I use in order to update the position and speed of the rocket:</p> <pre><code> Vector2 gravity = new Vector2(0, (float)400); // 400 is the sweet spot value that i have found works best for the gravity Vector2 totalAcceleration = gravity + _terrain.WindDirection; float deltaT = (float)gameTime.ElapsedGameTime.TotalSeconds; // Elapsed time since last update() call foreach (Rocket rocket in _instantiatedRocketList) { rocket.RocketSpeed += Vector2.Multiply(gravity, deltaT); // Only changes the Y component rocket.RocketSpeed += Vector2.Multiply(_terrain.WindDirection, deltaT); // Only changes the X component rocket.RocketPosition += Vector2.Multiply(rocket.RocketSpeed, deltaT) + Vector2.Multiply(totalAcceleration, (float)0.5) * deltaT * deltaT; // We update the angle of the rocket accordingly rocket.RocketAngle = (float)Math.Atan2(rocket.RocketSpeed.X, -rocket.RocketSpeed.Y); rocket.CreateSmokeParticles(3); } </code></pre> <p>I know that the basic equations to find the final X and Y coordinates are:</p> <ul> <li><p>X = V0 * cos(theta) * totalFlightTime</p></li> <li><p>Y = V0 * sin(theta) * totalFlightTime - 0.5 * g * totalFlightTime^2</p></li> </ul> <p>where X and Y are the coordinates of the player I want to hit, V0 the initial speed, theta the angle at witch the rocket is shot, totalFlightTime is, like the name says, the total flight time of the rocket until it reaches (X, Y) and g is the gravity (400 in my game).</p> <h2>Questions:</h2> <p>What I am having problems with, is knowing where to add the wind in those formulas (is it just adding "+ windDirection * totalFlightTime" in the X = equation?), and also what to do with those equations in order to do what I want to do (finding the initial speed and theta angle) since there are 3 variables (V0, theta and totalFlightTime) and only 2 equations?</p> <p>Thanks for your time.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload