make object face camera (not so simple)

Started by
14 comments, last by DekuTree64 13 years, 4 months ago
there is of course a basic method of making an object face a point:
generate a lookat matrix...to do this i use D3DXMatrixLookat
for most purposes this works fine and no more effort is needed.

However, consider this:
1) an object is at 0,0,0
2) it is set to always face camera
3) camera moves up to the "poles" of the object, that is near directly above(or below) it (approaching 0,x,0)

you will notice the object starts to spin around on it's axis as the camera moves farther toward exactly above it. For most objects this is not a big deal, but for my purposes it is a bad thing.

I have what is basically a flat grid of polygons that is positioned at 0,0,0 that should always face the camera but maintaining the same kind of alignment...it shouldn't "spin". Also, i need to be able to rotate in "increments" of degrees or radians.

does anyone have a solution?
Advertisement
One possible solution would be to apply incremental rotations to keep the object aligned towards the target point. For any one update, the axis to rotate about would be the (normalized) cross product of the object's current forward vector and the vector from the object's position to the target position; the angle can be computed from these two vectors as well. Once you have an axis-angle pair, you can then apply a relative rotation to re-align the object. (There are a few details that have to be taken into consideration, but that's the general idea. Also, there's a couple of particularly nice quaternion-based algorithms that can be used for this as well.)
Can you post a screenshot? It sounds like you are constructing a lookat matrix from the camera location to each individual polygon center, which will cause them to "spin around" as you pass over them looking down. Try building a lookat matrix that looks from the camera origin to camera_origin + camera_normal, and use that for every polygon (you will only need to calculate it once, as the camera normal won't change between polygons). That sounds more like it is what you are looking for.
use Quaternions, the problem u face is called Gimbal lock and is caused by the use of Euler angles.

here a very good tutorial
Quote:Original post by kuroioranda
Can you post a screenshot? It sounds like you are constructing a lookat matrix from the camera location to each individual polygon center, which will cause them to "spin around" as you pass over them looking down. Try building a lookat matrix that looks from the camera origin to camera_origin + camera_normal, and use that for every polygon (you will only need to calculate it once, as the camera normal won't change between polygons). That sounds more like it is what you are looking for.
Are you referring to viewplane-aligned billboarding? If so, that would probably be a good solution as well (maybe better than the incremental rotations I suggested earlier, depending on the context).
Quote:Original post by Danny02
use Quaternions, the problem u face is called Gimbal lock and is caused by the use of Euler angles.
It's not gimbal lock :| And neither Euler angles nor quaternions have anything to do with it :/

For some reason, there's a tendency (on this forum and others) to a) attribute every artifact or undesirable behavior related to rotation to gimbal lock and b) cite quaternions as the optimal (or only) solution. I don't know where this comes from exactly, but none of that is relevant here. A 'look at' transform is constructed using simple vector math, not Euler angles, and the behavior the OP is describing is simply an artifact of the way the transform is constructed - it's not due to gimbal lock.
Quote:Original post by jyk
Quote:Original post by kuroioranda
Can you post a screenshot? It sounds like you are constructing a lookat matrix from the camera location to each individual polygon center, which will cause them to "spin around" as you pass over them looking down. Try building a lookat matrix that looks from the camera origin to camera_origin + camera_normal, and use that for every polygon (you will only need to calculate it once, as the camera normal won't change between polygons). That sounds more like it is what you are looking for.
Are you referring to viewplane-aligned billboarding? If so, that would probably be a good solution as well (maybe better than the incremental rotations I suggested earlier, depending on the context).


Yes. Especially when you are moving around I find that viewpoint aligned billboards shift in ways that tend to make me nauseous. So it seemed like a win-win to suggest doing it the viewplane-aligned way.
I guess i wasn't clear enough. I think this problem is indeed gimbal lock. In any case, its nothing to do with view aligned quads...Im trying to rotate a single object to face the camera in world space.

The same thing happens when i use Euler angles to rotate the object.
Don't you simply want to take the inverse of the rotation part of the camera matrix?

EDIT: Apologies, I didn't read the last sentence of your post.

When you build the lookat matrix, what do you use as your UP vector?

T
I use (0,1,0) as the up vector.
The problem is you up vector aligned with your view vector. There are several ways to solve this, one is by modifying your up vector a bit so it does not align with your camera (small amount will solve the problem), this is usually the way space movement is done (modifyng up vectors while rotating space ships).

Hopefully this helps.
Cheers

This topic is closed to new replies.

Advertisement