PDA

View Full Version : How do I get the forward vector or my camera?

hashbrown
09-06-2016, 01:38 PM
The only functionality I'm missing on my camera is getting the forward vector using the pitch and yaw. I know I've calculated the Pitch and Yaw correctly because the camera rotates appropriately when moving my mouse, but my character won't walk towards the direction I'm looking at. Just forward,back,left, and right relative to the world's origin.

I have no idea how to get the fwd vector using my calculated pitch and yaw. Hope somebody can point me towards the right direction. Below I'll share some of my implementation. Thanks!

Mat4 Camera::getOrientation() {
Mat4 xAxis = Trig::Rotate(X_AXIS, Vec3(-m_pitch, 0, 0));
Mat4 yAxis = Trig::Rotate(Y_AXIS, Vec3(0, m_yaw, 0));
return xAxis * yAxis;
}

Mat4 Camera::GetView() {
return getOrientation() * Trig::Translate(m_pos);
}

// When I move the mouse
Mat4 Camera::OffsetOrientation(float rightAngle, float upAngle) {
m_yaw += rightAngle;
m_pitch += upAngle;
normalizeAngles();
}

// Forward, back, left, right
void Camera::OffsetPos(Vec3 &offset) {
m_pos += offset;
}

Everything works perfectly up to now, but I'd love for the camera to move towards where I'm looking at.

Oh and if you want to check out Trig::Rotate, you can see it here (www.pastebin.com/vta8sSnD).

GClements
09-06-2016, 03:49 PM
If your matrix transforms from eye space to world space, the forward vector will be the third column.

If your matrix transforms from world space to eye space, the forward vector will be the third row.

BBeck1
09-06-2016, 06:49 PM
And of course the other columns (1 and 2) form vectors pointing up and to the side. (Multiplying a vector times -1 reverses it's direction to give you the other directions.) The fourth column contains the position. Or maybe they are rows as GLements pointed out. You can throw away the 4th (w) value when turning them into 3D vectors. You just need the w value when the data is in the matrix in order to make all the math work out right.

I jump back and forth between environments so much I never know which one is the -Z axis or whether it's column or row major. So, I just try both and one will work and the other won't.

hashbrown
09-06-2016, 08:15 PM
Thank you very much guys, I got it working after reading the last 2 replies.