コード例 #1
0
ファイル: CollisionMaster.cs プロジェクト: ancientgods/SoG
 private Vector2 ResolveMovementCollision(BoxCollider col1, BoxCollider col2)
 {
     int iRotatedCol = 0;
     Vector2 v2TopLeft = new Vector2(col1.Left, col1.Top);
     Vector2 v2TopRight = new Vector2(col1.Right, col1.Top);
     Vector2 v2BottomLeft = new Vector2(col1.Left, col1.Bottom);
     Vector2 v2BottomRight = new Vector2(col1.Right, col1.Bottom);
     if (col1.fRotation != 0f)
     {
         iRotatedCol = 1;
         v2TopLeft = new Vector2(col2.Left, col2.Top);
         v2TopRight = new Vector2(col2.Right, col2.Top);
         v2BottomLeft = new Vector2(col2.Left, col2.Bottom);
         v2BottomRight = new Vector2(col2.Right, col2.Bottom);
         v2TopLeft = Utility.RotateVector2AroundPoint(col1.WorldPosition, v2TopLeft, -col1.fRotation);
         v2TopRight = Utility.RotateVector2AroundPoint(col1.WorldPosition, v2TopRight, -col1.fRotation);
         v2BottomLeft = Utility.RotateVector2AroundPoint(col1.WorldPosition, v2BottomLeft, -col1.fRotation);
         v2BottomRight = Utility.RotateVector2AroundPoint(col1.WorldPosition, v2BottomRight, -col1.fRotation);
     }
     else if (col2.fRotation != 0f)
     {
         iRotatedCol = 2;
         v2TopLeft = Utility.RotateVector2AroundPoint(col2.WorldPosition, v2TopLeft, -col2.fRotation);
         v2TopRight = Utility.RotateVector2AroundPoint(col2.WorldPosition, v2TopRight, -col2.fRotation);
         v2BottomLeft = Utility.RotateVector2AroundPoint(col2.WorldPosition, v2BottomLeft, -col2.fRotation);
         v2BottomRight = Utility.RotateVector2AroundPoint(col2.WorldPosition, v2BottomRight, -col2.fRotation);
     }
     Vector2 v2ToEdge = Vector2.Zero;
     if (iRotatedCol == 0)
     {
         Rectangle rec = new Rectangle((int)col1.Left, (int)col1.Top, col1.iWidth, col1.iHeight);
         Rectangle rec2 = new Rectangle((int)col2.Left, (int)col2.Top, col2.iWidth, col2.iHeight);
         byte byBestDir = 0;
         int iDistanceUp = Math.Abs(rec.Top - rec2.Bottom);
         int iDistanceRight = Math.Abs(rec.Right - rec2.Left);
         int iDistanceDown = Math.Abs(rec.Bottom - rec2.Top);
         int iDistanceLeft = Math.Abs(rec.Left - rec2.Right);
         int iLowest = iDistanceUp;
         if (iLowest > iDistanceRight)
         {
             byBestDir = 1;
             iLowest = iDistanceRight;
         }
         if (iLowest > iDistanceDown)
         {
             byBestDir = 2;
             iLowest = iDistanceDown;
         }
         if (iLowest > iDistanceLeft)
         {
             byBestDir = 3;
             iLowest = iDistanceLeft;
         }
         v2ToEdge = -Utility.AnimationDirectionToVector2((int)byBestDir) * (float)iLowest;
     }
     else if (iRotatedCol != 1)
     {
         if (col2.IsPointInside(v2TopLeft))
         {
             v2ToEdge = col2.GetDistanceToClosestEdge(v2TopLeft);
         }
         else if (col2.IsPointInside(v2TopRight))
         {
             v2ToEdge = col2.GetDistanceToClosestEdge(v2TopRight);
         }
         else if (col2.IsPointInside(v2BottomLeft))
         {
             v2ToEdge = col2.GetDistanceToClosestEdge(v2BottomLeft);
         }
         else if (col2.IsPointInside(v2BottomRight))
         {
             v2ToEdge = col2.GetDistanceToClosestEdge(v2BottomRight);
         }
     }
     return v2ToEdge;
 }