look at

Started by
3 comments, last by apatriarca 13 years, 11 months ago
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?) ?
Advertisement
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.
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 !
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...
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.

This topic is closed to new replies.

Advertisement