i have tried to implement a first person camera, but as you might guess: It don't work. everything works fine in the whole 3d department of my code, except for the camera. if you look forwards everything works, but if you look 90 degrees to the side instead of the camera looking up and down when you move the mouse it spins in a circle and if you look 180 degrees behind yourself the camera is inverted. how do i fix this? i have tried many different formulas but none of them work.
mouse processing:
camera transformation code:
whole code:
(and i also cant manage to figure out how to draw a 'hud' or just simple things right on the display after the transformations)
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#target
Also, 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.html
Specifically, 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.
EDIT
You 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.
I think that's right but I'm tired.
OK so; I have majorly rewritten how everything about my perspective/transformation/drawing works. I now first generate 1 mesh, where everything 3d is stored and then draw all of that at once to the screen. 2d operations work now too and i think i have gotten a much better grasp on how the perspective transformations work. i now also use the camera transformation. the only thing that doesent work now is: the camera. I think I'm using the camera correctly, but apparently I'm not. and if you see anything I'm doing wrong now with the transformations, pleas tell me.
I think your formula is just a bit off. You have z and y reversed. It should be :
{"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 :
ok thank you! you've been a great help in all of this. there is just 1 more thing id like to figure out. in my code right after i use the camera transformation, i draw the whole map mesh. now i would like to also draw other things and use transformations on them. but if i trie to do that, it doesent really work. the things i draw after the new transformations move with the camera rotation. how would i fix that?
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);
thank you! you've been a great help!
if anybody wants the full source code:https://github.com/dontknow3d/fps-camera-using-allegro-5