Jump to content
  • Advertisement
Sign in to follow this  

ArcBall Rotation Control in Java

This topic is 5195 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

I have basic mouse movement of a model in my app and I want the rotation control that arcball gives. So I'm going through the NeHe Tutorial #48 on ArcBall and trying to figure out what's needed in order to replace what I have and put arcball in. I'm using JOGL (java opengl bindings). Why do I have to define the Matrix4fT and Matrix3ft classes? Doesn't OpenGL do the matrix work for me? Am I having to rip out the glTranslate, glRotate, glScale calls in my app and replace them w/ these matrices? This is a bit confusing for me at this point. My model rotates, translates, and scales nicely, but I just don't have the mouse control that I want. I'm having a bit of a problem trying to figure out how to port arcball into a java app from the ex's given in the tutorial (no java samples given in the tutorial). If anyone knows JOGL ... my main class implements the GLEventListener interface which defines the game loop (display() method) and also my MouseListener methods, etc..., so I'm wondering if I can just put all the member variables in that class. No need for the separate arcball class as I see it since all the event work and rotation happen in this one class of mine. Any help much appreciated.

Share this post

Link to post
Share on other sites
Maybe it would help if you had an actual explanation of how an arcball rotation works instead of just the code.

When you press the mouse button down you need to translate the screen coordinates of the mouse into coordinates in the object space of the thing you want to control with arcball rotation. Then on every mouse movement event, take the last position of the mouse in object coordinates, and the current position of the mouse in object coordinates. Convert those two coordinates into vectors and use their cross product to generate an axis to rotate the object about, and the angle of the vectors as the rotation angle. Apply that rotation to the object and then set the new mouse position to last mouse position and wait for another mouse movement event.

So the hard part to translate the mouse coordinates into object coordinates. How I prefer to do this is to create a virtual sphere around the object to be manipulated (the virtual trackball). I use this sphere to create a circle on the screen through projecting through the transformation matrices. Once you have the center of the sphere and its radius in screen coordinates. Then the transformation of mouse coordinates into arcball coordinates looks like:

x = (screen_x - trackball.center.x) / trackball.radius;
// invert y: mouse coordinate y-axis runs opposite of GL's y-axis
y = - (screen_y - trackball.center.y) / trackball.radius;
z = 0.0;

double r_squared = x * x + y * y;
if (r_squared < 1.0) z = sqrt(1.0 - r_squared);

(x, y and z are in object coordinates, the other variables are in screen coordinates.)

This gets you a virtual trackball in a hemisphere in front of the object. Personally, I prefer the virtual trackball to expand further around the object, so I extend the coordinates with something like:

double r1 = sqrt(r_squared);
if (r1 <= 3.0) {
x *= 2.0 - r1;
y *= 2.0 - r1;
z = -sqrt(1.0 - x * x - y * y);

This sort of wraps the mouse around the back of the object (it makes more sense if you try it out). There are different ways to handle this outside area. This is a spherical transformation. You can also do a hyperbolic transformation, which gives you a larger area outside the object to play with. All that really matters is that it is continuous with the virtual trackballs center hemisphere.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!