31
Modding / LeadVector
« on: May 08, 2014, 12:35:08 PM »
Derail Moved from
http://fractalsoftworks.com/forum/index.php?topic=7966.30
LeadVector
this is actualy designed to calculate the intercept of a bullet fired from point Mloc with Mvel speed, at target at Tloc with Tvel velocity
**fixed
for the purposes of this calculation even in cases such as missiles Mvel can be asumed to be the objects maximum speed, as long as object HAS a maximum speed, and IS allways accelerating
IF cases:
http://fractalsoftworks.com/forum/index.php?topic=7966.30
LeadVector
Code: java
//find intercept between a missile and a target
//adapted from code by Jens Seiler
//http://jaran.de/goodbits/2011/07/17/calculating-an-intercept-course-to-a-target-with-constant-direction-and-velocity-in-a-2-dimensional-plane/
//have fun if you want to actually understand what going on
//basicaly gets where T will be by the time M gets to it
public static Vector2f LeadVector(Vector2f TLoc, Vector2f TVel, Vector2f MLoc, Vector2f MVel)
{
//get missiles speed
float MSpeed = (float)Math.sqrt(MVel.lengthSquared());
//separate out the vectors
float Tx = TLoc.getX();
float Ty = TLoc.getY();
float Mx = MLoc.getX();
float My = MLoc.getY();
float TVx = TVel.getX();
float TVy = TVel.getY();
//subtract position vectors
float x1 = Tx - Mx;
float y1 = Ty - My;
//quadratic fun
float h1 = TVx*TVx + TVy*TVy - MSpeed*MSpeed;
if (h1==0)
{
h1= (float) .0001;
}
float minusMHalf = -(x1*TVx + y1*TVy)/h1; // h2/h1
float discriminant = minusMHalf * minusMHalf - (x1*x1 + y1*y1)/h1; // (h2/h1)^2-h3/h1 (ie D)
//can they intersect?
if (discriminant < 0)
{
return new Vector2f(0,0);
}
double root = Math.sqrt(discriminant);
double t1 = minusMHalf + root;
double t2 = minusMHalf - root;
double tMin = Math.min(t1, t2);
double tMax = Math.max(t1, t2);
//which of the 2 is smaller
double time = tMin > 0 ? tMin : tMax;
//can return -ve time (this is less then useful)
if (time < 0)
{
return new Vector2f(0,0);
}
//calculate vector
return new Vector2f((float)(time * TVx), (float)(time * TVy));
}
this is actualy designed to calculate the intercept of a bullet fired from point Mloc with Mvel speed, at target at Tloc with Tvel velocity
**fixed
for the purposes of this calculation even in cases such as missiles Mvel can be asumed to be the objects maximum speed, as long as object HAS a maximum speed, and IS allways accelerating
IF cases:
Spoiler
1) IF Tvel and Mvel are in the same direction: LeadPoint defaults to Tloc (the objects location)
2) IF Mvel is insufficiet to reach T: LeadPoint defaults to Tloc (the objects location)
3) IF Mvel is still accelerating: by a large this is irelevant as it will default to (2)
4) IF Tvel is changing(trying to djink): LeadPoint will produce the shortest intercept point every time (so as long as it is recalculated say 1/sec it will be on target)
5) IF chasing object has no maximum speed(or very high compared to acceleration): it will produce the best lead it can for its curent speed, not ideal, but accounting for this case is largely irrelevant (error will be insignificant, usualy a few extra % time above the "ideal" time to intercept)
2) IF Mvel is insufficiet to reach T: LeadPoint defaults to Tloc (the objects location)
3) IF Mvel is still accelerating: by a large this is irelevant as it will default to (2)
4) IF Tvel is changing(trying to djink): LeadPoint will produce the shortest intercept point every time (so as long as it is recalculated say 1/sec it will be on target)
5) IF chasing object has no maximum speed(or very high compared to acceleration): it will produce the best lead it can for its curent speed, not ideal, but accounting for this case is largely irrelevant (error will be insignificant, usualy a few extra % time above the "ideal" time to intercept)
[close]