Lerping 3d旋转(away3d)

时间:2013-10-27 15:26:45

标签: actionscript-3 3d rotation away3d

我正在使用away3d和awayphysics创建一个3D游戏。

我已经创建了一个旋转公式,它将使用“平滑因子”旋转我的模型。

private var chRotation:Number = 0;

public override function update(delta:uint):void
{
    if(target){
        var smooth:Number = 0.95;
        var tp:Vector3D  = target.ghostObject.position;
        var cp:Vector3D  = entity.ghostObject.position;
        var targetAngle:Number = -((180 / Math.PI) * Math.atan2(cp.z - tp.z, cp.x - tp.x));

        if(oldTgRotation - targetAngle != 0){
            if((oldTgRotation - targetAngle) > 300){
                chRotation = -180;
            }else if((oldTgRotation - targetAngle) < -300){
                chRotation = 180;
            }
        }

        chRotation += (targetAngle + (chRotation - targetAngle) * (smooth - (delta / 800))) - chRotation;

        entity.ghostObject.rotation = new Vector3D(0, chRotation, 0);
        oldTgRotation = targetAngle;
    }
}

这部分工作,它可以工作,直到网格从-180转到180 cus,然后代码将向后旋转网格,所以:-180 -90 0 90 180

它应该从-180到180前进。但是如何?

编辑:我添加了一种解决方案,但这仍然不完美:

if(oldTgRotation - targetAngle != 0){
    if((oldTgRotation - targetAngle) > 300){
        chRotation = -180;
    }else if((oldTgRotation - targetAngle) < -300){
        chRotation = 180;
    }
}

2 个答案:

答案 0 :(得分:0)

您应该使用modulus operator代替if

curRotation = curRotation % 360;

答案 1 :(得分:0)

好的,为了解决这个问题,我添加了一个布尔开关:

private var chRotation:Number = 0;
private var switchf:Boolean = false;
private var switchb:Boolean = false;

public override function update(delta:uint):void
{
    if(target){
        var smooth:Number = 0.95;
        var tp:Vector3D  = target.ghostObject.position;
        var cp:Vector3D  = entity.ghostObject.position;
        var targetAngle:Number = -((180 / Math.PI) * Math.atan2(cp.z - tp.z, cp.x - tp.x));

        if(oldTgRotation - targetAngle != 0){
            if((oldTgRotation - targetAngle) > 300){
                switchf = true;
            }else if((oldTgRotation - targetAngle) < -300){
                switchb = true;
            }
        }

        if(switchf){
            if(chRotation >= 177){
                switchf = false;
                chRotation = -180;
            }else{
                targetAngle = 190;
            }
        }
        if(switchb){
            if(chRotation <= -177){
                switchb = false;
                chRotation = 180;
            }else{
                targetAngle = -190;
            }
        }

        chRotation += (targetAngle + (chRotation - targetAngle) * (smooth - (delta / 800))) - chRotation;

        entity.ghostObject.rotation = new Vector3D(0, chRotation, 0);
        oldTgRotation = targetAngle;
    }
}