Thread: Defining a plane using three points?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Thanks all for the responses.

    I did exactly what you said Bob partly from other sites and partly from just figuring it out myself from my knowledge of vector operations.

    Big problem. It seems that Euler orientations are not unique. In other words if my spaceship is point at a planet and my yaw and pitch are 0.0f and 0.0f that orientation is not unique. It is possible to yaw and pitch all over the place and point back at the planet...and the yaw and pitch are not 0.0f 0.0f. But the formula I'm using always calculates the yaw and pitch to be 0.0f.

    Here is what I'm doing:

    Code:
    void CCamera::RotateTo(D3DXVECTOR3 vecTarget)
    {
      //vecTarget must share origin with camera vector
      D3DXVECTOR3 toTarget=vecTarget-Pos;
      D3DXVec3Normalize(&toTarget,&toTarget);
      
      //Normalize camera look
      D3DXVECTOR3 nLook;
      D3DXVec3Normalize(&nLook,&Look);
      
      //Cross product is axis of rotation
      D3DXVec3Cross(&m_vecAxis,&toTarget,&nLook);
      
      //Dot product is cos of angle of rotation
      m_fAxisAngle=D3DXVec3Dot(&toTarget,&nLook);
        
      //Construct axis angle rotation matrix
      D3DXMATRIX T;
      D3DXMatrixRotationAxis(&T,&m_vecAxis,m_fAxisAngle);
      
      //Now extract yaw, pitch, roll from T and rotate
      YPRFromRotMatrix(T,m_vecTargYPR.x,m_vecTargYPR.y,m_vecTargYPR.z);
      
      //Find difference between current YPR and new YPR to face object
      D3DXVECTOR3 diff;
      diff.x=m_vecTargYPR.x-m_fYaw;
      diff.y=m_vecTargYPR.y-m_fPitch;
      diff.z=0.0f;  
      m_vecRotInc=diff;
       
      //Test code to 'snap' to new orientation
      //Yaw(m_vecTargYPR.x-m_fYaw);
      //Pitch(m_vecTargYPR.y-m_fPitch);
      
          
    }
    So you see I'm doing exactly what everyone has shown. My problem is this. How do I transform my up,look, and right vectors to represent the new rotation? If the up, right, and look vectors are not altered in my camera class there is no rotation. I can create a matrix to do the rotation but I'm not sure what to do then?

    Code:
    D3DXVec3TransformCoord(&Up,&Up,&NewMatrix);
    D3DXVec3TransformCoord(&Look,&Look,&NewMatrix);
    D3DXVec3TransformCoord(&Right,&Right,&NewMatrix);
    Since I'm not sure this works what I did was create the new axis-angle rotation matrix and then proceed to extract the yaw and pitch from the matrix. Roll is pretty much a moot point in this so I don't use it.

    Using this the spacecraft does not always point at the same place. So when I want to turn to 0.0f,0.0f,0.0f it should be the same exact spot on the skysphere. It's not and often times it's way off.

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    43
    The yaw and pitch values should be some multiple of PI. Use mod, (angle % PI), before comparing them with zero. Observe that 180 degree yaw and 180 degree pitch is the same os 0 degree yaw and 0 degree pitch, only difference is that the roll angle is flipped.

    also observe that you need to apply yaw, pitch, roll transforms in the same order in your code to get consistent results.

    Don't you want roll angle too, since it's a space simulator and not just a ground based 3d game where up is always up?

    Be wary of taking code snippets from sites that talk about FPS, since Euler angles works fine as long as you don't look at the same direction as the up-vector (if you play Quake and look straight up/down I guess you'll see the screen start to spin, and you don't want that in a flight simulator). This has to do with the fixed up-vector, you need to have a matrix that describes the orientation of the camera so you can get your up-vector perpendicular with the look-at vector.

    I think you'll be much happier if you change to using quaternions, you will get much cleaner code that way in the end (and it's not difficult if you are comfortable with matrices and vectors to begin with).

    Yaw, pitch and roll is nice to use as input parameters but they suck when you use them between calculations (Euler angles contain singularities at the poles where the math breaks down). Though it is possible to do camera motion using Euler angles (I once did) but it's no fun.

    Look for code for generating a rotation matrix for rotating one vector to another. Then multiply the current camera orientation matrix with that rotation matrix and you should get the matrix that describes the new orientation. Order of matrix multiplies matter, it's not like scalar math.

    enough of my ranting...good luck =)

    /f

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Yahtzee C++ programme help
    By kenneth_888 in forum C++ Programming
    Replies: 13
    Last Post: 09-05-2007, 02:14 PM
  2. CProg Fantasy Football version pi
    By Govtcheez in forum A Brief History of Cprogramming.com
    Replies: 155
    Last Post: 12-26-2006, 04:30 PM
  3. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM