Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (536)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Rotating Sphere with mouse [solved]  (Read 413 times)
0 Members and 1 Guest are viewing this topic.
Offline RobinB

JGO Ninja


Medals: 44
Projects: 1
Exp: 3 years


Spacegame in progress


« Posted 2013-01-22 14: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?
Offline Roquen
« Reply #1 - Posted 2013-01-22 15:02:49 »

search for: arcball
Offline RobinB

JGO Ninja


Medals: 44
Projects: 1
Exp: 3 years


Spacegame in progress


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

Thank you, solved

I adapted some code i found on code.google.com

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);
            }
        }
    }

}
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Riven (20 views)
2014-07-29 18:09:19

Riven (13 views)
2014-07-29 18:08:52

Dwinin (12 views)
2014-07-29 10:59:34

E.R. Fleming (31 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (42 views)
2014-07-24 01:59:36

Riven (42 views)
2014-07-23 21:16:32

Riven (28 views)
2014-07-23 21:07:15

Riven (29 views)
2014-07-23 20:56:16

ctomni231 (60 views)
2014-07-18 06:55:21
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!