Results 1 to 4 of 4

Thread: 3rd person view to 1st person view calculations

  1. #1

    3rd person view to 1st person view calculations

    Hey all!

    I have been working on my first 3D game (and entry to the compo!), and I came across something tricky.
    I made sure I read all the threads in this forum, so as not to repeat someone else. I saw NecroDome asked something similar a couple of months ago but seems to have given up on the thread.

    Here it is:
    In your 3D 'World', you have a 'board' (rectangle). The coordinates to the centre of the board are Wx, Wy and Wz; its rotation is RWx, RWy and RWz. Now, with opengl, to draw the board, all i need to do is
    translatef(Wx,Wy,Wz); //move to centre of board
    rotatef(RWx, 1, 0, 0); // rotate board (= entire world)
    rotatef(RWy, 0, 1, 0);
    rotatef(RWz, 0, 0, 1);
    calllist(board); //draw board

    ok, i have had tht working for a while.
    Then, I created a tank, which has coordinates Tx, Ty and Tz, from the left-top corner of the board (important!); and rotation RTx, RTy and RTz (where rotation=(0,0,0) means the tank is flat on the board and facing the right).
    The tank is therefore 'linked' to the board, since it is defined relative to the board.
    Similarly, to draw the board and the tank, i write the same code as above, plus:
    translatef(-boardsize.x/2,-boardsize.y/2,-boardsize.z/2); //move to top left corner of the board
    translatef(Tx,Ty,Tz); // move to position of tank
    rotatef(RTx, 1, 0, 0); // rotate tank
    rotatef(RTy, 0, 1, 0);
    rotatef(RTz, 0, 0, 1);
    calllist(tank); // draw tank

    Clear, so far?
    Well, now, I decided to 'see' the 'world' from the tank's point of view, when a certain key is pressed.

    That's where im having trouble. You see, everything is drawn relative to the centre of the board, so all I need to do is to calculate the World(centre of the board) coordinates from the tank (that took me a few hours to figure out! cos i first thought it was even more complicated!)!

    Obviously, if the tank is not rotated, it's very straight forward, just work out (boardsize.x/2-Tx) and (boardsize.y/2-Ty) and (boardsize.z/2-Tz) and place the centre of the board there!

    But when I start rotating in all three axis, I cannot work out the position and the rotations of the World so that the 'camera' is positioned at Tx, Ty, Tz, with the correct angle.

    In NecroDome's thread previous to this, I saw that the way to do it seems to depend on the order you move, rotate and draw everything. However, in my program, i always translate, THEN rotate along the X, THEN rotate along the Y, THEN rotate along the Z axis, like in the code above.
    See?

    I have started working out equations with 2 or three variables to place the centre of the board. so I have Wx = f(RTx,RTy) and Wz = f(RTy,RTz), but while I don't get equations for Wy and for RWx, RWy and RWz, I can't test it!

    Im asking because Im sure many ppl have come across changing views like this, so maybe you have a made procedure to deal with it, or jst the formulae im needing...

    Well, enough said. If there is anything not clear, please let me know.

  2. #2

    3rd person view to 1st person view calculations

    Ok, it seems too challenging, or maybe not challenging enough...
    I doubt it be the second one, so to get it started, here is my progress so far:

    In the situation described above, when the view is switched to the tank's (camera at the tank's location and looking the same way the tank is facing) the centre of the board can be defined as follows:
    Wx = [ ( c + a ) / 2 + ( ( c - a ) / 2 ) cos(2RTx) ] sin[ angleUCX cos(RTx) + RTy] + [ (Ty) sin(RTx) ] cos(RTy)
    Wz = - [ a cos(RTy) - b sin(RTy) ] cos(RTz) + (Ty) sin(RTx)
    where
    a:= BoardSize.x/2 - Tx;
    b:= BoardSize.z/2 - Tz;
    c:= sqrt( sqr(a) + sqr(b) );
    angleUCX := arctan ( b/a );

    I am pretty confident about the above equations, even though I cannot certify them 100%.
    For the y coordinate, however, I could not work out a complete equation, but I did one only including RTx and RTy (no z rotation). It is as follows:
    Wy = b sin(RTx) - (Ty) cos(RTx).

    I have tested the previous for many combinations of rotations and displacements of the tank, and they seem to work.
    What I still have not managed, is 1) to get a full equation for Wy including RTz; 2) equations for RWx, RWy and RWz to suit the situation I described in the previous post.

    Hope this helps someone help me...!
    Any math genius out there

  3. #3

    3rd person view to 1st person view calculations

    I'm not sure whether I understand you correctly (I am not genius, so let me know if I am wrong). The problem is that you have world matrix for the board and world matrix for the tank and you need to obtain the position of the tank in world and its rotation, right?

    You can obtain world position from the matrix by inversing it and then extracting x, y, and z from the last column. The same applies for rotation since basically you want to rotate the world in reverse to tank rotation, so you see everything from the front of the tank.

    In Direct3D there is D3DXMatrixDecompose function, which can do the trick. Once I saw a link to a good article about how to do it manually (or extracting rotations in different order) on GameDev forums, but don't remember the link. A quick search came to this page, which may be helpful.

  4. #4

    3rd person view to 1st person view calculations

    Ok! I didn't really understand the bit of code you mentioned, but it gave me another idea about how to do it, so now it works!
    Thanks!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •