# Point relative to moving Origin

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

## Recommended Posts

Hey there. I'm currently working on something in c# XNA, and I have a problem I've been stuck on for ages. I have an object which can rotate 360 degrees and can move all over the screen (any x,y coordinates), in relation to an origin on the texture (the center of the texture) . The problem I have is that I need another few points which are 'stuck' to the texture, and will move only in relation to my main object. So if the texture rotates 90 degrees to the left, the other points will all rotate around the texture's origin to stay in the same relative place. I can explain better with a picture (attached). Any help here would be appreciated.

##### Share on other sites
Hidden
My guess is that given the origin of sprite V0=(x0, y0) and fixed point V1=(x1, y1) that we want to rotate, we can calculate vector V=(x, y)=V1-V0=(x1-x0, y1-y0) that represents vector between position vectors V0 and V1.

From V we get angle=atan2(y, x). There should be a function named like this or similar in C#. From angle we can calculate new coordinates for whatever rotation angle:
new x=cos(angle)*magnitude of V
new y=sin(angle)*magnitude of V

Note: magnitude of V is sqrt(x*x+y*y).

I hope I got it right. If I didn't, remember that beer is not good for you. Cheers!

Its easier than you think. When you rotate the triangle you actually rotate three points around the origin. Its the same with your "Fixed Point", just rotate it around the origin. Google for transformations, to be exact for 2d rotation. Its a simple matrix multiplication with your "Fixed Point".

the_visualist

##### Share on other sites
I can't seem to get my head round this. If I have a vector with the coordinates of the origin of triangle, a vector with the coordinates for the fixed point which are, say, (triangleOrigin.X + 20, triangleOrigin.Y + 20), and the angle of the triangle, what am I applying the matrix rotation to?

##### Share on other sites
you would need to apply it to the vector pointing to the point, so it is now pointing to the new spot.

##### Share on other sites
Right so now I've got:

point1 = Vector2.Transform(point1, Matrix.CreateRotationZ(angle)); point1 += origin; 

Where point1 is the fixed point, and the origin is the well, origin. But this doesn't appear to do anything.

##### Share on other sites

Right so now I've got:

point1 = Vector2.Transform(point1, Matrix.CreateRotationZ(angle)); point1 += origin; 

Where point1 is the fixed point, and the origin is the well, origin. But this doesn't appear to do anything.

What you need to do is rotate something about the triangles origin. The something is your FixedPoint in relation to the triangles Origin, which means:
Vector something = FixedPoint - Origin; (this is called translation by the way)
Rotate(something, angle);

Afterwards you need to translate it back to where it was to get the correct coordinates:
Vector newFixedPoint = something + origin

Here is the matrix that you need for the rotation - to make sure that you are actually using the correct one:

the_visualist

##### Share on other sites
Don't forget to make sure that your angle is stored as the correct type (radians vs degrees). I know I've made many mistakes where I assumed a library uses one, but really uses the other. I believe C#'s Math.cos(double) and Math.sin(double) use radians. If so, you can use MathHelper.ToRadians(double) I believe is the name of the function.

1. 1
2. 2
Rutin
16
3. 3
4. 4
5. 5

• 11
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633722
• Total Posts
3013538
×