Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


'arcball/trackball' object rotation method question

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post

Link to post
Share on other sites
okay nevermind i just noticed i''m trying to rotate from the last mouse location instead of where dragging started..


i must be getting tried

Share this post

Link to post
Share on other sites
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.


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

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!