Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Virtual trackball implementation

I've been trying for the past few days to make a working implementation of a virtual trackball for the user interface for a 3D graphing-like program. But I'm having trouble.

Looking at the numbers and many tests the problems seems to be the actual concatenation of my quaternions but I don't know or think so. I've never worked with quaternions or virtual trackballs before, this is all new to me. I'm using the Quaternion class supplied by JOGL. I tried making my own and it worked (or at least as far a I know) but it was a complete mess so I just went with JOGL's.

When I do not concatenate the quaternions the slight rotations I see seem to be what I want, but of course It's hard when it's only moving a little bit in any direction. This code is based off of the Trackball Tutorial on the OpenGL wiki.

When I use the Quaternion class's mult (Quaternion q) method the graph hardly moves (even less than not trying to concatenate the quaternions).

When I tried Quaternionclass'sadd (Quaternion q)` method for the fun of it I get something that at the very least rotates the graph but not in any coherent way. It spazzes out and rotates randomly as I move the mouse. Occasionally I'll get quaternions entirely filled with NaN.

In my code I will not show either of these, I'm lost with what to do with my quaternions. I know I want to multiply them because as far as I'm aware that's how they are concatenated. But like I said I've had no success, I'm assuming the screw up is somewhere else in my code.

Anyway, my setup has a Trackball class with a public Point3f projectMouse (int x, int y) method and a public void rotateFor (Point3f p1, Point3f p2), Where Point3f is a class I made. Another class called Camera has a public void transform (GLAutoDrawable g) method which will call OpenGL methods to rotate based on the trackball's quaternion.

Here's the code:

public Point3f projectMouse (int x, int y)
{
    int off = Screen.WIDTH / 2;  // Half the width of the GLCanvas
    
    x = x - objx_ - off;         // obj being the 2D center of the graph
    y = off - objy_ - y;
    
    float t = Util.sq(x) + Util.sq(y); // Util is a class I made with
    float rsq = Util.sq(off);          // simple some math stuff
                                       // off is also the radius of the sphere
    float z;
    
    if (t >= rsq)
        z = (rsq / 2.0F) / Util.sqrt(t);
    else
        z = Util.sqrt(rsq - t);
    
    Point3f result = new Point3f (x, y, z);
    return result;
}

Here's the rotation method:

public void rotateFor (Point3f p1, Point3f p2)
{
    // Vector3f is a class I made, I already know it works
    // all methods in Vector3f modify the object's numbers
    // and return the new modify instance of itself
    Vector3f v1 = new Vector3f(p1.x, p1.y, p1.z).normalize();
    Vector3f v2 = new Vector3f(p2.x, p2.y, p2.z).normalize();
    
    Vector3f n = v1.copy().cross(v2);
    float theta = (float) Math.acos(v1.dot(v2));
    
    float real = (float) Math.cos(theta / 2.0F);
    n.multiply((float) Math.sin(theta / 2.0F));
    
    Quaternion q = new Quaternion(real, n.x, n.y, n.z);
    
    rotation = q;  // A member that can be accessed by a getter

    // Do magic on the quaternion
}

EDIT:

I'm getting closer, I found out a few simple mistakes.

1: The JOGL implementation treats W as the real number, not X, I was using X for real

2: I was not starting with the quaternion 1 + 0i + 0j + 0k

3: I was not converting the quaternion into an axis/angle for opengl

4: I was not converting the angle into degrees for opengl

Also as Markus pointed out I was not normalizing the normal, when I did I couldn't see much change, thought it's hard to tell, he's right though.

The problem now is when I do the whole thing the graph shakes with a fierceness like you would never believe. It (kinda) moves in the direction you want it to, but the seizures are too fierce to make anything out of it.

Here's my new code with a few name changes:

public void rotate (Vector3f v1, Vector3f v2)
{
    Vector3f v1p = v1.copy().normalize();
    Vector3f v2p = v2.copy().normalize();
    Vector3f n = v1p.copy().cross(v2p);
    
    if (n.length() == 0) return; // Sometimes v1p equals v2p
    
    float w = (float) Math.acos(v1p.dot(v2p));
    
    n.normalize().multiply((float) Math.sin(w / 2.0F));
    w = (float) Math.cos(w / 2.0F);
    
    Quaternion q = new Quaternion(n.x, n.y, n.z, w);
    q.mult(rot);

    rot_ = q;
}

Here's the OpenGL code:

    Vector3f p1 = tb_.project(x1, y1); // projectMouse [changed name]
    Vector3f p2 = tb_.project(x2, y2);
    tb_.rotate (p1, p2);
    
    float[] q = tb_.getRotation().toAxis(); // Converts to angle/axis
    gl.glRotatef((float)Math.toDegrees(q[0]), q[1], q[2], q[3]);

The reason for the name changes is because I deleted everything in the Trackball class and started over. Probably not the greatest idea, but oh well.

EDIT2:

I can say with pretty good certainty that there is nothing wrong with projecting onto the sphere.

I can also say that as far as the whole thing goes it seems to be the VECTOR that is the problem. The angle looks just fine, but the vector seems to jump around.

EDIT3:

The problem is the multiplication of the two quaternions, I can confirm that everything else works as expected. Something goes whacky with the axis during multiplication!

like image 905
SpaceFace Avatar asked Jul 03 '12 15:07

SpaceFace


1 Answers

The problem is the multiplication of the two quaternions, I can confirm that everything else works as expected. Something goes whacky with the axis during multiplication!

You are absolutely correct!! I just recently submitted a correct multiplication and Jogamp has accepted my change. They had incorrect multiplication on mult(quaternion).

I am sure if you get the latest jogl release, it'll have the correct mult(Quaternion)

like image 151
Tek Avatar answered Sep 22 '22 19:09

Tek