Jump to content
  • Advertisement
Sign in to follow this  
Matt Aufderheide

make object face camera (not so simple)

This topic is 2929 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!