0
\$\begingroup\$

I'm trying to program camera movement to be relative to camera rotation. (Forward is always forward, regardless of pitch, yaw, and roll) I want to be able to move forward, backward, left, right, up, and down.

I do not want to use a matrix.

I want to use sin and cos from the standard math library.

The camera rotates on all 3 axis. The rotation order is z(roll), y(pitch), x(yaw). When all rotations are 0, positive z is forward, positive y is down, and positive x is left.

So far I've gotten forward and backward movement to work with:

velZ = speed * cos(rotX) * cos(rotY);
velY = speed * sin(rotY);
velX = speed * sin(-rotX) * cos(rotY);

If somebody knows how to do this or knows where I can find information on this, it would be greatly appreciated.

\$\endgroup\$
  • 3
    \$\begingroup\$ A matrix is just a concise definition of multiplying some values and getting multiple values back out. If you can define those multiplications with 20 lines of procedural code, you can do it as well or better with a matrix. Why the matrix-less requirement? It's equally true, of course, that if you can find the matrix definition, you can create 20 lines of code to mimic it. \$\endgroup\$ – Seth Battin Oct 16 '14 at 21:12
  • \$\begingroup\$ This is an XY problem - a request for instruction on an inappropriate solution. If there was insight to be obtained by performing the calculations directly from the trigonometry this might be forgiven, but the opposite is true - the solution will be so cumbersome, and so difficult to make performant, that all insight will be buried under the code. \$\endgroup\$ – Pieter Geerkens Oct 18 '14 at 3:08
1
\$\begingroup\$

In neutral position you have defined forward to be the positive Z vector (0, 0, 1). There are two vectors perpendicular to that vector (if we ignore sign), up (0, 1, 0) and left (1, 0, 0).

The easiest thing would be to create all three vectors and to apply a matrix transformation to find the left, up, and forward vector in 'camera space'.

Matrix transform = Matrix.CreateFromYawPitchRoll(y, p r);
Vector3 forward = Vector3.Transform(transform, new Vector3(0, 0, 1));
Vector3 left = Vector3.Transform(transform, new Vector3(1, 0, 0));
Vector3 up = Vector3.Transform(transform, new Vector3(0, 1, 0));

You do not necessarily need a matrix. You can also find the left and up matrix by using the cross product. (See here Unity's documentation on it, but the mathematical principals apply in general).

Once you have all three vectors you can strafe and move up by simply adding the correct vector (multiplied by movement speed) to the camera position (and look at) vector(s). For example:

public void MoveLeft(float speed)
{
    camera.Position = camera.Position + (left * speed);
}
\$\endgroup\$
  • \$\begingroup\$ I'll look into It. But I'd like to just use sin and cos functions if I can. I've never worked with matrix rotations in this way before. \$\endgroup\$ – Willy Goat Oct 16 '14 at 17:46
  • 2
    \$\begingroup\$ Those sin/cos functions are exactly how the matrix is created. See here. There's no sense in rewriting what someone else did and thousands have tested, especially if it's part of a library you're already using. \$\endgroup\$ – Icy Defiance Oct 16 '14 at 18:37
  • \$\begingroup\$ I do not want to use a matrix. I want to use sin and cos. \$\endgroup\$ – Willy Goat Oct 16 '14 at 20:41
  • \$\begingroup\$ What framework are you using, are matrix calculations not built in? That would complicate things. However, not using matrices will also complicate things. I do not have time at the moment to deduce all calculations done by the matrix for all cases. (There are some corner cases when using trig, where you need to account for the correct quadrant and stuff). Maybe someone else will do it for you, or you can deduce them yourself. \$\endgroup\$ – Roy T. Oct 17 '14 at 8:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.