Maths + lack of sleep = Need help please
I've been sitting here for hours wasting time and watching my IQ pack up its bags to leave... :faded:
My last resolve is you guys before something gets thrown :D
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));
1 Attachment(s)
Thats the right direction
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?