2011-06-08 16 views
1

進出口試圖對其形狀創建具有球蹺蹺板,其基於所述形狀角度,球滾動。球滾動軸承在DirectX用C#

這裏是它的屏幕截圖。

enter image description here

所以,基於由跟蹤條值generatated角度的蹺蹺板運動的形狀。

下面是聲明的變量:

private const float ONE_DEGREE = 0.0174532924f; 
     private ID3DMesh tab;   
     private ID3DMesh ball; 

的 '標籤' 是可變的形狀。

此方法將形狀的角度:

public void setShapeAngle(float degree) 
{    
    tabTargetAngle = Util.DegreeToRadian(degree); 
} 

這裏是更新它的方法:

public void Update(int elapsedTime) 
     { 

      if (tab.Pitch != tabTargetAngle) 
      { 
       if (tabTargetAngle > tab.Pitch) 
       { 
        if (tab.Pitch >= (tabTargetAngle - ONE_DEGREE)) 
        { 
         tab.Pitch = tabTargetAngle; 

        } 
        else 
        { 
         tab.Pitch += tabuaSpeed * elapsedTime;       
        } 
       } 
       else if (tabTargetAngle < tab.Pitch) 
       { 
        if (tab.Pitch <= (tabTargetAngle + ONE_DEGREE)) 
        { 
         tab.Pitch = tabTargetAngle; 

        } 
        else 
        { 
         tab.Pitch -= tabuaSpeed * elapsedTime;       

        } 

       } 

      }             
     } 

對象的一切,都是ID3DMesh對象。這是ID3DMesh類的代碼。

public interface ID3DMesh : IDisposable 
    { 
     Color Ambient { get; set; } 
     CollisionTestMethod CollisionDetectionMethod { get; set; } 
     Mesh D3DXMesh { get; } 
     Color Diffuse { get; set; } 
     Color Emissive { get; set; } 
     Material[] Materials { get; set; } 
     ID3DMesh Parent { get; set; } 
     float Pitch { get; set; } 
     Vector3 PivotOffset { get; set; } 
     float PivotOffsetX { get; set; } 
     float PivotOffsetY { get; set; } 
     float PivotOffsetZ { get; set; } 
     Vector3 Position { get; set; } 
     RenderOptions RenderSettings { get; set; } 
     float Roll { get; set; } 
     Vector3 Scale { get; set; } 
     float ScaleX { get; set; } 
     float ScaleY { get; set; } 
     float ScaleZ { get; set; } 
     Color Specular { get; set; } 
     float SpecularSharpness { get; set; } 
     Texture[] Textures { get; set; } 
     Color WireColor { get; set; } 
     float X { get; set; } 
     float Y { get; set; } 
     float Yaw { get; set; } 
     float Z { get; set; } 

     MeshBoundingBox GetBoundingBox(); 
     MeshBoundingSphere GetBoundingSphere(); 
     float GetDepth(); 
     float GetHeight(); 
     float GetWidth(); 
     Matrix GetWorldMatrix(); 
     bool Intersects(ID3DMesh mesh); 
     void Link(ID3DMesh parentMesh, Vector3 linkPosition); 
     void Move(float xAmount, float yAmount, float zAmount); 
     void Render(); 
     void RenderPlanarShadow(Plane groundPlane, Light light, bool allowDoubleBlending); 
     void SetDepth(float depth); 
     void SetDepth(float depth, bool uniformScale); 
     void SetHeight(float height); 
     void SetHeight(float height, bool uniformScale); 
     void SetPlanarShadowOpacity(float shadowOpacity); 
     void SetScale(float amount); 
     void SetScale(float xAmount, float yAmount, float zAmount); 
     void SetSize(float width, float height, float depth); 
     void SetWidth(float width); 
     void SetWidth(float width, bool uniformScale); 
    } 

我試着使用Move(float,float,float)方法。但它並沒有像它應該的那樣移動。如果你能幫助我。

謝謝。

回答

2

(注:下面我會忽略第三個層面,因爲球總是沿着同一平面上移動)

如果我們把蹺蹺板作爲參照系,我想的運動球將類似於harmonic oscillator。也就是說,在給定的時間瞬間沿蹺蹺板的球的位置,S(t)的,將通過下面的公式給出:

S(t)的= L cos(2π噸/ T +φ)

其中L是蹺蹺板的長度(諧波的幅度),T是球從蹺蹺板的一端移動到另一端並返回到開始所花費的時間(諧波的週期)。 φ是諧波的初始相位,用來調整公式,所以s(0)給出了起始位置。如果你想讓它在中心開始,你需要使s(0)= 0,這意味着你需要餘弦爲0.所以你必須使φ爲π/ 2(90度),因爲cos(π/2)= 0.

有了這個,你可以通過改變世界變換把球放在原地。如果將它旋轉到蹺蹺板的當前角度(我們稱之爲θ(t)),則可以將球沿着xx軸平移s(t)的值。

這相當於將(s(t),θ(t))視爲極座標中球的位置。然後,你可以在給定的時間笛卡爾座標(X(t),Y(t))的這些公式:

X(t)= S(t)的COS(θ)

ÿ (t)= s(t)sin(θ)

1

(假設上矢量爲(0,1,0),標籤與X軸對齊)

你能想象的球會必須「滾」下沿的標籤X軸,你將不得不計算的Y位置,讓它堅持選項卡。

你可以使用Move()方法爲X位置,球的速度都有影響它是在一個相對的方式X位置。

通過設置Y屬性,可以更容易地計算每個X位置的Y位置(只要球保留在選項卡上)。

如果我是你,我會通過創建計算Y位置,以使球「貼標籤」的任何X位置的方法開始。

如果沒有你指出正確的方向,請更多地討論「它沒有移動,因爲它應該」有點。