Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Simple question, difficult answer

Senuka Harbinger
A-Life, one bit at a time
Join date: 24 Oct 2005
Posts: 491
12-10-2006 09:57
"Where do I fire to hit a moving target?"

This is a question that is asked an answered by people everyday without doing any complex math; in the world of sports, its where does the quarterback throw the ball so that his reciever will be able to catch it. in hunting, it's how far you need to lead your game while it's running.


programatically, this has stumped me.

given the simplest scenario: the shooter is stationary, the target moves at a set velocity (in a set direction), and the bullet travels at a constant speed- what angle do I need to fire the bullet at so that it will hit the target?

I believe it involves solving a pair of equations.

doing some simple vector algebra, I am able to determine the time it will take the bullet to travel to it's target based on the initial distance apart, and the directional vector (non-normalized), that the bullet is traveling in. I can then use that to determine the final position vector of the target when the bullet will hit it in terms of the bullet's initial directional vector. This comprises the first equation.

The second equation I do not know what it is, but I know it needs to have the bullet's directional vector isolated. Has anyone tried answering this question before, and/or have the solution to it?
_____________________
My SLExchange shop

Typos are forgiven; desecrating the english language with reckless abandon and necrophilic acts is not.


The function is working perfectly fine. It's just not working the way you wanted it to work.
2k Suisei
Registered User
Join date: 9 Nov 2006
Posts: 2,150
12-10-2006 10:27
something like this?:

TargetPosition=llDetectedPos()+llDetectedVel();


This is what I use for missiles.
Senuka Harbinger
A-Life, one bit at a time
Join date: 24 Oct 2005
Posts: 491
12-10-2006 10:42
From: 2k Suisei
something like this?:

TargetPosition=llDetectedPos()+llDetectedVel();


This is what I use for missiles.


yes, but if your shot takes 10 seconds to get there, that won't be accurate (since a raw velocity is in m/s, said equation would be accurate for only taking 1 second to reach the target). you'd need llDetectedPos()+llDetectedVel()*shot_to_closure_time.

I'm able to find shot_to_closure_time in terms of the vector the bullet is fired at, but I cannot find out the vector to fire the bullet along.
_____________________
My SLExchange shop

Typos are forgiven; desecrating the english language with reckless abandon and necrophilic acts is not.


The function is working perfectly fine. It's just not working the way you wanted it to work.
Tyken Hightower
Automagical
Join date: 15 Feb 2006
Posts: 472
12-10-2006 11:09
You should probably start with a few things and work from there:
  1. Position of target
  2. Velocity of target
  3. Position of bullet (firing location)
  4. Velocity of bullet


That said, I don't use any math to hit moving targets. :o
_____________________
Seifert Surface
Mathematician
Join date: 14 Jun 2005
Posts: 912
12-10-2006 13:27
From: Senuka Harbinger
"Where do I fire to hit a moving target?"

This is a question that is asked an answered by people everyday without doing any complex math; in the world of sports, its where does the quarterback throw the ball so that his reciever will be able to catch it. in hunting, it's how far you need to lead your game while it's running.
Your brain is doing complex math, you're just not aware of it.

This is some code I wrote ages ago, untested in SL. It also assumes no gravity, so the bullets should have buoyancy set to 1.0. I think I might have worked out a version with gravity as well, but I can't find it right now. I think it might involve finding roots of cubic polynomials as well, which is nasty. Anyway, here's the code, not taking into account gravity:
CODE
vector p; // is enemy pos relative to you (so enemypos - yourpos)
vector v; // is enemy vel
float s; // is bullet speed

vector get_aim(float t, vector p, vector v, float s)
{
return (p/t + v)/s;
}

vector shoot_dir(vector p, vector v, float s) //output is a unit vector in the direction you should shoot, or <0,0,0> if its impossible to hit
{
float C = p * p;
float B = 2*p * v;
float A = v*v - s*s;

float foo = B*B - 4*A*C;

if(foo<0)
{ //then no solutions - impossible to hit;
return <0,0,0>;
}
else
{
float t1 = ( -B - llSqrt(foo) )/(2*A);

if(t1>=0)
{
return llVecNorm(get_aim(t1, p, v, s));
}
else
{
float t2 = ( -B + llSqrt(foo) )/(2*A);
return llVecNorm(get_aim(t2, p, v, s));
}
}
}
_____________________
-Seifert Surface
2G!tGLf 2nLt9cG
Tyken Hightower
Automagical
Join date: 15 Feb 2006
Posts: 472
12-10-2006 15:20
From: Seifert Surface
Your brain is doing complex math, you're just not aware of it.

This is some code I wrote ages ago, untested in SL. It also assumes no gravity, so the bullets should have buoyancy set to 1.0. I think I might have worked out a version with gravity as well, but I can't find it right now. I think it might involve finding roots of cubic polynomials as well, which is nasty. Anyway, here's the code, not taking into account gravity:


Any chance you could explain this in non-code physics or mathematical terms, just for reference? Brain a little fried from studying for exams. I'm curious to know how to actually solve it now. :(
_____________________
Seifert Surface
Mathematician
Join date: 14 Jun 2005
Posts: 912
12-10-2006 15:32
I don't really remember the details, I worked this out many years ago. It's going to be something along the lines of solving for two linear parametric formulae (the positions of the target and the bullet) being equal at some time T (i.e. when they hit). The t1 and t2 in the code are the two possible times at which one can hit the target, assuming the bullet has a constant fixed speed s. There's a quadratic equation that pops up from somewhere, hence the two solutions.
_____________________
-Seifert Surface
2G!tGLf 2nLt9cG
Tyken Hightower
Automagical
Join date: 15 Feb 2006
Posts: 472
12-10-2006 18:11
From: Seifert Surface
I don't really remember the details, I worked this out many years ago. It's going to be something along the lines of solving for two linear parametric formulae (the positions of the target and the bullet) being equal at some time T (i.e. when they hit). The t1 and t2 in the code are the two possible times at which one can hit the target, assuming the bullet has a constant fixed speed s. There's a quadratic equation that pops up from somewhere, hence the two solutions.

I seem unable to figure this out. :(
_____________________
Soen Eber
Registered User
Join date: 3 Aug 2006
Posts: 428
12-10-2006 21:34
Caveat: Its been 20 years since I've done this, I'm probably missing something.

Took me a few minutes to recall highschool algabra. You're solving for the intersection of two equations on a graph - flip the problem from the perspective of looking at it in a map view on a two dimensional coordinate system, and you see that each path is a x/y equation. So for example if the bullet path is described as

y = 5x

and the target is described as

3y = 2x-1

then you have to (I think) solve for both equations by using various associative and transitive laws to derive a quadratic equation you can solve for:

5x-y = 0
2x-3y+1 = 0
5x-y = 2x-3y+1
(5x-y)(2x-3y+1) = 0
(solve for the quadratic - I forgot how, but that's what Google is for)

Once you have solved for x and y, use trig to compute the angle for that pair of coordinates compared to where you are shooting from. Its not simple, but there's a lot of info on how to do the math at online math sites.

Please don't move while you shoot, I strongly suspect that would kick the answer into Caculus range.
Senuka Harbinger
A-Life, one bit at a time
Join date: 24 Oct 2005
Posts: 491
12-10-2006 21:46
Thanks for the code seifart.

just a question about your variables:


vector v=velocity of target?
vector p=position of target?
float s=speed of bullet?


Soen: I had not thought of looking at each path as an x/y equation, and was instead thinking at it in terms of vector algebra/calculus. Looking things over now, I believe that Seifart's code does exactly that.


some of the equations in Seifart's code match my partial solution to the problem, so I believe that this is all correct. I'll work with it in-world when I have the chance and send out the results here.
_____________________
My SLExchange shop

Typos are forgiven; desecrating the english language with reckless abandon and necrophilic acts is not.


The function is working perfectly fine. It's just not working the way you wanted it to work.
Tyken Hightower
Automagical
Join date: 15 Feb 2006
Posts: 472
12-10-2006 21:50
From: Senuka Harbinger
Thanks for the code seifart.

just a question about your variables:


vector v=velocity of target?
vector p=position of target?
float s=speed of bullet?


Soen: I had not thought of looking at each path as an x/y equation, and was instead thinking at it in terms of vector algebra/calculus. Looking things over now, I believe that Seifart's code does exactly that.


some of the equations in Seifart's code match my partial solution to the problem, so I believe that this is all correct. I'll work with it in-world when I have the chance and send out the results here.

If you have some math, I'd love to share. :)
_____________________
Seifert Surface
Mathematician
Join date: 14 Jun 2005
Posts: 912
12-10-2006 22:15
From: Senuka Harbinger
vector v=velocity of target?

Yes, or target_velocity - my_velocity if this object is also moving and the bullet velocity will be relative to the motion of this object.
From: Senuka Harbinger
vector p=position of target?

As I say in the comments, this is target_position - my_position.
From: Senuka Harbinger
float s=speed of bullet?

Yes.
_____________________
-Seifert Surface
2G!tGLf 2nLt9cG