Sign in to follow this  
at0m

look at

Recommended Posts

Hi, I represent an entity using three vectors describing the position, the up and forward. I can't find the way to compute the new forward and up vector to make them point towards a position eg computing the correct rotation matrix (or is there another way?) ?

Share this post


Link to post
Share on other sites
If you are using Direct3D then you can use D3DX maths library to create a rotation matrix about an axis (D3DXMatrixRotationAxis()). To compute the axis to rotate around cross the look direction with the target direction and to work out the rotation angle take the arc cosine of the dot product of the two vectors.

You need to be careful if you don't want roll too, but try this and see if it gives you what you want.

Share this post


Link to post
Share on other sites
Yeah looks good!
I'm using glm, but I have everything I need for these calculations though. Thanks for this I'll try it out and keep you posted !

Share this post


Link to post
Share on other sites
Ok I finally found a simpler solution here it goes :

I compute the right vector with cross(fwd, target). Then I have :
fwd = target - pos and
up = cross (fwd, right)
and it works fine (+no angle manipulation) !
I hope it's still mathematically correct...

Share this post


Link to post
Share on other sites
There are an infinity of frame of reference which can be constructed starting from a point and a vector. Your problem isn't therefore well defined. Your method computes a possible frame of reference, but there are some issue.

I don't understand why you have chosen fwd and target to compute the right vector. I think they should be fwd and up. Target is a general point and it can also be zero. It can also be parallel to fwd. This isn't therefore a good choice in my opinion. On the other hand up is always perpendicular to fwd by construction.

right = cross(fwd, up); // now you have the initial reference
fwd = normalize(target - pos); // now you have a new unit forward vector
up = normalize(cross(right, fwd)); // in this way we obtains a new up vector

This method works iff (target - pos) isn't parallel to the current right vector. If this can't happen because for example the new target is always in a neighborhood of the old one, then you can just ignore this problem. If this isn't possible you have to choose a different vector.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this