Jump to content
  • Advertisement
Sign in to follow this  
awesomefox5

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.

If you intended to correct an error in the post then please contact us.

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.

Thanks for reading, and thanks in advance.

Share this post


Link to post
Share on other sites
Advertisement
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!

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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: 3.png?w=510

the_visualist

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!