Rotation Problems

6 posts

Flag Post

How can I rotate at a fixed speed, towards another object? I know how to ease it or snap it to another object, but I can’t for the life of me figure out how to rotate towards another object at say, 3 degrees a frame. Any ideas?

 
Flag Post

There are many questions like this at stackoverflow, try searching there. In short, get shortest angle between your object’s facing and target, direction of rotation, then if the angle is bigger than your threshold, turn by threshold, else face.

 
Flag Post

Can you give an example of what you want, because I’m failing to understand.

 
Flag Post

I don’t see how rotating something will move it..

 
Flag Post
function onEnterFrame(e:Event):void {
    enemy.rotateTowards(player);
    ... // anything else
}
...
// class Enemy
function rotateTowards(target:DisplayObject):void {
    var tp:Point=target.localToGlobal(new Point(target.x,target.y));
    var p:Point=this.localToGlobal(new Point(x,y)); // or ignore this if target.parent===this.parent
    var angleToTarget:Number=Math.atan2(tp.y-p.y,tp.x-p.x)*180/Math.PI;
    // rotation is rounded to -180..180 as well as angleToTarget
    var delta:Number=angleToTarget-rotation;
    if (delta<-180) delta+=360;
    if (delta>180) delta-=360;
    var threshold:Number=8; // angle in degrees
    if (delta<0) threshold=-threshold;
    if (abs(delta)>abs(threshold)) rotation+=threshold; else rotation+=delta;
}
Originally posted by JohnnyBohnny:

I don’t see how rotating something will move it..

He’s speaking about rotation speed, which can only be per-frame.

 
Flag Post

Oh, ok. So it is just rotating to face another object, but it is limited to a maximum speed. Got it.