COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

3D mouse rotation

• Follow

```I solved the problem (finally) I was having with mouse rotation along the X
and Y axes producing the side effect of rotating the up vector along the Z
axis.  It's really simple in retrospect, and I've
been kicking myself for having not seen it immediately.  I'd like to get
your opinions on whether this is the generally employed technique:

The problem is that the Up vector was getting rotated around the Z axis as
my rotation routines rotated around the X and Y axes (up/down and
left/right, respectively).  It's a consequent of accumulated
rotations around the X and Y axes after a few iterations.

The solution is to simply cancel out the up axis rotation along the Z axis.
This is done by forcing the up vector to be the cross product of (1,0,0)
and the current forward direction (after rotation).  That will force the up
vector to point straight up again.

Opinions?

```
 0

```Tony O'Bryan wrote:

> I solved the problem (finally) I was having with mouse rotation along the
> X and Y axes producing the side effect of rotating the up vector along the
> Z
> axis.

After I posted this, I realized that I hadn't actually solved the problem.
I just solved one particular facet of it.  The general problem continued to
manifest itself, so ignore what I wrote earlier about solving it.

However, I seem to have really solved the general problem now.  Fortunately,
or unfortunately since it took so long, the solution is still simple.
After including a visual representation of the camera axes on-screen, I was
able to see the root of the problem.  This isn't specifically an OpenGL
issue, but it's such a common problem that I think it's worth posting here
for the world's future OpenGL programmers.

When rotating the camera exactly along one of the two primary axes of
rotation (the X and Y axes), there is no residual rotation around the Z
axis.  This is probably (I'm not a math type, so I'm speculating) because
the axis that is not being rotated around will produce zero for the sine or
cosine (depending on the axis of rotation) contributing to the Z axis
rotation (its angle is zero).

When rotating the camera along a line not aligned with one of the two
primary axes, the camera's X vector will tilt away from the (1,0,0) vector.
This tilt needs to be corrected so that the camera's X vector is forced
back to a level plane position.  However, the vector (1,0,0) can't be brute
forced into the camera since that won't take into account where the camera
is pointed.  It will also greatly distort any up/down angle active in the
current view.

The solution is as follows (apply this after the camera has been rotated):

1) Calculate the cross product between the camera's current forward (Z)
vector and it's current up (Y) vector to get the camera's X vector (Y cross
Z).

2) Set the Y component of the X vector to zero, and normalize the vector
(setting the Y component to zero denormalized the vector).

3) Calculate the cross product between the camera's forward (Z) vector and X
vector (Z cross X) to get the camera's new up vector (step 2 will destroy
the right angle between the camera's Y and X vectors).

The camera's X vector will now be forced to be level while preserving the
rest of the camera's view angles -- up/down and left/right.

```
 0

1 Replies
540 Views

Similiar Articles:

7/21/2012 2:59:03 AM