# Matching the rotation of one object with another

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

## Recommended Posts

Hey everyone! I implemented a way of drawing objects partially underwater, but an issue with rotation popped up.

How I do my drawing is I check the depth of the water and draw the top part of the object normally and the part submerged in water (height of the sprite - the water depth) separately. Since these two parts are often different sizes, rotating the object will cause both parts to rotate at different speeds, disfiguring the object.

What I need is a calculation that will change the submerged part's rotation to match with the rotation of the unsubmerged part. Think of two circles with one smaller and one larger. It will take the smaller one less time to complete a full rotation, so it won't be rotating in unison with the larger circle. I need something that, given the rotation value, will modify the smaller circle's rotation value and place it at the same point of rotation as the larger one (Ex. both circles will be at pi/2 rad in 3 seconds as opposed to the smaller circle reaching pi in 3 seconds and the larger one reaching pi/2 in 3).

Edited by Kimimaru

##### Share on other sites

I think the problem is your rotating both around their respective center and thus not making them rotate together. Rotate both around a common point, probably the center of the bottom part in this case.

//Do this for both pieces
glTranslatef( Bottom.x, Bottom.y, Bottom.z );
glRotatef( Rotation, 0.0f, 1.0f, 0.0f );


This is if you're using OpenGL( deprecated method shown ), but the same idea would apply.

##### Share on other sites

Yeah, that's precisely it. I'd want them to both rotate around the original center as if they were one piece, but I still need them separated so the lower part is drawn under the water. What I think I need actually is to change the origin I'm drawing both parts at and make sure their origins meet up in the same spot.

Say I had an entire sprite that was of height 70 and the top part was 20 and the bottom was 50. If my origin had a Y value of 35, I need to have the top part rotate around 35 and the bottom part to rotate around that same amount relative to its total size...how exactly to do it isn't coming to me, though. Especially when you consider that the top and bottom parts can be changing in size if the object is heading towards the surface of the water or coming into the water.

EDIT: Never mind; everything works fine when the rotation is 0, so I may have to adjust the rotation. It also seems like I'll need to change the parts drawn underwater depending on the rotation...so if an object is rotated by pi radians vertically then the part that wasn't underwater must now be underwater and the previously submerged part must now be above the surface.

Any ideas?

Edited by Kimimaru

##### Share on other sites

Sorry for the double post, but I uploaded a video simulating my problem: http://s300.photobucket.com/user/Kimimaru4000/media/RotationTest.mp4.html

You can see the effects of the rotation on the bottom part that is submerged in water. The rotations themselves are fine and match up, but it looks like I need to change the position or something. Any advice?

Edited by Kimimaru

##### Share on other sites

I really can’t understand your explanation nor the video at all.

I assume this is what you want to ask: “I want these 2 objects to stick together to form a single solid object under all rotations and scales etc.”

Make a parent object (which does not need an image of its own) and make the 2 body parts children of that object with local offsets from it.  The torso will have a positive Y offset and the legs will have a negative Y offset.  These should be stored in matrices.

When rendering, take the world matrix of the parent object and “propagate” it into all of its children.  In other words the parent has a world matrix and the 2 children have local matrices.  They need world matrices, and you get that by multiplying them with the parent’s world matrix.

That is, for each child, CHILD.WORLD_MATRIX = PARENT.WORLD_MATRIX * CHILD.LOCAL_MATRIX.

Draw using the childrens’ world matrices.  After this is done, any scaling, offsetting, or rotating done to the parent will correctly trickle down into the children.

L. Spiro

• 18
• 19
• 11
• 21
• 16