Advanced motion prediction ( 2D ) help

Started by
25 comments, last by noobnerd 12 years, 5 months ago
advanced motion prediction:

Heloo there, I am making a 2D space shooter and have come quite far already. I have the problem now that the enemy AI doesnt aim well enough using my current functions.
I therefore would need a better motion predicting function than the one i am currently using. I thought of a system that tell the ai where to aim if i am moving with constant speed and if i turn with a constant turn rate.

Like this:

[size=2][attachment=6027:show.jpg]


where : the blue line represents the targets current motion vector
the green line is the bullets velocity vector, it can be directed in any direction
the black line at the player is the players current motion vector
and the curving black line represents the targets future positions. ( it will be a circle )


So i have a target at a position a,b with a motion vector c and a constant turn rate of d
i also have the player at a position e,f with a motion vector g and a bullet velocity of h the bullet will have the combined vector of the ship and its own vector


What i want to know is the coordinates where i should aim considering that all the values are constant.
I have been able to do this without the turning but i have not been able to solve this one.

Please help

OH i am programming basic so please do not post C++ ansvears as i will not understand them huh.gif
Advertisement
I would first change the frame of reference to one where the shooter is at the origin and currently has a speed of 0. The basic idea for these problems is always the same: Find a time t in the future (t>0) such that the distance from the target to the origin is t*bullet_speed. Then shoot in the direction of where you expect the target to be at time t.

You can either solve the problem analytically or numerically. In the case of constant turning, I don't think you'll find an analytic solution, but doing it numerically shouldn't be too hard.
Wow thanks for a fast reply!
yes that is what i did in the case without turning, but i cant seem to find a way to get this done numerically. My problem is, that the only way i manage to describe the motion of the target is with a function containin Sin and Cos both with an unkown inside... i dont know how to extract those :(

Could you possibly help me with this?

Wow thanks for a fast reply!
yes that is what i did in the case without turning, but i cant seem to find a way to get this done numerically. My problem is, that the only way i manage to describe the motion of the target is with a function containin Sin and Cos both with an unkown inside... i dont know how to extract those :(

Could you possibly help me with this?


What you are trying to do (extracting the unknown from inside the trigonometric functions) is finding an analytical solution, which is probably impossible. If you write the equation as F(t) = 0, you can start with some guess t[0] (say t[0] = current_distance/bullet_speed) and then apply a few iterations of the Newton-Raphson method,

t[1] = t[0] - F(t[0])/F'(t[0])
t[2] = t[1] - F(t[1])/F'(t[1])
t[3] = t[2] - F(t[2])/F'(t[2])
t[4] = t[3] - F(t[3])/F'(t[3])

where F'(t) means the derivative of F at t.

t[4] will probably be a very good approximation to a solution. If your target's speed is relatively small compared to the bullet's speed (say, less than half), this process will pretty much always work.
Wow, i have just recently done derivata in school :) "newton-raphson method" sounds fancy!

i will definitely give it a try. Thank you
okay so im stuck again mellow.gif

these are the equations i have been able to get

x = (sin(g+h*k)*r+a-e-i*k))/k
y = (cos(g+h*k)*r+b-f-j+k)/k
x[sup]2[/sup]+y[sup]2[/sup] = z[sup]2[/sup]
x[sup]2[/sup]+y[sup]2[/sup]-z[sup]2[/sup]= 0
[sup]
[/sup]
where g is the initial angle of the "radius" that the ship will turn around ( it will fly in a circle if it has constant turnrate )
h is the turnrate in angles/frame
k is the amount of turns or frames
r is the radius of the circle
a,b are the x,y coordinates of the center of the circle
e,f are the x,y coordinates of the player ship
i,j are the x and y components of the ships motion vector
z is the bullet velocity
x,y are the bullets x and y motion vector components

i dont have a clue about how to derivate the last function ( not the x^2+y^2-z^2 = 0 but the one where you insert the sin(g+h*k.... ) stuff ) huh.gif

help?

and thanks for helping me out this far biggrin.gif
[font="Arial"]So which variables are your unknowns? x,y, and z? (Also, your last two equations are equivalent.)[/font]


[font="Arial"]The following answers your question if the unknowns are x,y, and z: The Jacobian (matrix of partial derivatives) of the function[/font]

[font="Arial"]h(x,y,z) = (h1(x,y,z), h2(x,y,z), h3(x,y,z))[/font]

[font="Arial"]where[/font]

h1(x,y,z) = x - (sin(g+h*k)*r+a-e-i*k)/k;

h2(x,y,z) = y - (cos(g+h*k)*r+b-f-j+k)/k;

h3(x,y,z) = x^2+y^2 - z^2;

[font="Arial"]is[/font]

[font="Arial"]Dh(x,y,z) = [/font]

[font="Arial"]
[ 1, 0, 0]
[ 0, 1, 0]
[ 2*x, 2*y, (-2)*z] .[/font]

[font="Arial"]The idea is that you're trying to find x,y, and z such that h(x,y,z)=(0,0,0), using the Newton-Raphson method.

It'd probably be a good exercise to work this out on your own, or to compute additional derivatives if you have additional unknowns. To do this, you'll need,[/font]

[font="Arial"]1.) The Chain Rule (just wiki it)[/font]
[font="Arial"]2.) The Product Rule (ditto)[/font]
[font="Arial"]3.) Some trig. derivatives. Namely, (d/dx)(sin(x)) = cos(x) and (d/dx)(cos(x)) = -sin(x).[/font]
Just try to express the position of the target at time t. Can you do that?
@ Emergent, x and y and k are unknown. I undestand know that it was somewhat misleading to have bullet velocity as z.
I dont really understand how to use the [1,0,0] [0,1,0] [2*x,2*y,(-2)*z] which you have provided. I assume it is a matrix, but i dont know much about them. I get the last line of the ansvear, it is the derivative of the last function?

I will do as you say and wiki theese things and perhaps even learn how to do that blink.gif
Thank you.

@Alvaro

yes i can smile.gif

x = a+sin(g+h*t)*r
y = b+cos(g+h*t)*r

where x,y are the future coordinates
a,b is the center of the "circle"
g is the angle of the original "radius"
h is the turnrate per frame
r is the radius of the "circle"

the radius can be obtained like this:

r = (180*v)/(h*pi)

where v is the velocity of the target
h is the turnrate
pi is pi

a and b can be obtained like this:

a = cos(m+90)*r
b = sin(m+90)*r

where m is the initial angle of the target ( compared to a line pointing upwards on the screen )

and the angle of the "radius" is

g = m-90

@Alvaro

yes i can smile.gif

x = a+sin(g+h*t)*r
y = b+cos(g+h*t)*r


That's great! Now define

F(t) := x^2 + y^2 - bullet_speed^2*t^2 = (a+sin(g+h*t)*r)^2 + (b+cos(g+h*t)*r)^2 - bullet_speed^2*t^2
F'(t) = 2*(a+sin(g+h*t)*r)*cos(g+h*t)*h - 2*(b+cos(g+h*t)*r)*sin(g+h*t)*h - 2*bullet_speed^2*t

I might have made a mistake in taking the derivative, so check it. Then apply what I posted earlier about Newton-Raphson.

[EDIT: Indeed, I had made [at least] one mistake, which noobnerd detected. I had "F(t) := x^2 + y^2 - bullet_speed*t" where it should have said "F(t) := x^2 + y^2 - bullet_speed^2*t^2".]

This topic is closed to new replies.

Advertisement