Hey,

I have this problem I'm trying to solve with rays/vectors with respect to aiming. I have two Spatial objects with Vector3f's for their positions, two Vector3f objects for their respective motion directions, and two float values for their respective motion velocities. I also have a float value for the velocity of the weapon fired by the first Spatial. I need to work out a direction that object A should turn to face so that the weapon fired will hit object B's expected position.

I thought I could do this with simple vector arithmetic, but I can't quite work out how - is there an obvious solution I'm missing, or is there some trick to it? I'd be grateful for any help given.

-Joe

this should do it:

` private static final Vector3f ms_lastIterationTargetPos = new Vector3f();`

private static final Vector3f ms_TargetFuturePos = new Vector3f();

public static Vector3f getPredictedPosition(

final Vector3f p_target,

final MovementRule p_targetMovementRule,

final MovementRule p_projectileMovementRule,

int p_maxIterations,

final double p_toleranceDst) {

ms_lastIterationTargetPos.set(p_target);

double l_dst;

do {

p_maxIterations--;

final float l_projectileFlyTime = p_projectileMovementRule.getTimeUntilPointReached(ms_lastIterationTargetPos);

if (l_projectileFlyTime == 0.0F) {

return ms_lastIterationTargetPos;//insta-hit

} else if (l_projectileFlyTime < 0.0F) {

return null;//unreachable

}

ms_TargetFuturePos.set(p_targetMovementRule.getPositionInFuture(l_projectileFlyTime));

l_dst = (double) ms_TargetFuturePos.distance(ms_lastIterationTargetPos);

ms_lastIterationTargetPos.set(ms_TargetFuturePos);

} while (l_dst > p_toleranceDst && p_maxIterations > 0);

return Float.isNaN(ms_TargetFuturePos.lengthSquared()) ? null : ms_TargetFuturePos;

}

public interface MovementRule {

Vector3f getPositionInFuture(float p_relativeTime);

float getTimeUntilPointReached(final Vector3f p_pos);

}

the algorithm does:

1. how long will my projectile take to hit the target's position?

2. where will the target be until then

3. repeat until the predicted position doesn not need to be corrected further

does that mean it actually worked?

Thanks a lot for this! I originally ended up coming up with a fairly ugly and inefficient solution, but yours is much better.