Sign in to follow this  
relpats_eht

Rotation not working correctly in skeletal animation

Recommended Posts

last night i started adding skeletal animation to my MilkShape model class (only joint rotation for now). well, it works except for the fact that the verticies are moving out from the bone and back in, as if they were rotating around a point a slight distance away from the bone. ill post the code i wrote as well as a variable explanation
void MSmodel::rotatebone(int bonei, float angle, float xAxis, float yAxis, float zAxis){
    int i,l,b;                            // looping vars
    float cosTheta = (float)cos(angle);
    float sinTheta = (float)sin(angle);
    CPoint vNewPos;
    CPoint vPos;
    
    for(i=0; i<meshes; i++){
        for(l=0; l<mesh[i].numverts; l++){
            for(b=0; b<mesh[i].vert[l].owners.size(); b++){
                if(mesh[i].vert[l].owners[b] == bonei){
                    vPos = mesh[i].vert[l].pos - bone[bonei].pos;
                
                    vNewPos.x = (cosTheta + (1 - cosTheta) * xAxis * xAxis) * vPos.x;
                    vNewPos.x += ((1 - cosTheta) * xAxis * yAxis - zAxis * sinTheta) * vPos.y;
                    vNewPos.x += ((1 - cosTheta) * xAxis * zAxis + yAxis * sinTheta) * vPos.z;

                    vNewPos.y = ((1 - cosTheta) * xAxis * yAxis + zAxis * sinTheta) * vPos.x;
                    vNewPos.y += (cosTheta + (1 - cosTheta) * yAxis * yAxis) * vPos.y;
                    vNewPos.y += ((1 - cosTheta) * yAxis * zAxis - xAxis * sinTheta) * vPos.z;

                    vNewPos.z = ((1 - cosTheta) * xAxis * zAxis - yAxis * sinTheta) * vPos.x;
                    vNewPos.z += ((1 - cosTheta) * yAxis * zAxis + xAxis * sinTheta) * vPos.y;
                    vNewPos.z += (cosTheta + (1 - cosTheta) * zAxis * zAxis) * vPos.z;

                    mesh[i].vert[l].pos = bone[bonei].pos + vNewPos;
                }
            }
        }
    }
}
type CPoint is in most basic terms a class that holds x,y, and z values mesh is an array of structs of all meshes vert is an array of structs of all verticies and is declared by mesh bone is an array of structs of all joints pos (in both bone and vert) is a CPoint of its current location owners is a vector of the indicies of all the bones that control the selected vertex if anyone can tell me what im doing wrong, or a suggested improvement about how i am doing things, please reply, thank you.

Share this post


Link to post
Share on other sites
It looks like you are pivoting on the wrong point. At frame zero does everything look ok, and then a few rotation frames later everything appears disjoined?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this