Sign in to follow this  
Six222

Camera rotation

Recommended Posts

I've been having issues working out how to rotate my camera with my mouse, now my math skills are still very basic and i've been following some tutorials but i've been having lots of issues... I've got my movement working with the WASD keys i can move forwards/backwards and strafe left and right. This Is my current rotation code:

[code]

void CCamera::Rotate()
{
D3DXVECTOR3 vDirection,vRotAxis;
D3DXMATRIX matRotAxis,matRotZ;

D3DXVec3Normalize(&vDirection,&(m_vLookAtPt - m_vEyePt));
D3DXVec3Cross(&vRotAxis, &vDirection, &m_vUp);
D3DXVec3Normalize(&vRotAxis, &vRotAxis);

D3DXMatrixRotationAxis(&matRotAxis, &vRotAxis, m_Input->GetMouseY() / -360.0f);
D3DXMatrixRotationZ(&matRotZ, m_Input->GetMouseX() / -360.0f);

D3DXVec3TransformCoord(&vDirection, &vDirection, &(matRotAxis * matRotZ));
D3DXVec3TransformCoord(&m_vUp, &m_vUp, &(matRotAxis * matRotZ));
m_vLookAtPt = vDirection + m_vEyePt;
}


void CCamera::Update(void)
{
Rotate();
Move();

D3DXMatrixLookAtLH(&m_matView, &m_vEyePt, &m_vLookAtPt, &m_vUp);
matFinal = m_matView * matProjection;
}
[/code]

Now this all works fine until i activate my mouse input... when i do this the whole camera decides to spin on the x and z axis loads and slows down when my mouse gets closer to (0,0). To be honest i'm completely clueless if anyone could give me some advice it'd be really appreciated!

Share this post


Link to post
Share on other sites
It's fairly simple if you imagine what's going on. You're currently getting the X/Y coordinates of the mouse, and using those to nudge your rotation matrix each frame. So naturally if your cursor is away from the origin, the values of its position act as the amount to increment your rotation by, whereas what you want is a comparison between the last known mouse position (from the previous frame) and the current mouse position and use that delta as your rotation increment.

So at the beginning of your function I'd create a pair of static integers and a pair of floats..


[code]

static int lastpos[2];
int newpos[2];
float inc[2];

newpos[0] = GetMouseX()
newpos[1] = GetMouseY();

inc[0] = newpos[0] - lastpos[0];
inc[1] = newpos[1] - lastpos[1];

//store current pos for next frame's calculations..
lastpos[0] = newpos[0];
lastpos[1] = newpos[1];

//may need to play with the 360 value to get what you want, 360 may be too large.
inc[0] /= 360;
inc[1] /= 360;

.... do your existing D3D matrix transforms here..

// replace your existing mouseinput lines with these:

D3DXMatrixRotationAxis(&matRotAxis, &vRotAxis, inc[1]);
D3DXMatrixRotationZ(&matRotZ, inc[0]);


[/code]

And that should make life beautiful so long as your actual rotation code is proper and correct. The key is measuring the relative change from the previous frame's mouse position and the current frame's mouse position, and using that to determine how to adjust your view rotation, instead of plugging in the absolute mouse position.

Share this post


Link to post
Share on other sites
Hey thanks for the reply.. i tried this method before (i read up on that you should store the previous mouse position), but i decided to do it again and it sort of works... But it's still giving me weird results the actual camera is spinning around a hell of a lot. This has led me to believe that my rotation code is wrong. But what i don't understand is that this code was pulled straight from a tutorial which i've even compiled and it works but it doesn't want to work in my application...


[url="http://www.two-kings.de/tutorials/basics/basics01.html"]http://www.two-kings.de/tutorials/basics/basics01.html[/url]

Share this post


Link to post
Share on other sites
In his tutorial he calls a function at the end of the camera update function that you're not calling g_App.GetDevice()->SetTransform(D3DTS_VIEW,&m_matView);, you need to set the view transform after calling your LookAt function. That might do the trick

Share this post


Link to post
Share on other sites
[quote name='radioteeth' timestamp='1311351848' post='4838993']
In his tutorial he calls a function at the end of the camera update function that you're not calling [color="#707070"]g_App.GetDevice()->SetTransform(D3DTS_VIEW,&m_matView);, you need to set the view transform after calling your LookAt function. That might do the trick[/color]
[/quote]

Ah I should mention i'm using DX11..

[code]
camera.Update();


CBUFFER cBuffer;
cBuffer.ViewProj = camera.GetViewProjection();

Renderer->GetDeviceContext()->UpdateSubresource(g_pConstantBuffer, 0, 0 , &cBuffer, 0, 0);

[/code]

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