Page 1 of 1

Need help with rotations

Posted: Sun Jun 05, 2011 8:57 am
by ExNihilo
I need to create a number of objects made up of several rectangular boxes, possible best described by this "drawing":

Code: Select all

o-----o
|\   /|
| \ / |
|  X  |
| / \ |
|/   \|
o-----o
where o's is where joints are. I've got this working fine when centered at the origin (I calculate positions of the joints - the corners of the whole thing - and then the positions for the boxes from there), but cannot rotate this object. I've tried creating a rotation matrix arond a, for example, Vector3.Forward axis, and while the boxes get drawn correctl and seem to interract with the rest of the geometry correctly, the joints do not. I have code that destroys joints when the joined entities get too far from each other, and, immediately upon creation, the joins give an Error of about 1.3f (which tells me nothing, but its obviously not what it should be, since the edges boxes are much closer together).

I'll need to create many of these objects and do not know the positions and orientations ahead of time. Is there any way to create one centered at the origin and then transform the whole thing (so, all the Entities and all Joints) by a given Matrix?

Re: Need help with rotations

Posted: Sun Jun 05, 2011 2:34 pm
by Norbo
It's usually easiest to set everything up in local space first (or at least with identity orientation). Then, you can just rotate/translate the involved entities consistently. The joint properties will not need to be touched after the initial local space configuration. Provided that the transformation applied to the entities is correct, everything should work.

To create a correct transformation for an individual entity, first compute the offset from the 'center' of the structure that you are rotating around. Rotate that offset by the rotation amount. The new position of the entity is then the center + R(offset), where R is the rotation transformation. Finally, add the translation component of the transformation: center + R(offset) + T.

Re: Need help with rotations

Posted: Mon Jun 06, 2011 12:12 am
by ExNihilo
huh... would've never thought of doing it this way late at night.
Thanks for the help!, things seem to be working correctly now. Though I'm sure I'll have more questions to come..