Sign in to follow this  
zahidayar

easy camera rotation

Recommended Posts

hello, how can i rotate the first person view when i press the right or left keys on the keyboard? currently this is the code im using but it doesnt do well after rotating really far left or right: else if ( e.KeyCode == Keys.Left ) this.cameraTarget.X--; else if ( e.KeyCode == Keys.Right ) this.cameraTarget.X++; the code basically changes the camera target of the device's view transform: device.Transform.View = Matrix.LookAtLH( this.cameraLocation , this.cameraTarget , new Vector3(0, 1, 0)); any suggestions would be appreciated... thanks for your time, Zahid [Edited by - zahidayar on May 24, 2006 3:12:31 PM]

Share this post


Link to post
Share on other sites
Here's the code I'm using right now that takes the relative mouse input from DirectInput, but the process can use any input.

void SimCamera::Rotate(SimInput *p_input)
{
D3DXVECTOR3 vDirection, vRotAxis;
D3DXMATRIX matRotAxis, matRotZ;

D3DXVec3Normalize(&vDirection, &(look - eye));
D3DXVec3Cross(&vRotAxis, &up, &vDirection);
D3DXVec3Normalize(&vRotAxis, &vRotAxis);

D3DXMatrixRotationAxis(&matRotAxis, &vRotAxis, p_input->GetRelativeY() / 200);
D3DXMatrixRotationY(&matRotZ, p_input->GetRelativeX() / 200);

D3DXVec3TransformCoord(&vDirection, &vDirection, &(matRotAxis * matRotZ));
look = eye + vDirection;
}

Share this post


Link to post
Share on other sites
thanks a lot for the help Nick!

i was able to successfully rotate the camera along the Y axis by translating your code to c#:

else if ( e.KeyCode == Keys.Left )
{
Vector3 movementDirectionVector = Vector3.Normalize ( this.cameraTarget - this.cameraLocation );
Vector3 rotationAxisVector = Vector3.Cross ( this.cameraUpOrientation , movementDirectionVector );
rotationAxisVector = Vector3.Normalize ( rotationAxisVector );

Matrix rotationMatrix = Matrix.RotationY ( -.07F );
movementDirectionVector = Vector3.TransformCoordinate ( movementDirectionVector , rotationMatrix );
this.cameraTarget = this.cameraLocation + movementDirectionVector;

}

else if ( e.KeyCode == Keys.Right )
{
Vector3 movementDirectionVector = Vector3.Normalize ( this.cameraTarget - this.cameraLocation );
Vector3 rotationAxisVector = Vector3.Cross ( this.cameraUpOrientation , movementDirectionVector );
rotationAxisVector = Vector3.Normalize ( rotationAxisVector );

Matrix rotationMatrix = Matrix.RotationY ( .07F );
movementDirectionVector = Vector3.TransformCoordinate ( movementDirectionVector , rotationMatrix );
this.cameraTarget = this.cameraLocation + movementDirectionVector;
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this