我有四个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