pythonmath3dpanda3d

Math formula for first person 3d game


I want to make a first person 3d game but I can't set the camera formula right.

So I have a rotation: 0 to 359. Next the x,y coordinates, z remains the same.

Camera rotation : 0 - front, 90 - left, 180 - back, 270 - right but I can adapt it

What is the formula for the camera ?

Platform: Panda3d, python, opengl

Thank you


Solution

  • OK, it looks like you need a Doom style camera movement, i.e., no up-down turns. Consider this:

    1. You need to render the "world" as seen through the camera.
    2. Assuming positive x is to the right and positive y is to your front, when the camera moves to the right the world's image moves to the left.
    3. When the camera turns positively to the left, the world's image turns to the right.

    Now, let's try to construct the equations:

    1.First, translate the world coordinates to the camera's position:

    Xwt = Xw - Xc;
    Ywt = Yw - Yc;
    Zwt = Zw;
    
    (Xc,Yc,Zc) = camera position
    (Xw,Yw,Zw) = world coordinates of object in the scene
    (Xwt,Ywt,Zwt) = world coordinates of object translated to camera position
    

    2.Now, rotate the translated coordinates by an angle opposite to the camera's rotation:

    Xwc =  Xwt * Cos(psi) + Ywt * Sin(psi);
    Ywc = -Xwt * Sin(psi) + Ywt * Cos(psi);
    Zwc =  Zwt
    
    Psi = angle of camera rotation
    (Xwc,Ywc,Zwc) = world coordinates of object transformed to camera orientation
    

    You can combine the two steps and transform it to a matrix form.