0

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 に回転するまで機能します。コードはメッシュを後方に回転させます。-180 -90 0 90 180

-180 から 180 に進む必要があります。しかし、どのように?

編集:私は一種の解決策を追加しましたが、これはまだ完全ではありません:

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

2 に答える 2

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;
    }
}
于 2013-10-28T11:01:04.607 に答える
0

の代わりにモジュラス演算子を使用する必要がありifます。

curRotation = curRotation % 360;
于 2013-10-28T01:30:23.197 に答える