Re: 3D mouse rotation
- From: Tony O'Bryan <storm_reaver@xxxxxxxxx>
- Date: Sat, 01 Oct 2005 16:27:10 GMT
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.
.
- Prev by Date: Re: Translation in OpenGL
- Next by Date: Re: Transfer data from VRAM to system memory
- Previous by thread: Transfer data from VRAM to system memory
- Next by thread: glCopyTexImage2D and npot frame buffer
- Index(es):
Relevant Pages
|
Loading