我有四個32×32的矩形用於我的遊戲角色(統稱爲船和每個矩形代表具有自己的碰撞檢測的單元格(船艙)系統)。基於鄰接矩形旋轉後矩形的位置
我將這些單元格以2x2格式保存在一起。沿着X軸和Y軸移動'船'一切都很好。但是,當我旋轉「船」時,各個矩形旋轉並面向正確的方向,但它們不再「在一起」。也就是說,矩形A1的右邊框與矩形A2的左邊框不同步,等等......
如何糾正矩形的位置,使它們在旋轉後仍對齊?這是我的第一場比賽,我不是一個數學家,因此我發現自己在這件事上要求您的專業知識!
非常感謝您的幫助。如果您需要更多信息(即代碼),那麼我會提供!
更新:
我終於得到了這個工作!以下是我的代碼。我希望這可以幫助別人!
//The point we want to rotate around (in this example, I was rotating around another ship)
Vector2 origin = new Vector2(selectedShip.Position.X, selectedShip.Position.Y);
test1.Position = Vector2.Transform(test1.Position - origin, Matrix.CreateRotationZ(testRot)) + origin;
test2.Position = Vector2.Transform(test2.Position - origin, Matrix.CreateRotationZ(testRot)) + origin;
test3.Position = Vector2.Transform(test3.Position - origin, Matrix.CreateRotationZ(testRot)) + origin;
test4.Position = Vector2.Transform(test4.Position - origin, Matrix.CreateRotationZ(testRot)) + origin;
// Alter the rotation of the four ship compartments for drawing
test1.Rotation += testRot;
test2.Rotation += testRot;
test3.Rotation += testRot;
test4.Rotation += testRot;
// Update rectangle positions
test1.Rect = new Rectangle((int)test1.Position.X, (int)test1.Position.Y, ship01Texture.Width, ship01Texture.Height);
test2.Rect = new Rectangle((int)test2.Position.X, (int)test2.Position.Y, ship01Texture.Width, ship01Texture.Height);
test3.Rect = new Rectangle((int)test3.Position.X, (int)test3.Position.Y, ship01Texture.Width, ship01Texture.Height);
test4.Rect = new Rectangle((int)test4.Position.X, (int)test4.Position.Y, ship01Texture.Width, ship01Texture.Height);
theSprite.Draw(ship01Texture, test1.Rect, null, Color.White, test1.Rotation, Vector2.Zero, SpriteEffects.None, 0.0f);
theSprite.Draw(ship01Texture, test2.Rect, null, Color.White, test2.Rotation, Vector2.Zero, SpriteEffects.None, 0.0f);
theSprite.Draw(ship01Texture, test3.Rect, null, Color.White, test3.Rotation, Vector2.Zero, SpriteEffects.None, 0.0f);
theSprite.Draw(ship01Texture, test4.Rect, null, Color.White, test4.Rotation, Vector2.Zero, SpriteEffects.None, 0.0f);
非常感謝大家的幫助!
轉動而轉動是角度的功能(例如),並指出圍繞你轉的。你確定這個點對於你所有的矩形都是一樣的嗎? – wondra
所有矩形的原點應設置爲已經是旋轉點的矩形的中心。試試這個:RotateAboutOrigin(new Vector2(test1.Width/2,test1.Height/2),Vector2.zero,rotation) – pid
謝謝@pid,我試過了,現在我可以圍繞一個點旋轉我的'ship'。然而,這一點並不是「船」的中心,它不在船上。這很可能是我混淆了我的邏輯。我現在所擁有的,我可能稍後會用它來使'船'軌道成爲一個身體(即一顆行星)。我會用更多信息更新我的問題。此外,感謝您更新您的答案 - 這對我很有幫助,我相信這對於那些其他XNA初學者來說是一個很大的幫助! – user3256944