Jump to content
  • Advertisement
Sign in to follow this  
PedroHenrique

Angle to hit a moving target (platform game)

This topic is 2590 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi!
My name is Peter (Peter, in English) I'm from Brazil.
My question is as follows:

I am creating a game with gravity.
The player shoots a cannon with a preset speed.

I would like the firing angle is calculated so that the shot hit a moving target (constant speed). The target has constant vertical position (0), the shooter as well. The target can move left and right.

I would like this help. I am grateful right now.

Even more!

Share this post


Link to post
Share on other sites
Advertisement
This is a frequently-discussed problem. If you search on these forums for "target leading" or "leading shots" or "aiming angle," I think you'll find what you need.

Here's one thread on the subject: link . Specifically, the last post in that thread has an answer. It's written using vector notation, and "*" denotes the inner (a.k.a. "dot") product.

(Also, I think there's a button you can click to delete your earlier, duplicate posts yourself.)

Share this post


Link to post
Share on other sites
that topic, the explanation is for a top-down game, but my game is in the style Platform, with gravity. There on the topic, I have not seen anything about the shooting angle.

Share this post


Link to post
Share on other sites
I see. That diagram kind of explains it. My plan would be to compute how long it would take me to shoot at a particular location.

If d is the distance from the gun along the horizontal axis, alpha is the shooting angle and s is the initial speed of the projectile, we have:
t = 2*s*sin(alpha)/g (this is the time it takes gravity to bring the ball down, if its velocity has an initial vertical component of s*sin(alpha))
d = s*cos(alpha)*t (the projectile travels horizontally at a constant speed of s*cos(alpha))

From those two equations and using the fact that cos(alpha)^2+sin(alpha)^2=1, we can compute t as a function of d:

t = (sqrt(2)/g) * sqrt(s^2 +- sqrt(s^4-d^2*g^2))

Now we want to find a future time t such that the target is at a location where it takes time t for the projectile to get there. If the target is initially at P and it's advancing with a velocity of v, we get
d = P + v*t

t = (sqrt(2)/g) * sqrt(s^2 +- sqrt(s^4-(P+v*t)^2*g^2))

If you expand this equation, you'll get a polynomial of degree 4 on t, which I would solve numerically.

Check my math, and ask again if you can't get it to work. Perhaps you should post a very specific case with all the details specified (like the initial projectile velocity, the initial position of the target...), and we can look at what the solution would look like in that case.

EDIT: Actually, you can avoid dealing with all those sqrt, for the most part, but I'll wait for your particular example to show you how.

Share this post


Link to post
Share on other sites
And how do I get the angle?
I put the time as unknown, but want to know the angle (the time is due).

If this is useful:

Projectile:
x_init=0
y_init=0
speed=15
angle=?

Target:
x_init=100
y_init=0 (constant)
hor_speed=5

Thanks in advance




Share this post


Link to post
Share on other sites
I'll take gravity (which you didn't specify) to be 10, and I'll do it with speed=50 instead of 15: I don't think you can hit the target with such a weak cannon. :)

So using the notation of my previous post,
t = 2*s*sin(alpha)/g
d = s*cos(alpha)*t

Let's square everything and make the angle go away (we'll get back to the angle at the very end, but figuring out t is the hard part).
g^2*t^2/(4*s^2) = sin(alpha)^2
d^2/(s^2*t^2) = cos(alpha)^2

Adding the two, we get
(g/2)^2*t^2/s^2 + d^2/(s^2*t^2) = 1
(g/2)^2*t^4 - s^2*t^2 + d^2 = 0

Now we substitute d = p + v*t and we get
(g/2)^2*t^4 - s^2*t^2 + (p+v*t)^2 = 0

This is our equation in t. We can solve it using Newton-Raphson, for instance. We are trying to find a root of
F(t) := (g/2)^2*t^4 - s^2*t^2 + (p+v*t)^2

Its derivative is
F'(t) = 4*(g/2)^2*t^3 - 2*s^2*t + 2*(p+v*t)*v

Start with a guess that ignores gravity and the movement of the target, so we get something in the right order of magnitude.
t[0] = p/s = 100/50 = 2
t[1] = t[0] - F(t[0])/F'(t[0]) = 2 - 2500/(-8100) = 2.30864197530864197530
t[2] = t[1] - F(t[1])/F'(t[1]) = 2.30864197530864197530 - (-172.50515434148863501415)/(-9197.31136664276937606400) = 2.28988593373684760947
t[3] = t[2] - F(t[2])/F'(t[2]) = 2.28982123722532354387
t[4] = t[3] - F(t[3])/F'(t[3]) = 2.28982123645158132597

So we can probably stop here. We can verify that F(t[4]) is very small (-.0000000000000010713).

Now for the angle, you need to go back to
g^2*t^2/(4*s^2) = sin(alpha)^2
sin(alpha) = sqrt(g^2*t^2/(4*s^2)) = .22898212364515813259
alpha = asin(.22898212364515813259) = .23103189518455891966

If you want that in degrees, it's about 13.23715252698403305127.

Share this post


Link to post
Share on other sites
g^2*t^2/(4*s^2) = sin(alpha)^2
d^2/(s^2*t^2) = cos(alpha)^2

Adding the two, we get
(g/2)^2*t^2/s^2 + d^2/(s^2*t^2) = 1[/quote]

There is an error here??

-------------------------
For programming, I think derivatives are not very good to use

Share this post


Link to post
Share on other sites
Hi! My code so far is:

var t, i; //declarate the vars

t=(target_x - cannon_x) / (cannon_hor_speed - target_hor_speed) //define the time
i=instance_create(x,y, obj_bullet) //create a object
i.hspeed=cannon_hor_speed //define the horizontal speed
i.vspeed=-0.5 * gravity * t; //define the vertical speed
i.gravity = gravity; //define the gravity


Note: Based on the horizontal velocity, I calculated the meeting point of the objects. Using these equations:

space = speed1.t
space2 = space0 + speed2.t

space = space2
space0 = distance between objects

speed1.t = dist + speed2.t
t = dist / (speed1-speed2)

This code is working.
Thank you guy. You're very smart and cool.

Resolved.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!