# Need help with 2D Vector rotation

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

## Recommended Posts

So here is the deal: I've got this player consisting of diffrent parts. Like this:

Body

UpperBody

Arms

LowerBody

Feet
If the body rotates, the lowerbody rotates with it (render transform), and when the lowerbody rotates the feet rotates as well! This part works great.

So, now to my issue. The body on the toplevel may have a velocity. I want to translate that velocity to the feets coordinating system, so that when Im rotated in a direction the feet will move along accordingly. I have this function at my disposal:

Vector2D.RotateAroundPoint(Vector2D point, Vector2D origin, float rotation) //Im pretty sure this works, stold it from a physics engine and have been using it for other stuff
What I've been trying to do is something in similar:

Vector2D feetVelocity = Vector2D.RotateAroundPoint(body.Velocity, body.Origin, body.Rotation);feetVelocity = Vector2D.RotateAroundPoint(feetVelocity, LowerBody.Origin, LowerBody.Rotation);

But this doesnt work as I would expect. This has been buggering me the entire evening, so if anyone is good with algebra, please help me out here.

##### Share on other sites
So basically what you have is a frame hierarchy, and you're having trouble getting it to stay together when the body has velocity? Am I understanding this correctly?

Can you give us a little more information on the nature of the problem, and how you're structuring the relationships between the body parts? Do all the body parts contain an absolute position in the world, or does the body have an absolute position and the child parts (upper body, lower body, feet, arms, etc) store positions relative to the body?

##### Share on other sites

So basically what you have is a frame hierarchy, and you're having trouble getting it to stay together when the body has velocity? Am I understanding this correctly?

Can you give us a little more information on the nature of the problem, and how you're structuring the relationships between the body parts? Do all the body parts contain an absolute position in the world, or does the body have an absolute position and the child parts (upper body, lower body, feet, arms, etc) store positions relative to the body?

The body contains the world position, the bodyparts only contains local position relative to the body.

##### Share on other sites

If the body rotates, the lowerbody rotates with it (render transform)...

The body contains the world position, the bodyparts only contains local position relative to the body.

It would help a lot if you would post your code for applying transforms from parent to child. EDIT: It's not clear how you're applying the render transform or, as mentioned above, how you're applying parent-to-child transforms.

A common implementation for animating a frame hierarchy is to calculate a transform for each child based on it's local transform (with respect to parent) and it parent's animated transform. Something like:
 void TransformChild( PartReference childPart, MatrixTransform parentMat ) { childPart.finalTransform = childPart.transform * parentMat; if( childPart has a child ) TransformChild( childPart.child, childPart.finalTransform); if( childPart has a sibling ) TransformChild( childPart.sibling, parentMat ); } 
Given that function, you calculate the final transforms each frame:
 TransformChild( Body, worldTransform ); 
Then, each part is rendered using it's finalTransform.

This assumes your hierarchy is something like:
 Body Body.child = UpperBody UpperBody.child = LeftArm LeftArm.sibling = RightArm UpperBody.sibling = LowerBody LowerBody.child = LeftLeg LeftLeg.sibling = RightLeft 

##### Share on other sites
Hey again, sorry for my late response and thanks for helping me out!

Here is my recursive render call. What it does is first draw, for instance the texture of the body. Then it recursivly does the same thing with legs, head, etc but relative to the body.

Origin is a globalposition while rotation origin is just a local position from the textures top-left corner to its center.
 Matrix offset = transform; Matrix offset2 = transform; offset = Matrix.CreateTranslation(new Vector3(-g.rotationOrigin.X, -g.rotationOrigin.Y, 0)) * Matrix.CreateScale(g.Scale) * Matrix.CreateRotationZ(-g.Rotation) * Matrix.CreateTranslation(new Vector3(g.Origin.X, -g.Origin.Y, 0)) * offset; offset2 = Matrix.CreateTranslation(new Vector3(0, 0, 0)) * Matrix.CreateScale(g.Scale) * Matrix.CreateRotationZ(-g.Rotation) * Matrix.CreateTranslation(new Vector3(g.Origin.X, -g.Origin.Y, 0)) * offset2; if (g != null) g.DrawTexture(offset, camera); if (e.GraphicalSpace != null) { List<Entity> ent = e.GraphicalSpace.GetEntities(); XnaDeferredRenderSpace s = (XnaDeferredRenderSpace)e.GraphicalSpace; s.DrawTextures(ent, offset2, camera); } 

##### Share on other sites
Your code has various vectors and functions you haven't defined (so I don't know where the values come from or what the functions do), so it's difficult to understand what you're doing. I'm just guessing, but it looks like you're applying both the parent's transform and some global rotations, etc., to each child. A child's parent transform already contains the global rotation.

To clarify what you should be doing (pseudo-code):
 Matrx bodyTransform = // the transform for Body - position, rotation, etc. Draw( Body, bodyTransform ); //... // here is the Draw( bodyPart, transform ) pseudo-code Draw( bodyPart, transform ) { bodyPart.DrawTexture( transform, camera ); for( each child of bodyPart ) { Matrix childTransform = // the LOCAL transform for the child relative to its parent childTransform *= transform; // child's transform transformed by its parent Draw( child, childTransform ); } } 

##### Share on other sites

Your code has various vectors and functions you haven't defined (so I don't know where the values come from or what the functions do), so it's difficult to understand what you're doing. I'm just guessing, but it looks like you're applying both the parent's transform and some global rotations, etc., to each child. A child's parent transform already contains the global rotation.

To clarify what you should be doing (pseudo-code):
 Matrx bodyTransform = // the transform for Body - position, rotation, etc. Draw( Body, bodyTransform ); //... // here is the Draw( bodyPart, transform ) pseudo-code Draw( bodyPart, transform ) { bodyPart.DrawTexture( transform, camera ); for( each child of bodyPart ) { Matrix childTransform = // the LOCAL transform for the child relative to its parent childTransform *= transform; // child's transform transformed by its parent Draw( child, childTransform ); } } 

Ive got the drawing working correctly. What I do want to know is how to some how invert-transform a localpositio, lets say the tip of an arm, to the world position. Something like this:

Vector2 armTip; //The local position of the tip on the arm.
armTip = InversTransform(armTip, Body)// Gets armTip position in Body's coordinate-system
armTip = InversTransform(armTip, world) //Gets armTip position in World's coordinate-system

1. 1
2. 2
Rutin
19
3. 3
4. 4
khawk
15
5. 5

• 13
• 26
• 10
• 11
• 44
• ### Forum Statistics

• Total Topics
633743
• Total Posts
3013644
×