Jump to content



Rotating an Offset Point

  • You cannot reply to this topic
1 reply to this topic

#1 Ntwiles   Members   -  Reputation: 101

Like
0Likes
Like

Posted 20 February 2012 - 09:58 PM

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?

Ad:

#2 meeshoo   Members   -  Reputation: 221

Like
0Likes
Like

Posted 01 March 2012 - 11:04 AM

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.






We are working on generating results for this topic
PARTNERS