Reputation: 1111
This is what I want, somebody wrote somewhere on net but i never use quaternion before, so have no idea how to implement it. I am sure it would be just a matter of one simple equation but how to implement in c/c++ code?
Here:
"You could use a bone stored as a vector made of 2 points (Head,Tail).
Since you are rotating it, Head will be the fulcrum and Tail will rotate
around an arbitrary axis. That's a quaternion's job."
I have absolute positions of all vertices of a cylindrical mesh, now if I create two vectors/points V1(x, y, z) and V2(x, y, z) at top and bottom ends, then I will be able to transform the mesh vertices by just transforming the V2(top end) point, but V1(bottom end) should not change its position.
I can do in OpenGL with glutSolidCylinder, its pretty simple, but here I want to implement with mesh vertices, so each vertex should be updated after any change in top or bottom vectors/points.
Thanks.
Upvotes: 0
Views: 348
Reputation: 51933
I use 1 transform matrix + bone length instead
How would you know around what axis to rotate from 2 points only? So make transform matrix representing bone origin. One axis is the bone axis (red) other one is the rotation axis of the joint (green) and the last is perpendicular to each (blue). Also the bone size (length) is needed (orange)
Now the mesh is a tree of bones
root sub-mesh is the starting point and the top layer bones are connected to it via joints. Second layer bones are connected to the first layer bones, and so on ... So you have to add index of previous bone/sub-mesh to which is current bone connected and can add also list of connected lower layer bones to speed up processing
Drawing / computing
this task is really just forward kinematics so translate/rotate root bone to desired object position. Take it matrix and store it to some temp. Draw/handle root sub-mesh/bone.
Now for
loop through all directly connected sub-meshes/bones to it and take its matrix and compute the multiplication of temp and it. Order depends on matrix style (row/column wise) this gets you actual matrix for sub-mesh/bone.
So handle this as root sub-mesh/bone before moving to next bone translate this matrix along blue axis by bone length and recursively handle all next bones ...
see old OpenGL API implementation: Rotating a multipart object
Rotation of joints
now you can add one or two angles of rotation per each bone. I use 1 degree of freedom rotations so I would rotate just around green axis. Simple multiplication by rotation matrix no quaternions needed.
Either remember new matrix (rotated) or the angle and create rotated matrix before any use of bone but always remember the original matrix to avoid cumulative rounding errors
[notes]
For more info google direct or forward kinematics problem. Also CCD and Inverse Kinematics may help there are tons of robotic arm examples in OpenGL so search ... You can transform any kinematics to set of single axis linear and angular actuators
For games and demos you can use BVH files to animate your meshes see:
Upvotes: 1