Allegro.cc Forums » Programming Questions » First person camera using transformations

Credits go to Edgar Reynaldo for helping out!
 Edgar Reynaldo Major Reynaldo May 2007 You have to understand there are two matrices involved. The projection matrix, and the view matrix. You're mixing the two, and you still aren't even trying to do what I showed you in the other thread.https://www.allegro.cc/forums/thread/617512/1038669#targetAlso, you are mixing 2D and 3D drawing without resetting your projection matrices properly.You should read over the manual entry for transformations.https://liballeg.org/a5docs/trunk/transformations.htmlSpecifically, look at the docs for al_build_camera_transform , al_orthographic_transform, and al_perspective_transform.For 2D you use the orthographic transform for the projection matrix. For 3D you use the perspective transform for the projection matrix. For 2D view matrix, all you need is the identity. For 3D, you need to apply a camera transform to the view matrix.First, see https://liballeg.org/a5docs/trunk/transformations.html#al_use_projection_transform for the difference between 2D and 3D projection matrices.Second, see https://liballeg.org/a5docs/trunk/transformations.html#al_build_camera_transform for how to set up your camera. As I told you before, you need to use a forward, right, and up vector to define the orientation of the camera. You also need a position for the camera. Then the lookAt parameter becomes camera.position + forward_vector.EDITYou can also 'cheat' and calculate the look vector yourself using a little trig.The compass heading becomes the rotation around the y-axis on the xz plane. ``` +Y -Z | / | / | / -X_____|/____ +X /| / | / | +Z -Y ``` And then the pitch become the rotation around the right vector. ```double heading = atan2(-dz,dx); double pitch = acos((dist(xz)/dist(xyz)); ``` I think that's right but I'm tired.
 Edgar Reynaldo Major Reynaldo May 2007 I think your formula is just a bit off. You have z and y reversed. It should be :``` fw.x = cos(pitch)*cos(yaw); fw.z = cos(pitch)*-sin(yaw); fw.y = sin(pitch); ``` {"name":"611674","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/9\/4\/94b598fff0562e56a5c7bf072c62b984.png","w":800,"h":600,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/9\/4\/94b598fff0562e56a5c7bf072c62b984"}Or you can build your forward vector using transforms : #SelectExpand 1 void RebuildTransform() { 2 fw = Vec(0,0,-1); 3 rt = Vec(1,0,0); 4 up = Vec(0,1,0); 5 /// Apply yaw 6 ALLEGRO_TRANSFORM t1; 7 al_identity_transform(&t1); 8 al_rotate_transform(&t1 , up.x , up.y , up.z , yaw); 9 al_transform_coordinates(&t1 , &rt.x , &rt.y , &rt.z); 10 al_transform_coordinates(&t1 , &fw.x , &fw.y , &fw.z); 11 12 /// Apply pitch 13 al_identity_transform(&t1); 14 al_rotate_transform(&t1 , rt.x , rt.y , rt.z , yaw); 15 al_transform_coordinates(&t1 , &up.x , &up.y , &up.z); 16 al_transform_coordinates(&t1 , &fw.x , &fw.y , &fw.z); 17 18 /// Apply roll 19 //... 20 Vec look = pos + fw; 21 al_build_camera_transform(&t , pos.x , pos.y , pos.z , look.x , look.y , look.z , up.x , up.y , up.z); 22 }
 Edgar Reynaldo Major Reynaldo May 2007 You need to transform your objects first, before applying the camera transform.Do the same thing, but swap the order.```ALLEGRO_TRANSFORM t; al_identity_transform(&t); al_translate_transform(&t , 0 , -3 , 0); al_compose_transform(&t , &cam); al_use_transform(&t); ```