围绕点的2D矢量旋转(小地图)

时间:2018-07-02 14:46:05

标签: c# math

我正在尝试做的事情:

  1. 使用当前玩家的位置和 敌人的位置。
  2. 2D小地图上的“向上”方向应始终 显示游戏中玩家面前的内容,然后显示右侧 方向应显示播放器右侧的内容,对于 向下和向左。
  3. 玩家应始终位于小地图的中央。

我已经做过的事情:

根据玩家在游戏中的x和y坐标在小地图上渲染其玩家,并根据他们在x和y坐标上在其上渲染敌人。当我在游戏中四处走动时,小地图中的敌人相对于玩家的移动而移动。

我尝试过的方法(但是没有用):

float radarX = 200;
float radarY = 200;
float zoom = 2;

// Function
float xOffset = radarX - LocalPlayer.Position.x;
float yOffset = radarY - LocalPlayer.Position.y;

draw(zoom *(LocalPlayer.Position.x + xOffset),
zoom * (LocalPlayer.Position.y + yOffset);

foreach(Player p in Game.OtherPlayers) // list of enemies
{
Vector2 rotatedLocation = VectorExt.Rotate(new Vector2(p.Position.x, p.Position.y), -LocalPlayer.Yaw - 90); // negate and -90 to convert to normal coordinate system (0 @ RHS, 90 @ Top, 180 @ LHS, 270 @ Bottom)

float tempX = zoom * (rotatedLocation.x + xOffset);
float tempY = zoom * (rotatedLocation.y + yOffset);

draw(myPen, zoom * (LocalPlayer.Position.x + xOffset), zoom * (LocalPlayer.Position.y + yOffset);
}
// End of function

// VectorExt.Rotate
var ca = Math.Cos(radians);
var sa = Math.Sin(radians);
return new Vector2(Convert.ToSingle(ca * v.x - sa * v.y), Convert.ToSingle(sa * v.x + ca * v.y));
// End of VectorExt.Rotate

谢谢。

1 个答案:

答案 0 :(得分:1)

  

在游戏中旋转玩家时,敌人旋转,但是它们似乎绕着0,0轴旋转,而不是玩家。

是的,这就是您的轮换代码所做的。要绕另一个点旋转,必须首先减去该旋转中心的坐标,然后进行旋转,然后再次添加该旋转中心的坐标。

另请参阅this other C++ question

相关问题