0

I want to create a simple missile object, which moves at a set speed and rotates towards a specific target at a given rotation speed. However, I'm having trouble figuring out the math of it. This is my code so far:

private function enterFrame(e:Event):void {

    // Rotate the missile towards the target.
    var targetAngle:Number = getAngle(target.x, target.y, x, y);
    if (targetAngle < 0) {
        targetAngle += 360;
    }
    if (targetAngle - turnSpeed > rotation) {
        rotation += turnSpeed;
    } else if (targetAngle + turnSpeed < rotation) {
        rotation -= turnSpeed;
    } else {
        rotation = targetAngle;
    }   

    // Set the target point to move to based on angle and speed.
    var newX:Number = x + Math.sin(degreesToRadians(rotation)) * speed;
    var newY:Number = y + Math.cos(degreesToRadians(rotation)) * speed;

    // Move to new location
    x = newX;
    y = newY;
}

private function getAngle (x1:Number, y1:Number, x2:Number, y2:Number):Number {
    var dx:Number = x2 - x1;
    var dy:Number = y2 - y1;
    return (Math.atan2(dy,dx) * 180) / Math.PI;
}

private function degreesToRadians(degrees:Number):Number {
    return degrees * Math.PI / 180;
}

I've been trying to debug it using trace and such, but I can't seem to figure out where the problem is, most likely because there are many problems and I can't tell if I've fixed one because the others are masking it. I suspect that the issue(s) lie somewhere in the rotation calculations, since I'm pretty sure that the movement part is working as it should, but I can't say for sure.

At any rate, whatever I do, the missiles always seem to fly off in random directions, sometimes tracking towards straight up, or straight down, or just looping around after nothing in particular.

AgentPaper
  • 109
  • 4
  • http://stackoverflow.com/questions/5972715/homing-missile-in-as3 – Marty Nov 20 '13 at 03:54
  • @MartyWallace: Does that actually do the same thing as what I'm trying to do here? I saw that, but I couldn't figure out how it worked. – AgentPaper Nov 20 '13 at 04:01
  • 1
    Biggest thing I noticed was you're using `sin` and `cos` on the wrong axes. – Marty Nov 20 '13 at 04:25
  • Another thing is that you use clamped rotation without ever checking which side to turn is the closest. So that if your missile has angle for target at 359, it'll turn left even if its current rotation is 2. I've got an answer for this problem ready... here http://stackoverflow.com/questions/14807287/how-can-i-determine-whether-its-faster-to-face-an-object-rotating-clockwise-or/14807491#14807491 To implement this, you need to play a little more with target angle. – Vesper Nov 20 '13 at 05:08
  • Thanks for the help. For what it's worth, I got this working well enough that I was able to realize that this kind of missile behavior was actually worse for the game than a more simplistic model, so I ended up cutting most of it out. (now missiles simply move towards their target at fixed speed) Ah well, at least I learned something! :p – AgentPaper Nov 20 '13 at 07:20

0 Answers0