Trigonometry hell Finishing the day with cool code…

Trigonometry hell.

Finishing the day with cool code running:

  • World coordinates to camera coordinates, especially usefull for sound sources. Indeed, top, right and front depends on the relative position of the source in the camera space, not the global one.
  • Transformation of the sticks direction into world coordinates relative to camera, once again. This time, calculation is based on the lookat location, keeping the system centered on the screen.

Second point was trickier but solved first… Finding the position of an object in the camera space is basically a conversion of reference: center of the world is not (0,0,0) anymore, but camera world location + up is not (0,1,0) anymore, but camera orientation.

A bit of code, it can help to understand the trick:

// creation of the camera matrix, not sure it’s possible to retrieve it easier…
Matrix4 cam_mat = Matrix4( cam->getDerivedOrientation() );
cam_mat.setTrans( cam->getDerivedPosition() );
// inversion of the cam matrix
Matrix4 cam_mat_inverse = cam_mat.inverse();
// for a given vector expressed in global
Vector3 v( 10, 5, -45 );
// construction of a matrix representing this translation
Matrix4 m = Matrix4::IDENTITY;
m.setTrans( rel );
// MAGIC! > conversion to camera space
m = cam_mat_inverse * m * cam_mat;
// and, finally, getting back the coordinates in camera space
v = relm.getTrans();


Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.