ntwiles 145 Report post Posted February 21, 2012 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: [CODE] 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; [/CODE] 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: [CODE] Vector2 point = new Vector2(shot_offset_x,shot_offset_y); point = MathTool.rotatePoint(point,p.getDirection()); point.X += p.X; point.Y += p.Y; [/CODE] [CODE] 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; } [/CODE] 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? 0 Share this post Link to post Share on other sites
meeshoo 512 Report post Posted March 1, 2012 What you are doing in the first code example is you are rotating the triangle around its own origin (local coordinate space - "the middle" of the triangle). If you translate it first, then rotate, and then translate back, your triangle's position stays fixed as it rotates around itself to say so. What you have to do is just rotate it straight away around the player's position. So just skip the "move to player position" and "move back" steps and it should work fine if the rotating code is OK, I haven't checked that. 0 Share this post Link to post Share on other sites