MouseAndKeyboard
MouseAndKeyboard

Reputation: 445

2D vector rotation around a point (minimap)

What I am trying to do:

  1. Render a small minimap with the current player's location and the enemy locations.
  2. The "up" direction on the 2D minimap should always show what is in front of the player in game, then the right direction should show what is on the right of the player, same for down and left.
  3. The player should always be centered on the minimap.

What I have already done:

Rendered the player on the minimap based upon it's in game x and y coordinates, rendered the enemies on the minimap based upon their x and y coordinates. When I move around in game the enemies in the minimap move relative to the player movements.

What I tried (but didn't work):

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

Thanks in advance.

Upvotes: 0

Views: 324

Answers (1)

Wim Coenen
Wim Coenen

Reputation: 66753

When you rotate your player in game the enemies rotate however they appear to be rotating around the 0,0 axis and not the player.

Yes, that's what your rotation code does. To rotate around another point, you must first subtract the coordinates of that rotation center, then do the rotation, then add the coordinates of the rotation center again.

See also this other C++ question.

Upvotes: 1

Related Questions