I've been sitting here for hours wasting time and watching my IQ pack up its bags to leave...
My last resolve is you guys before something gets thrown
I just quickly registered and did a quick search first of all before posting, if I have missed a similar post my apology. On to the problem.
What I want to do is have say a turret fire a bullet at a constant velocity towards a moving object which say is moving around the screen. Now if the turret fires a bullet it will likely miss due to the fact that the moving object has moved from the position it was aiming at.
What I would want the turret to do is take the objects vector and position and use that to calculate the angle that it should fire at inorder to hit the object.
I have attempted this but as usual my math skills have failed me. I have no idea now if this is the wright way to be going about the code, so if you could help please I would very much appreciate it.
Code:
// Calculate the distance to the moving object 'mover'.
var dx = mover._x - 275;
var dy = mover._y - 200;
var dist = Math.sqrt(dx*dx+dy*dy);
// The time it may take to reach the 'mover'.
var timeToTarget = (dist/shotSpeed);
// Predicted position of the 'mover'.
var cx = mover._x + (mover.vector.x*timeToTarget);
var cy = mover._y + (mover.vector.y*timeToTarget);
// Predicted angle.
var predictAngle = Math.atan2((cy-200),(cx-275));
The program is not anything different from what you have drawn.
I've added a green circle to the image in the attachment and its direction to the predicted point. What I am trying to do is have the green circle reach the predicted point that the blue circle will be at.
Doing that I would work out how much more of an angle would it take to hit the blue circle if the green circle was moving at a constant velocity.
Does that make it any clearer? I can upload a quick swf if that will help?
I'm not really sure if I know how to help you, but since there's no use in sleeping right now for me anymore, I thought I'd give it a shot...here's what I came up with:
I could be wrong though since I'm strung out on nicotine and coffee :P
If you're looking for the angle between the vertical line going through the projectile (which I forgot to put in there) you're firing and alpha, then it's just 90degrees - alpha
Last edited by Articulate-Dee; 07-04-2008 at 07:49 AM.
There's lots of different approaches in there, I think the one by "rje" was what the OP eventually settled on.
The approach I supplied in there, post #20, solves a tougher problem than what you need to solve but a lot of the discussion around it is relevant. It solves the "target moves w/ constant acceleration" case, but it appears you need to solve the "target moves w/ constant velocity, acceleration==0" case.
Yours is a simpler problem - you only need to solve a quadratic equation in t.
Take the quartic posted there, and set ax=ay=0. This: