2017-02-16 10 views
1

私は行列の数学に精通していないので、どんな助けにも感謝します。だから私は回転して動くシンプルな船スプライトを持っています。私は回転行列とコードを使用してみました外接矩形を計算するために、私は、これらのチュートリアルの降りた:回転行列がスプライトの位置を変更する

http://xbox.create.msdn.com/en-US/education/catalog/tutorial/collision_2d_perpixel

しかし、いつでもスプライトをすることで、大幅な境界矩形変化のXとYの位置だ回転します100ピクセルのように。

public static Rectangle CalculateBoundingRectangle(Rectangle rectangle, Matrix transform) 
    { 
     // Get all four corners in local space 
     Vector2 leftTop = new Vector2(rectangle.Left, rectangle.Top); 
     Vector2 rightTop = new Vector2(rectangle.Right, rectangle.Top); 
     Vector2 leftBottom = new Vector2(rectangle.Left, rectangle.Bottom); 
     Vector2 rightBottom = new Vector2(rectangle.Right, rectangle.Bottom); 

     // Transform all four corners into work space 
     Vector2.Transform(ref leftTop, ref transform, out leftTop); 
     Vector2.Transform(ref rightTop, ref transform, out rightTop); 
     Vector2.Transform(ref leftBottom, ref transform, out leftBottom); 
     Vector2.Transform(ref rightBottom, ref transform, out rightBottom); 

     // Find the minimum and maximum extents of the rectangle in world space 
     Vector2 min = Vector2.Min(Vector2.Min(leftTop, rightTop), 
            Vector2.Min(leftBottom, rightBottom)); 
     Vector2 max = Vector2.Max(Vector2.Max(leftTop, rightTop), 
            Vector2.Max(leftBottom, rightBottom)); 

     // Return as a rectangle 
     return new Rectangle((int)min.X, (int)min.Y, 
          (int)(max.X - min.X), (int)(max.Y - min.Y)); 
    } 

これはMicrosoftのサイトとすべてから来ているので、私はこれがうまくいくと仮定しています。だから問題は私の行列と思う。私は、回転行列だけを使用した場合、回転行列だけを回転させると考えました。 X座標とY座標を大幅に変更しないでください。

// calculate transformation 
      Matrix transformation = Matrix.CreateRotationZ((float)rotation) * Matrix.CreateRotationZ((float)rotation);; 

      //update bounding rectangle 
      rectangle = new Rectangle((int)(position.X - origin.X), (int)(position.Y - origin.Y), texture.Width, texture.Height); 
      rectangle = BoundingAndCollision.CalculateBoundingRectangle(rectangle, transformation); 

は、私は元の行列とそれらの異なる順序なしを含む他の様々なものを試してみました:今これは私が持っているものです。私はまた、チュートリアルでは、何のために働くだろう、一般的なものだった言ってみましたが、それは基本的に同じ結果が得られた:それは私はちょうど私を聞かせて、スクリーンショットを投稿することができます十分に明確でない場合は

Matrix transformation = Matrix.CreateTranslation(new Vector3(origin, 0.0f)) * 
       Matrix.CreateRotationZ((float)rotation) * 
       Matrix.CreateScale(scale) * 
       Matrix.CreateTranslation(position.X, position.Y, 0); 

をknow.Thankあなたは事前に助けを求めています!

答えて

1

回転行列は0,0の周りを回転すると、あなたの長方形がすでに世界に配置されます。各頂点から矩形の中心を引く(0,0を中心とする矩形を変換)した後、(元の場所に再び場所)を回転した後に再度追加最初解決するために

。コードYでは、Yが上から下に行くと仮定します。

public static Rectangle CalculateBoundingRectangle(Rectangle rectangle, Matrix transform) 
{ 

    Vector2 center = new Vector2(rectangle.Left + (rectangle.Width/2), rectangle.Top + (rectangle.Height/2); 

    // Get all four corners in local space 
    Vector2 leftTop = new Vector2(rectangle.Left, rectangle.Top) - center; 
    Vector2 rightTop = new Vector2(rectangle.Right, rectangle.Top) - center; 
    Vector2 leftBottom = new Vector2(rectangle.Left, rectangle.Bottom) - center; 
    Vector2 rightBottom = new Vector2(rectangle.Right, rectangle.Bottom) - center; 

    // Transform all four corners into work space 
    Vector2.Transform(ref leftTop, ref transform, out leftTop); 
    Vector2.Transform(ref rightTop, ref transform, out rightTop); 
    Vector2.Transform(ref leftBottom, ref transform, out leftBottom); 
    Vector2.Transform(ref rightBottom, ref transform, out rightBottom); 

    leftTop += center; 
    rightTop += center; 
    leftBottom += center; 
    rightBottom += center; 

    // Find the minimum and maximum extents of the rectangle in world space 
    Vector2 min = Vector2.Min(Vector2.Min(leftTop, rightTop), 
           Vector2.Min(leftBottom, rightBottom)); 
    Vector2 max = Vector2.Max(Vector2.Max(leftTop, rightTop), 
           Vector2.Max(leftBottom, rightBottom)); 

    // Return as a rectangle 
    return new Rectangle((int)min.X, (int)min.Y, 
         (int)(max.X - min.X), (int)(max.Y - min.Y)); 
} 
+0

完全に機能しました!どうもありがとうございます! –

関連する問題