# How to rotate a 3D - Cube by Mousemoves?

Hey…

I´ve just coded a 3D Cube…
It works with the Win32 GDI(I know very slow) and works with mathematical matrix-computing.

It´s possible to rotate it around the 3 Axis, with buttons…

Know I wanna know how to rotate it with simply mousemoves…can you help me at this problem?

You use the movement of the mouse to calculate the value of the angles.

So, if the first value you read from the mouse was that it was located at (30,50) and the next time you read the mouse, it is at (50, 40) then you’ll have a positive y rotation and a negative x rotation.(As you can see, the X component tells the y axis rotation, and the Y component tells the x axis rotation(some thinking about geometry will cleary make you understand why.)

Still, we don’t have a rotation value. Well for that, you need a mouse speed. So let’s say I use, for every 10 units move by the mouse, I have an angle of 10 degrees.

so (50,40) - (30,50) = (20, -10). This what gives us :

at -10 degree rotation around the x axis, and a +20 degrees around the y axis.

You can even had a user defined variable(like in games) to set the mouse speed to the liking of the users. You just multiply the value of the angle with that variable to have the new mouse speed.

There is something called quaternions that can make 3D rotation whit just your mouse, i have the files (trackball.h, trackball.c) made by Gavin Bell for Silicon Graphics and dinospin.c an example on how to use it, e-mail me so i can give you all this files.

I think the source code for what Gorg was describing can be found in the meshview example here http://trant.sgi.com/opengl/examples/more_samples/more_samples.html
It is what I based my Planet3D code on.
http://www.dialspace.dial.pipex.com/voodoopeople/mars3d.html

Originally posted by Gorg:
[b]You use the movement of the mouse to calculate the value of the angles.

So, if the first value you read from the mouse was that it was located at (30,50) and the next time you read the mouse, it is at (50, 40) then you’ll have a positive y rotation and a negative x rotation.(As you can see, the X component tells the y axis rotation, and the Y component tells the x axis rotation(some thinking about geometry will cleary make you understand why.)

Still, we don’t have a rotation value. Well for that, you need a mouse speed. So let’s say I use, for every 10 units move by the mouse, I have an angle of 10 degrees.

so (50,40) - (30,50) = (20, -10). This what gives us :

at -10 degree rotation around the x axis, and a +20 degrees around the y axis.

You can even had a user defined variable(like in games) to set the mouse speed to the liking of the users. You just multiply the value of the angle with that variable to have the new mouse speed.
[/b]

So it isn´t possible to rotate around the Z Axis when you move the mouse?

Well, of course you can use whatever axis you want. I’ve just stick with opengl basic axis position, and has as I know, mice only rolls on 2 axis. Of course, if you have a track ball, you could also use it to make full 3d rotation, but I find that quite ankward.

If you stick with openl axis standard, a rotation around the z axis would mean leaning on the right or on the left( like Thief, SS2, SOF, etch). So using keyboard for that one is still fine unless you want the player to chose how fast he will lean.

[This message has been edited by Gorg (edited 06-10-2000).]

Originally posted by Mikey3:
So it isn´t possible to rotate around the Z Axis when you move the mouse?

Actually the trackball sources implement a rotation about an arbitrary axis specified by a virtual sphere and the point on that sphere on which you start to drag with your mouse.
That means if you start on the edge of that virtual sphere and drag around the screen in a circle you rotate around the viewing direction, well the camera’s z axis if you want.
I like that method best and it’s most intuitive and better than most methods in CAD or DCC programs.

An additional detail, which may or may not apply to what you are trying to do, is that you may do to something like this:

#define SENSITIVITY 0.2f

static POINT pos, oldpos;
float difx, dify;

GetCursorPosition( &pos );
SetCursorPosition( (screen_width >> 1), (screen_height >> 1) ); // use your screen height and width variables here
difx = (pos.x - (screen_width >> 1)) * SENSITIVITY;
dify = (pos.y - (screen_height >> 1)) * SENSITIVITY;

view_angles[YAW] += difx;
view_angles[PITCH] += dify;

Notice getting and then setting the cursor position? I had to do something similar to this to lock the mouse to my window, otherwise it will go outside and things may not work so great.

Maybe of use to you…or maybe not?

Good Luck!

You are right geoff. Weird things happen if you try to read the cursor outside the window because windows only send messages to the window that the message applies to.(ugh, what an ugly sentence, sorry I am french)

You can, by using some win32 calls, read the cursor outside the window.

But I think that your solution is more elegant because you only touch your window.