Jump to content
  • Advertisement
Sign in to follow this  
hufeilxl

question about camera matrix

This topic is 4558 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I used 'D3DXQuaternionRotationYawPitchRoll' to build a quaternion matrix, and first step is Calculatesing the inverse of the quaternion matrix (D3DXMatrixInverserotation), the second step is use zhe result to rotate my camera. but zhe effect is non good , because i cann't use mouse to control camera turn top left corner and top right corner. maybe the quaternion matrix is not fit for the first person shoot game. someone says quaternion matrix is fit for zhe third person shoot game, but I don't know the method. my thought is buliding two camera ,one is for the first person shoot game the other is for zhe third person shoot game. How to do ? help me please!

Share this post


Link to post
Share on other sites
Advertisement
There is something missing: Quaternions are _not_ the same as matrices. In fact a special kind of quaternions (namely the unit quaternions) is useable to describe a rotation, and also a special kind of matrices (namely those with orthonormal basis vectors) is useable for describing rotations.

The function D3DXQuaternionRotationYawPitchRoll computes three Quaternions in order from the 3 Euler angles yaw, pitch, and roll, and concatenate them to a single Quaternion. A function D3DXMatrixInverseRotation seems me not to exist at all (at least the MSDN search doesn't found something like that). The function D3DXMatrixInverse exists but expects a matrix (not a Quaternion) for input.

Both Quaternions as well as rotation matrices could be used in principle for 1st person as well as for 3rd person views.

Share this post


Link to post
Share on other sites
Here is how I always do it (note that playerPosition is just a structure containing the player's position and associated velocities):

// Update player position
playerPosition.yaw += playerPosition.yawVel;
playerPosition.pitch += playerPosition.pitchVel;

// Normalize and add (this allows you to move in the direction that you are pointing)
D3DXVECTOR3 cameraVel;
D3DXVec3TransformNormal( &cameraVel, &playerPosition.vel, &matOrientation );
playerPosition.pos += cameraVel;

// Use quaternions to rotate the camera and avoid gimble lock
D3DXQUATERNION quat;
D3DXQuaternionRotationYawPitchRoll( &quat, playerPosition.yaw, playerPosition.pitch, playerPosition.roll );
D3DXMatrixAffineTransformation( &matOrientation, 1.25f, NULL, &quat, &playerPosition.pos );
D3DXMatrixInverse( &matView, NULL, &matOrientation );


For future reference, you may only bump a post after 24 hours of inactivity.

Share this post


Link to post
Share on other sites
Than you for replies.

I read some books about Direct3D , it says if build a 1st person camera ,
must use spherical coordinate system, it is really ?

another question ,how to bulid a camera like 'World of Warcraft' ? Does it use
quaternions ?

Share this post


Link to post
Share on other sites
It is vastly easier (and more stable) to build a first person camera using yaw and pitch relative to the character, as you will never want to roll a first-person camera (relative to the character). Yaw and pitch together just represent a rotation, so expressing them as a unit quaternion is a fine way to do it.

To build a full camera, you actually need to generate both projection and view matrices (in D3D), or projection and half of the modelview matrix (in OpenGL). The parameters you'll want to have available for your camera are:


Vector3 position;
Quaternion orientation;
float fov;
float aspect;
float nearClip;
float farClip;


Using these parameters, you can generate any traditional camera. You generate the view matrix by translating by -position, and then rotating by -orientation. You generate the projection matrix by passing fov, aspect, nearClip and farClip to the projection matrix function. For an example of a camera effect in the projection matrix: To zoom, you narrow the fov.

Here is my code for deriving the view matrix of my camera, using Managed DirectX (so you'll have to translate to C++):


public Matrix ViewMatrix {
get {
if( viewDirty_ ) {
Vector3 pos = -pos_;
Quaternion ori = ori_;
ori.Invert();
viewMatrix_ = Matrix.Translation( pos ) * Matrix.RotationQuaternion( ori );
viewDirty_ = false;
}
return viewMatrix_;
}
}

public void SetPosOri( Vector3 pos, Quaternion ori )
{
pos_ = pos;
ori_ = ori;
viewDirty_ = true;
}

public void SetPosYawPitch( Vector3 pos, float yaw, float pitch )
{
SetPosOri( pos, Quaternion.RotationYawPitchRoll( yaw, pitch, 0 ) );
}

Share this post


Link to post
Share on other sites
Thank you for your reply !
Tell the truth ,I cann't understand your code now ,but I guess your code was able to bulid a good view matrix for 1st person game .

my bigget doubt is that how to use mouse to control orientation of camera,
and how to control angle of Pitch . those are my bigget problem.

Share this post


Link to post
Share on other sites
Typically, the way it's done is to map the mouse input:

On the X axis, map the input movement to the yaw. Wrap the yaw in the range [-pi..pi] (which is -180 to +180 degrees).

On the Y axis, mape the input movement to the pitch. Clamp the pitch in the range (-pi/2..pi/2), or slightly less (say, -80 to +80 degrees).

Last, make sure that you re-set the cursor to the center of your window each time you read it (without counting the mouse movement event for that re-setting), so that the user can just keep moving the mouse without hitting the edge.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!