Archived

This topic is now archived and is closed to further replies.

megoblks

Shot leading, aiming (ala skeet) and vectors

Recommended Posts

megoblks    122
Ok here''s the problem: I''ve got the shooter at 0,0 and lets say his shot''s velocity is known, say V1 The shooter is aiming at the target which is currently at x1,y1 heading towards a known point at a known velocity, V2 I need a way that the computer will figure out the exact lead (aiming point on the graph) so that when the shooter fires its projectile at x2,y2 it will collide with the target (just like skeet shooting). It had been suggested to use velocity vectors to solve this, but I didnt quite catch it and my math is rather rusty so I''m hoping someone can post the solution before I dig through a million and 1 items coming up with one. Thnx!

Share this post


Link to post
Share on other sites
megoblks    122
Thanks for the link, but I''m really more interested in the pinpoint solution for the target, not just getting close and a loop that just goes estimating and refining isn''t gonna cut it for me. Thanks anyway

Share this post


Link to post
Share on other sites
minorlogic    150

      
//----------------------------------------------------------------------------

//----------------------------------------------------------------------------

//---------------- Just shout to target with const speed

//----------------------------------------------------------------------------

//----------------------------------------------------------------------------

/*
TargetPos, TargetVel,
SourcePos, SourceVel,
EndPos, EndVel

RelTargetPos = TargetPos - SourcePos;
RelTargetVel = TargetVel - SourceVel;

RelEndPos = RelTargetPos + RelTargetVel*DT = RelSourceVel*DT;
RelTargetPos = (RelSourceVel - RelTargetVel)*DT;

RelTargetPos.x = (RelSourceVel.x - RelTargetVel.x)*DT;
RelTargetPos.y = (RelSourceVel.y - RelTargetVel.y)*DT;
RelTargetPos.z = (RelSourceVel.z - RelTargetVel.z)*DT;

RelSourceVel.x*RelSourceVel.x +
RelSourceVel.y*RelSourceVel.y +
RelSourceVel.z*RelSourceVel.z = AbsSourceVel;

TPX = (SV_X - TVX)*D_T;
TPY = (SV_Y - TVY)*D_T;
TPZ = (SV_Z - TVZ)*D_T;

SV_X*SV_X + SV_Y*SV_Y + SV_Z*SV_Z = ASV;

//-----------------
TPX/D_T + TVX = SV_X ;
TPY/D_T + TVY = SV_Y;
TPZ/D_T + TVZ = SV_Z;

1/D_T = t;

(TPX*t + TVX)*(TPX*t + TVX) +
(TPY*t + TVY)*(TPY*t + TVY) +
(TPZ*t + TVZ)*(TPZ*t + TVZ) = ASV;
//-----------------------

TPX*t*TPX*t + 2*TPX*t*TVX + TVX*TVX +
TPY*t*TPY*t + 2*TPY*t*TVY + TVY*TVY +
TPZ*t*TPZ*t + 2*TPZ*t*TVZ + TVZ*TVZ = ASV

t*t*(TPX*TPX + TPY*TPY + TPZ*TPZ)+
t*( 2*TPX*TVX + 2*TPY*TVY + 2*TPZ*TVZ)+
( TVX*TVX + TVY*TVY + TVZ*TVZ - ASV) = 0;
//-----------------------
t*t*(TPX*TPX + TPY*TPY + TPZ*TPZ)+
t*( 2*TPX*TVX + 2*TPY*TVY + 2*TPZ*TVZ)+
( TVX*TVX + TVY*TVY + TVZ*TVZ - ASV) = 0;

A = TPX*TPX + TPY*TPY + TPZ*TPZ;
B = 2*TPX*TVX + 2*TPY*TVY + 2*TPZ*TVZ
C = TVX*TVX + TVY*TVY + TVZ*TVZ - ASV

D = b*b - 4ac; //if D<0, can't get it
X1 = ( -b - sqrt( D ) )/ ( 2 * a );
X2 = ( -b + sqrt( D ) )/ ( 2 * a );
*/



bool SolveDeflectionCollision( const vector3& TargetPos, const vector3& TargetVel, vector3& ResultVel, float& ResultCollisionTime, float VelocityModule ){

XX_STACK_GUARD;
vector3 TP( TargetPos );
vector3 TV( TargetVel );

float AV_2 = VelocityModule * VelocityModule;

float A = TP.x*TP.x + TP.y*TP.y + TP.z*TP.z;
float B = (TP.x*TV.x + TP.y*TV.y + TP.z*TV.z)*2.0f;
float C = TV.x*TV.x + TV.y*TV.y + TV.z*TV.z - AV_2;

float Dis = B*B - 4.0f*A*C;
if( Dis < 0 ) return false;
float SQRT_D = (float)sqrt( Dis ) ;

float A_2 = A + A;

float MinDT = - B - SQRT_D;

if( MinDT == 0 ){ MinDT = -1;
}else{ MinDT = A_2/MinDT; }

float MaxDT = - B + SQRT_D;
if( MaxDT == 0 ){ MaxDT = -1;
}else{ MaxDT = A_2/MaxDT; }

//float MinDT = A_2 /( - B - SQRT_D ); // divide null problem

//float MaxDT = A_2 /( - B + SQRT_D ); // divide null problem


if( MinDT > MaxDT){
float T = MaxDT; MaxDT = MinDT; MinDT = T;//swap

}

float Time;

if( MinDT > 0 ){
Time = MinDT;
}else{
if( MaxDT > 0 ) Time = MaxDT;
else return false;
}

ResultVel = vector3( TV + (TP*(1.0f/Time) ) );
ResultCollisionTime = Time;

return true;
}
//----------------------------------------------------------------------------




thanks to Krunk


[edited by - minorlogic on January 29, 2003 8:10:48 AM]

Share this post


Link to post
Share on other sites
Krunk    122
I don''t know if the OP has seen your post yet, but it''s exactly what he was looking for, so I''m sure it will help.

Just a sugggestion, it might help to edit the post and put [ source ] [ /source ] tags (without the spaces) around the code to recover the indentation. Also, the comment block at the top makes it harder to see what''s going on (without source highlighting anyway). The source is easier to read, probably because it works with vectors rather than individual components.

Share this post


Link to post
Share on other sites
TerranFury    142
I''ll quote one of my previous posts from a while ago that I saved on my hard drive. I worked out a whole bunch of really messy simultaneous parametric equations, and then found patterns in them to condense them into more general operations like dot products. Here''s what I came up with (It''s probably equivalent to minorlogic''s solution):
quote:

OK, I redid the math, and am posting my results. This may be equivalent to what I originally posted, but I don''t feel like checking.

Here it is (A = target; B = bullet; k = bullet speed). Just solve for each variable sequentially and plug it into the next equation.

D = Ainitpos-Binitpos
E = D * D
F = 2(Avelocity * D)
G = k2 - (Avelocity * Avelocity)
t = (F + sqrt(F2 + 4GE))/(2G)
Bvelocity = D/t + Avelocity

DONE!

Share this post


Link to post
Share on other sites