开发者

as3 rotational drag / acceleration

Sprite.rotation+=10; Sprite.rotation*=0.97;

because in as3 the system goes from 180 to -180 I don't know how to apply a drag to a constantly rotating object if it moves either direction. Do I have to convert to radians somehow and then do something? I am开发者_如何学Python pretty bad with math.


I'm not sure "drag" makes sense with the code you've posted. What you've shown would slowly wind the object back to 0 rotation.

If you want a drag/acceleration effect, create a separate variable with your acceleration factor, which you apply every frame. Then, you can apply a factor to that variable to slow rotation down/speed it up.

Something like:

private var _rotationAcceleration:Number = 0;
private var _dragFactor:Number = 0.97;
private var _clip:Sprite;

private function startSpin():void {
    _rotationAcceleration = 10.0;
}
private function enterFrameListener(event:Event):void {
    _clip.rotation += _rotationAcceleration;
    _rotationAcceleration *= _dragFactor;
}


I think you're looking for this:

private function updateRotation():void
{
    var _dx:Number = _player.x - stage.mouseX; // rotate _player mc to mouse
    var _dx:Number = _player.y - stage.mouseY; // rotate _player mc to mouse

    // which way to rotate
    var rotateTo:Number = getDegrees(getRadians(_dx, _dy)); 

    // keep rotation positive, between 0 and 360 degrees
    if (rotateTo > _player.rotation + 180) rotateTo -= 360;
    if (rotateTo < _player.rotation - 180) rotateTo += 360;

    // ease rotation
    var _trueRotation:Number = (rotateTo - _player.rotation) / 5; // rotation speed 5

    // update rotation
    _player.rotation += _trueRotation;          
}

public function getRadians(delta_x:Number, delta_y:Number):Number
{
    var r:Number = Math.atan2(delta_y, delta_x);

    if (delta_y < 0)
    {
        r += (2 * Math.PI);
    }
    return r;
}

public function getDegrees(radians:Number):Number
{
    return Math.floor(radians/(Math.PI/180));
}


It actually does goes from 180 to -180 (contrary to what Reuben says), but higher/lower values get automatically corrected to that range (i.e. 181 is converted to -179)... one way to work with this is to use an auxiliary variable for your math (animation or whatever) and then assign it to the rotation, say:

myVar+=10;
myVar*=.97;
clip.rotation=myVar;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜