Java-Gaming.org Hi !
 Featured games (84) games approved by the League of Dukes Games in Showcase (564) Games in Android Showcase (151) games submitted by our members Games in WIP (605) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Rotating Sphere with mouse [solved]  (Read 502 times) 0 Members and 1 Guest are viewing this topic.
RobinB

JGO Ninja

Medals: 44
Projects: 1
Exp: 3 years

Spacegame in progress

 « Posted 2013-01-22 13:22:16 »

Hello,

Im having an small problem i cant figure out (my math skills are not very good).
I want to rotate an sphere with the mouse.

The following code works with one axis:

 1  2  3  4  5 `float xdelta = (float)(Math.cos(Math.toRadians(rx)));float zdelta = (float)(Math.sin(Math.toRadians(rx)));glRotatef(rx,0f,1f,0f);glRotatef(ry,xdelta,0f,zdelta);`

where rx = x rotation (mouse input)
ry = y rotation (mouse input)

however this has odd results when both rx and ry != 0.

So my question is: how to rotate an 3d rotation matrix?
Roquen
 « Reply #1 - Posted 2013-01-22 14:02:49 »

search for: arcball
RobinB

JGO Ninja

Medals: 44
Projects: 1
Exp: 3 years

Spacegame in progress

 « Reply #2 - Posted 2013-01-22 15:30:24 »

Thank you, solved

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99  100  101  102  103  104 `package window.Rendering;import org.lwjgl.util.vector.Vector2f;import org.lwjgl.util.vector.Vector3f;import org.lwjgl.util.vector.Vector4f;/** * Tutorial and code taken from Nehe Lesson 48 * */public class ArcBall {    private static final float Epsilon = 1.0e-5f;    Vector3f StVec;          //Saved click vector    Vector3f EnVec;          //Saved drag vector    float adjustWidth;       //Mouse bounds width    float adjustHeight;      //Mouse bounds height    public ArcBall(float NewWidth, float NewHeight) {        StVec = new Vector3f();        EnVec = new Vector3f();        setBounds(NewWidth, NewHeight);    }    public void mapToSphere(Vector2f point, Vector3f vector) {        // Copy paramter into temp point        Vector2f tempPoint = new Vector2f(point.x, point.y);        // Adjust point coords and scale down to range of [-1 ... 1]        tempPoint.setX((tempPoint.getX() * this.adjustWidth) - 1.0f);        tempPoint.setY(1.0f - (tempPoint.getY() * this.adjustHeight));        // Compute the square of the length of the vector to the point from the center        float length = (tempPoint.getX() * tempPoint.getX()) + (tempPoint.getY() * tempPoint.getY());        // If the point is mapped outside of the sphere... (length > radius squared)        if (length > 1.0f) {            // Compute a normalizing factor (radius / sqrt(length))            float norm = (float) (1.0 / Math.sqrt(length));            // Return the "normalized" vector, a point on the sphere            vector.x = tempPoint.getX() * norm;            vector.y = tempPoint.getY() * norm;            vector.z = 0.0f;        } else    //Else it's on the inside        {            // Return a vector to a point mapped inside the sphere             // sqrt(radius squared - length)            vector.x = tempPoint.getX();            vector.y = tempPoint.getY();            vector.z = (float) Math.sqrt(1.0f - length);        }    }    public void setBounds(float NewWidth, float NewHeight) {        assert((NewWidth > 1.0f) && (NewHeight > 1.0f));        // Set adjustment factor for width/height        adjustWidth = 1.0f / ((NewWidth - 1.0f) * 0.5f);        adjustHeight = 1.0f / ((NewHeight - 1.0f) * 0.5f);    }    // Mouse down    public void click(Vector2f NewPt) {        mapToSphere(NewPt, this.StVec);    }    // Mouse drag, calculate rotation    public void drag(Vector2f NewPt, Vector4f NewRot) {        // Map the point to the sphere        this.mapToSphere(NewPt, EnVec);        // Return the quaternion equivalent to the rotation        if (NewRot != null) {            Vector3f Perp = new Vector3f();            // Compute the vector perpendicular to the begin and end vectors            Vector3f.cross(StVec, EnVec, Perp);            // Compute the length of the perpendicular vector            if (Perp.length() > Epsilon)    //if its non-zero            {                // We're ok, so return the perpendicular vector as the transform                 // after all                NewRot.setX(Perp.x);                NewRot.setY(Perp.y);                NewRot.setZ(Perp.z);                // In the quaternion values, w is cosine (theta / 2),                 // where theta is rotation angle                NewRot.setW(0.3f*Vector3f.dot(StVec, EnVec)); //Faktor 0.3 beschleunigt die Bewegung etwas            } else                                    //if its zero            {                // The begin and end vectors coincide, so return an identity transform                NewRot.setX(0.0f);                NewRot.setY(0.0f);                NewRot.setZ(0.0f);                NewRot.setW(0.0f);            }        }    }}`