# Camera

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

## Recommended Posts

I have posted something very much like this before:) I'm trying to create a 3d person camera builtup from a position, view and up vector. I can move forward/backward, strafe and rotate around my character, the problem is that I can't make the character rotate so that it's back always facing the view of the camera. Would be really glad if someone could explain ( show some code ) how to achive this. // Brocket

##### Share on other sites
the viewdirection can be gotten from the modevlview matrix 2,6,10

##### Share on other sites
I already know the target direction, that's not the problem. The thing is that I don't know how to calculate the angle that I should use to rotate my model.
I tried to calculate the angle between the position/look vector of the camera, perhaps I'm doing something wrong or in the incorrect order?

##### Share on other sites
The dotproduct between the view dir and character facing direction will give you cos(theta).

##### Share on other sites
If you know the up and the forward vec, you can get the right vector via up cross forward. Then just fill your transformation matrix in colum major order and transform your points by that matrix.

Here's an example:

http://fileanchor.com/1283
BTW, 1,2,3,4,5 changes camera modes.

##### Share on other sites
Quote:
 Original post by Name_UnknownThe dotproduct between the view dir and character facing direction will give you cos(theta).

Hmm, I must do something very wrong:)
When rendering my scene I place the character at the cameras target vector, so
shouldn't the direction the character is facing always be the same direction that the camera is facing?

// Cameras position
CVector3f vPos;
// Cameras view
CVector3f vView;
// Cameras up
CVector3f vUp;
// Forward
CVector3f vForward = vDir - vPos;
// Dotproduct
float theta = Dot(vForward,vPos);

What's wrong?

##### Share on other sites
Quote:
 2float theta = Dot(vForward,vPos);What's wrong?

2 things in that line
A/ neither variable is normalized
B/ ones a vector and the other is a position

just use gluLookAt(..)
eg
gluLookAT( person.x - person_direction.x*camdist, person.y + 10, person.z - person_direction.z*camdist, person.x,person.y, person.z, 0,1,0)

##### Share on other sites
It could also be a problem that theta is not Dot(A,B).
Dot(A,B) = |a||b|cos(theta) is so you need to take arccos(Dot(A,B)) (also known as cos inverse)

##### Share on other sites
Would be really nice with some code:)

##### Share on other sites
Quote:
Original post by Brocketino
Quote:
 Original post by Name_UnknownThe dotproduct between the view dir and character facing direction will give you cos(theta).

Hmm, I must do something very wrong:)
When rendering my scene I place the character at the cameras target vector, so
shouldn't the direction the character is facing always be the same direction that the camera is facing?

// Cameras position
CVector3f vPos;
// Cameras view
CVector3f vView;
// Cameras up
CVector3f vUp;
// Forward
CVector3f vForward = vDir - vPos;
// Dotproduct
float theta = Dot(vForward,vPos);

What's wrong?

1. Your theta is not the angle(It's just the projection but it gives the quadrant the angle is lying). You have to divide the dot by he magnitudes of both vforward and vpos:
ie:
cos(theta) = dot(vforward,vpos)/(mag(vpos) * mag(vforward))
Then use acos to get the actual angle in radians.

2. You know the orientation the object is supposed to look at. You dont have to rotate your model to orient it, instead you could fill an object matrix and transform your model with that matrix. That's what I did with the demo.

[Edited by - relsoft on July 28, 2005 2:16:12 AM]

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 10
• 11
• 13
• 9
• 11
• ### Forum Statistics

• Total Topics
634091
• Total Posts
3015435
×