• 14
• 12
• 9
• 10
• 9

# Rotating an Offset Point

This topic is 2213 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

In my 2d TDS I'm attempting to offset the location where the player's bullets originate from. This position will obviously need to rotate around the player's origin as the player rotates. My math is rusty but I figured the best way to do this would be to treat it as a triangle with one vertex at the player's position and one at the bullet origin's position. I'm trying to rotate it like so:

 float x1 = shot_offset_x; //Move triangle to origin before rotation float y1 = shot_offset_y; float cos = (float)Math.Cos(p.getDirection()); //Based on player direction. float sin = (float)Math.Sin(p.getDirection()); x1 = x1 * cos + y1 * sin; //Rotating x1 and y1 about point 0,0? y1 = y1 * cos - x1 * sin; x1 += p.X; //Move triangle back to player position y1 += p.Y; 

The bullets created are always near the player but never in the correct position. They seem to be rotating around a point near the player but not at his origin. Can anyone show me where I went wrong?

Edit: I seem to be getting closer here. I'm working with the following code now:

 Vector2 point = new Vector2(shot_offset_x,shot_offset_y); point = MathTool.rotatePoint(point,p.getDirection()); point.X += p.X; point.Y += p.Y; 

 public static Vector2 rotatePoint(Vector2 point, float angle) { float cos = (float)Math.Cos(angle); float sin = (float)Math.Sin(angle); point.X = point.X * cos - point.Y * sin; point.Y = point.Y * cos + point.X * sin; return point; } 
This seems to want to work, but instead of the circle one would expect its making a figure eight shape. Is there still something I'm doing wrong?