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));





Reply With Quote