Shot leading, aiming (ala skeet) and vectors

Started by
5 comments, last by megoblks 21 years, 2 months ago
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!
Advertisement
I had to trudge back to page 5, but here's a nifty link!

Your situation is a bit more trivial compared to that of the poster in the link, so your solution might not be as complicated.

[edited by - Zipster on January 28, 2003 2:19:12 AM]
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

      //----------------------------------------------------------------------------//----------------------------------------------------------------------------//----------------  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  = ASVt*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*TVZC = TVX*TVX + TVY*TVY + TVZ*TVZ - ASVD = b*b - 4ac; //if D<0, can't get itX1 = ( -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]
Hi ! All...

Im waiting a comments about the code ?
was it helpfull ?
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.
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!

This topic is closed to new replies.

Advertisement