Monday, July 21, 2008

Rotation Issues

I have been working hard on coming up with a new LOD algorithm that eliminates that pesky "water" effect. I think I have a pretty good idea for a system that would not only remove that problem, but would also make the vertex shader faster.

Unfortunately I am currently stuck facing some problems with rotations. I have a mesh that I want to rotate around a sphere so that is always points at the camera. I could create a billboard matrix and use that, but there is a problem. That would create the water effect again. What I need to do is move the mesh in steps. If the camera moves beyond an angle threshold for either left/right or up/down, then the mesh should be moved one step in the appropriate direction.

I decided to use spherical coordinates to calculate the horizontal and vertical angles of the camera. This works great until you reach the north or south poles. Once you cross the poles, the mesh is rotated 180 degrees.

Here is the reason why that happens:

The "vertical" angle, theta, ranges from 0 to pi. Where 0 is pointing down the -Y axis and pi is pointing up the +Y axis. The "horizontal" angle, phi, ranges from 0 to 2pi. Where 0 is pointing along the +X axis, pi/2 is pointing along the +Z axis, pi is pointing along the -X axis, and 3pi/2 is pointing along the -Z axis.

In the case when the camera passes over the north (+Y) pole, theta is at its peak (pi) but then starts decreasing as you continue to the other side. Whereas phi essentially gets +pi added to it (or -pi depending up where the pole is crossed). That is why the object is being rotated 180 degrees (pi radians).


The problem is that I don't know how to fix this rotation. I have been tinkering around with it for the last several days to no avail. I certainly hope I come up with a solution soon. I really want to continue work on my new LOD algorithm.

Until next time...


John Leonard said...

Hey Patrick,

I don't know if it helps but I think there are similar problems with rotating cameras too - that at the poles everything swings around the z axis. Usually with cameras the up, right and look vectors are rotated every time the camera is rotated - perhaps you could try something similar?

The video you uploaded to YouTube is quite smooth, what did you use to capture it? Mine look like they went through some sort of kid-with-a-crayon-effect :)


Patrick said...

That's funny, I posted on the XNA forums asking how to fix the rotation problems and someone else mentioned the issues with cameras rotating as well.

I have been trying to think of ways to use a direction and/or up vector to solve the problem, but so far I can't really think of anything.

Some people have suggested using quaternions and other people have suggestions for entirely new coordinate systems. I feel that I have quite a bit of experimentation to do.

As for the video, I captured it using Fraps. I using the free version which is why the video was only 30 seconds long. I would have liked to show the wireframe mesh and how it updates, but I couldn't fit it all into one video.

After I captured the video with Fraps, I compressed it through Windows Movie Maker. It got the video from 300MB down to 3MB, while still being decent quality.