2012-08-28 4 views
1

스프라이트 주위를 회전하는 스프라이트의 네 모퉁이를 나타내는 4 개의 Vector2 객체가 중심을 중심으로 회전하려고합니다. 그러나 다음 코드에서는 Vector2 객체가 객체 중심에서 회전하는 대신 Client Space에서 0,0 주위로 회전합니다. 행렬 변환을 사용하면 전역 좌표 (0,0) 대신 객체 중심에서 Vector2 객체를 회전시킬 수있는 방법이 있습니까? 단지 spritePosition를 추가하기 전에 먼저 네 모서리를 회전하고 회전 후 spriteposition을 추가하고 확장이되었습니다 그것은 아마도에 훨씬 쉬울 것XNA에서 회전 행렬의 원점 변경

public Vector2[] CheckCollision() 
    { 
     //Get the 4 corners of the sprite locally 
     //We can get all 4 corners from only 2 vectors 
     Vector2 topLeft = new Vector2(position.X - spriteSize.X, position.Y - spriteSize.Y); 

     //Not sure why position is representing the 
     //bottom right instead of the center here.... 
     Vector2 bottomRight = position; 

     Vector2 bottomLeft = new Vector2(topLeft.X, bottomRight.Y); 

     Vector2 topRight = new Vector2(bottomRight.X, topLeft.Y); 

     //Create transformation matrix 
     Matrix transform = Matrix.CreateRotationZ(MathHelper.ToRadians(this.direction)) * 
      Matrix.CreateScale(this.scale); 

     //Transform the vectors 
     topLeft = Vector2.Transform(topLeft, transform); 
     bottomRight = Vector2.Transform(bottomRight, transform); 
     bottomLeft = Vector2.Transform(bottomLeft, transform); 
     topRight = Vector2.Transform(topRight, transform); 

     Vector2[] vectorArray = new Vector2[4]; 

     vectorArray[0] = topLeft; 
     vectorArray[1] = bottomRight; 
     vectorArray[2] = bottomLeft; 
     vectorArray[3] = topRight; 

     return vectorArray; 

    } 
+0

의 4 개 벡터에 spritePosition 'SpriteBatch.Draw()'를 호출 할 때 잘못된 원점을 지정했다고 생각하십시오. –

+0

@ColeCampbell 필자는 'SpriteBatch.Draw()'호출에서 스프라이트의 중심을 원점으로 지정합니다. 이를 통해 SpriteBatch는 Vector2 객체로 수행하려고하는 것처럼 객체 중심에서 스프라이트를 회전 할 수 있습니다. 위치 문제도 해결해야하지만 행렬 문제만큼 심각한 문제는 아니며 나중에 해결할 수 있다고 생각합니다. – Ryan

답변

0

: 여기

지금까지 회전하는 기능입니다 공연.

그냥 spriteSize의 해당 조합으로 네 모서리를하고 그이 추가 완료되면 Vector2.Transform을 할 스프라이트의 위치는 오른쪽 아래 모서리에 해당한다는 사실이 날 리드 vectorArray

관련 문제