'arcball/trackball' object rotation method question
Hi, i''m using an arcball/trackball sphere to try and rotate an object in 3d space, however I can''t seem to get it working.
my code is basicly split into three function, ones reads the mouse input and converts it to the -1.0 to 1.0 range needed for the sphere
float ox = (oldX - sphereX) / sphereRadius;
float oy = (oldY - sphereY) / sphereRadius;
float nx = (mouseX - sphereX) / sphereRadius;
float ny = (mouseY - sphereY) / sphereRadius;
trackball->drag(ox, oy, nx, ny);
where oldX, oldY is the prevoius mouse location and sphere Raduis is half the size of my square viewing pannel (screenWidth / 2), sphereX and sphereY are the center locations for my sphere (also half the screen size)
the drag method then updates the rotation as follows
drag(GLfloat oldX, GLfloat oldX, GLfloat newX, GLfloat newY) {
GLvector oldV = GLvector(oldX, oldY, projectToSphere(TRACKBALLRADIUS, oldX, oldY));
GLvector newV = GLvector(newX, newY, projectToSphere(TRACKBALLRADIUS, newX, newY));
// product of new and old
GLvector axis = newV.cross(oldV);
axis.normalize();
// rotation around cross axis
// calc angles
oldV -= newV;
GLfloat ang = oldV.magnitude() / (TRACKBALLRADIUS * 2.0f);
// clip
if (ang > 1.0f) {
ang= 1.0f;
}
else if (ang < -1.0f) {
ang = -1.0f;
}
// calc rad angle around axis
GLfloat angle = 2.0f * asin(ang);
// get delta rotation
GLquaternion delta = GLquaternion(axis, angle);
// combine
rotation = rotation * delta;
rotation.normalise();
}
and the final function is the projectToSphere function, used for getting the z component
GLfloat projectToSphere(GLfloat radius, GLfloat x, GLfloat y) {
GLfloat z;
GLfloat distance = sqrt(x*x + y*y);
if (distance < radius * M_POINT5) {
// inside the sphere
z = sqrt((radius * radius) - (distance * distance));
}
else {
// outside the sphere
GLfloat foo = radius / (M_POINT5 * 2);
z = (foo * foo) / distance;
}
return z;
}
GLvector and GLquaternion are obvoiusly vector and quaternion structs. I have working keyboard rotation (using slerps to rotate in 90degree multiples), but my drag function just makes my test object (a cube) rotate madly and huge amounts however little i move the mouse.
I''m sure it''s somthing simple that i''ve just missed, any help would be greatly appreciated
okay nevermind i just noticed i''m trying to rotate from the last mouse location instead of where dragging started..
DOH!
i must be getting tried
DOH!
i must be getting tried
Okay now i''ve had a new problem, in that when i drag the sphere around to a point, then stop dragging, it appears to alter the rotation axis for when i start dragging again.
example:
if i have an object facing towards me and drag left on the mouse, it rotates the direction of the drag, then i let go of the mouse button to stop the drag when it''s facing away from me.
so far so good
however if i then start dragging left it rotates the oppositie direction as before. now i''m assuming this is because if i was looking at it from the pov of the front face it would still be rotating correctly, however i was under the impression this was one of the things the arcball solved.
does anyone have any idea what i could be doing wrongly?
even a small hint would be nice.... anything? please
example:
if i have an object facing towards me and drag left on the mouse, it rotates the direction of the drag, then i let go of the mouse button to stop the drag when it''s facing away from me.
so far so good
however if i then start dragging left it rotates the oppositie direction as before. now i''m assuming this is because if i was looking at it from the pov of the front face it would still be rotating correctly, however i was under the impression this was one of the things the arcball solved.
does anyone have any idea what i could be doing wrongly?
even a small hint would be nice.... anything? please
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement