2013-10-27 80 views
0

我創建了一個使用away3d和離開物體的3D遊戲。Lerping 3D旋轉(away3d)

我創建了一個旋轉公式,它將使我的模型以「平滑因子」旋轉。

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; 
    } 
} 
+1

你也應該看看球面線性插值處理:球面線性插值()](HTTP://的Away3D .com/livedocs/away3d/4.0/away3d/core/math/Quaternion.html#slerp()) –

+0

謝謝,永遠不會注意到其中之一。我會看看以後:) – Duckdoom5

回答

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; 
    } 
} 
0

,您應該使用modulus operator代替if

curRotation = curRotation % 360; 
+0

對不起,這沒有奏效。網格會像瘋狂一樣旋轉。 – Duckdoom5

+0

我已經添加了,因爲curRotation會由於某種原因返回一個奇怪的值。 – Duckdoom5

+0

我已經更改了代碼,因此不再使用。它仍然有這種相同的行爲 – Duckdoom5