# Rotation problem

Hello,

I still haven’t learned how to do rotation in a 3D space. I tried reading from many different places, but I always fail to learn. Every rotation problem gives me a headache to try to solve it, and I normally end up giving up.

I want to create a quad in a given point (on click). After created, when the mouse button is down (dragging), I want to rotate that plane in the direction of the mouse. In the following image, the red color represents the mouse. So, in the first image, the quad is created, and it’s centered on the mouse. Then, the mouse is moved to the southeast, the quad should rotate to match the mouse direction.

Can someone give me a hand on this?

https://dl.dropbox.com/u/3279456/Screens/rotation.png

Check out the parts on rotating a vector and using quaternion.lookAt() (different than spatial.lookat!)

https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:math_for_dummies

You basically want to construct a new quaternion from new up and forward vectors (y/z) that you derive from rotating the old ones.

I had that opened for the last hour trying to understand, have wikipedia and some math pages but I just can’t understand it. I needed a teacher for this. For months I’ve been trying to understand this, failing everytime.

What you can do is get the mouse coordinates from the current frame and subtract them from the previous frame. This will get you the direction vector. i.e [java]direction = currentCoords.substract(previousCoords);[/java]

From that direction vector (make sure its a vector3f object) you can convert it to a rotation as normen has suggested. Try using an up vector in the Z direction. I think my math video series touched on this somewhere

Try to do what I said:

[java]

Vector curUp=obj.getLocalRotation().mult(Vector3f.UNIT_Y); //get current up vector by multiplying the unit-y vector by the current rotation

Vector curForward=obj.getLocalRotation().mult(Vector3f.UNIT_Y); //same for forward

desiredRotationDelta.multLocal(curUp); //rotate vectors by desired rotation amount

desiredRotationDelta.multLocal(curForward);

obj.setLocalRotation(new Quaternion.lookAt(curForward, curUp)); //set new rotation from vectors

[/java]

What maths background do you have? (i.e. how far did you get in school/whatever on maths subjects)

If you don’t understand at a minimum Radians/Degrees Trigonometry (don’t need to memorise all the formula but you need to know what they can do) and Pythagoras then you need to read up and properly understand all those before you do anything else…

And in that given example, who would be the desiredRotationDelta? I’m sorry, I’m really trying to understand, but I just can’t. This is my nightmere, a friend of mine already tried to explain rotations to me and I failed hard.

I solved with this:

Works the way I wanted, but I still want to understand what @normen said

I know up to integral calculus. I just finished a computer science undergraduation course.

@zarch said:
What maths background do you have? (i.e. how far did you get in school/whatever on maths subjects)

If you don't understand at a minimum Radians/Degrees Trigonometry (don't need to memorise all the formula but you need to know what they can do) and Pythagoras then you need to read up and properly understand all those before you do anything else...

I don't really think so, if you can imagine vectors and can use and understand the methods provided in jme then you basically don't need any real math knowledge to do stuff (but obviously the better you're at math the more efficient your code is ;))

@shirkit: it would be some quaternion that defines the amount of rotation you want your spatial to perform. Your code will only work when the up-vector of the object is always y-up, so you cannot have it "look up" for example.

Pythag may be optional (but is elegantly simple anyway) but I really don’t see how you can get very far without the others.

I agree a lot of the implementation hassle is done for you by the engine but you are going to need more understanding to make use of it properly.

@shirkit

To keep things simple for now just think of one axis at a time. Get a square bit of paper, write PI/2, PI, PI3/2, 2PI(or 0) on the four sides, stick a pencil through the middle.

The pencil is your axis. If you want to rotate horizontal then hold the pencil vertically. Vertically is the y axis (Vector3f.UNIT_Y). You can see now if you spin the pencil the angles spin around flat.

Hold the pencil flat towards you (Z axis), spin it. You can see how they move.

Flat across in front of you (X axis), spin it.

So that’s your 3 axis. If you want to do a quarter turn then you need to spin the pencil by PI/2. A half turn is PI, a 1/3rd turn is PI
2/3, etc.

Once you can move things on a single axis it’s then just a matter of combining rotations to give any movement you need.

Take the paper, mark a grid on it and put an A at 1,2 and a B at -1,-1.

Now spin the paper the quarter turn. You can see that your A and B have moved. You just took the vectors and applied a rotation to them…

@zarch Yeah I just meant that the only thing you need to learn is what you need to understand the existing methods workings. The topics you mentioned are really dire basics but just googling “radians” should give you an idea of how to translate them (even if you didn’t get the unit circle part). What I really mean is you can learn it with jme, you don’t need to go away, learn math and then come back (unlike with java)

Now I got it. All I needed to fully understand this was to draw a triangle. I love math problems. You can always solve with the simplest solution.