Alsalam alikom wa ra7mat allah wa barakatoh

Here is some explaination about how to rotate the camera just like the plane rotates...

We have Up Vector and a View Vector, to make the camera rotates right, we rotate the view vector about 0,1,0 vector with the angle we wish...

RotateView(-0.1f, 0, 1, 0);

RotateView will just calculate the new viewVector after multiplying it by the 3D Rotation matrix...

This is just regular,

Now how to make the the camera (the plane) rotates, we will simply rotate the upVector around the view vector but here will arise a problem that when we continue rotating right, the upVector will just miss the whole thing... mm try to imagine it.. we rotated it about a vector (view) then this view vector changed its direction, then we continue rotating it about the new view... blah blah.. just a total miss..

Actually, I don't know the professional way to solve it, but i got two good ideas (in my opinion), first one is whenever we rotate the view, we rotate the upVector about 0,1,0 with the same angle we rotated the view with... this will keep the upVector always perpendicular to the view vector and just looking rotated..

Second one is whenever we rotate the viewVector, we reset the upVector to 0,1,0 then rotate it with the total angle... instead of stepping each time 0.1 or whatever....

Both solutions r similar.. i'm attaching the code of the first one..

void CCamera::RotateView(float angle, float x, float y, float z)

{

CVector3 vNewView;

CVector3 vView = m_vView - m_vPosition;

float cosTheta = (float)cos(angle);

float sinTheta = (float)sin(angle);

vNewView.x = (cosTheta + (1 - cosTheta) * x * x) * vView.x;

vNewView.x += ((1 - cosTheta) * x * y - z * sinTheta) * vView.y;

vNewView.x += ((1 - cosTheta) * x * z + y * sinTheta) * vView.z;

vNewView.y = ((1 - cosTheta) * x * y + z * sinTheta) * vView.x;

vNewView.y += (cosTheta + (1 - cosTheta) * y * y) * vView.y;

vNewView.y += ((1 - cosTheta) * y * z - x * sinTheta) * vView.z;

vNewView.z = ((1 - cosTheta) * x * z - y * sinTheta) * vView.x;

vNewView.z += ((1 - cosTheta) * y * z + x * sinTheta) * vView.y;

vNewView.z += (cosTheta + (1 - cosTheta) * z * z) * vView.z;

m_vView = m_vPosition + vNewView;

// This part will rotate the upVector about 0,1,0 with the same rotation angle of the viewVector

RotateUp(angle, 0, 1, 0);

}

void CCamera::RotateUp(float angle, float x, float y, float z)

{

CVector3 vNewView;

CVector3 vUp = m_vUpVector;

float cosTheta = (float)cos(angle);

float sinTheta = (float)sin(angle);

vNewView.x = (cosTheta + (1 - cosTheta) * x * x) * vUp.x;

vNewView.x += ((1 - cosTheta) * x * y - z * sinTheta) * vUp.y;

vNewView.x += ((1 - cosTheta) * x * z + y * sinTheta) * vUp.z;

vNewView.y = ((1 - cosTheta) * x * y + z * sinTheta) * vUp.x;

vNewView.y += (cosTheta + (1 - cosTheta) * y * y) * vUp.y;

vNewView.y += ((1 - cosTheta) * y * z - x * sinTheta) * vUp.z;

vNewView.z = ((1 - cosTheta) * x * z - y * sinTheta) * vUp.x;

vNewView.z += ((1 - cosTheta) * y * z + x * sinTheta) * vUp.y;

vNewView.z += (cosTheta + (1 - cosTheta) * z * z) * vUp.z;

m_vUpVector = vNewView;

}

* Note : the code of multiplying with the matrix is not mine, i copied it (as it's) from GameTutorials

- Now, u can simply call (in ur VK_RIGHT) something like this

RotateView(-0.1f, 0, 1, 0);

CVector3 vAxis = m_vView - m_vPosition;

vAxis = Normalize(vAxis);

RotateUp(0.1f, vAxis.x, vAxis.y, vAxis.z);

This will give u the desidered effect, of course u can play with -0.1f and 0.1f values to make it looks better, or make it depend on ur straight-forward speed... but this is another story :)...

Hope u (me :D) enjoy it..

Alsalam alikom wa ra7mat allah wa barakatoh

Here is some explaination about how to rotate the camera just like the plane rotates...

We have Up Vector and a View Vector, to make the camera rotates right, we rotate the view vector about 0,1,0 vector with the angle we wish...

RotateView(-0.1f, 0, 1, 0);

RotateView will just calculate the new viewVector after multiplying it by the 3D Rotation matrix...

This is just regular,

Now how to make the the camera (the plane) rotates, we will simply rotate the upVector around the view vector but here will arise a problem that when we continue rotating right, the upVector will just miss the whole thing... mm try to imagine it.. we rotated it about a vector (view) then this view vector changed its direction, then we continue rotating it about the new view... blah blah.. just a total miss..

Actually, I don't know the professional way to solve it, but i got two good ideas (in my opinion), first one is whenever we rotate the view, we rotate the upVector about 0,1,0 with the same angle we rotated the view with... this will keep the upVector always perpendicular to the view vector and just looking rotated..

Second one is whenever we rotate the viewVector, we reset the upVector to 0,1,0 then rotate it with the total angle... instead of stepping each time 0.1 or whatever....

Both solutions r similar.. i'm attaching the code of the first one..

void CCamera::RotateView(float angle, float x, float y, float z)

{

CVector3 vNewView;

CVector3 vView = m_vView - m_vPosition;

float cosTheta = (float)cos(angle);

float sinTheta = (float)sin(angle);

vNewView.x = (cosTheta + (1 - cosTheta) * x * x) * vView.x;

vNewView.x += ((1 - cosTheta) * x * y - z * sinTheta) * vView.y;

vNewView.x += ((1 - cosTheta) * x * z + y * sinTheta) * vView.z;

vNewView.y = ((1 - cosTheta) * x * y + z * sinTheta) * vView.x;

vNewView.y += (cosTheta + (1 - cosTheta) * y * y) * vView.y;

vNewView.y += ((1 - cosTheta) * y * z - x * sinTheta) * vView.z;

vNewView.z = ((1 - cosTheta) * x * z - y * sinTheta) * vView.x;

vNewView.z += ((1 - cosTheta) * y * z + x * sinTheta) * vView.y;

vNewView.z += (cosTheta + (1 - cosTheta) * z * z) * vView.z;

m_vView = m_vPosition + vNewView;

// This part will rotate the upVector about 0,1,0 with the same rotation angle of the viewVector

RotateUp(angle, 0, 1, 0);

}

void CCamera::RotateUp(float angle, float x, float y, float z)

{

CVector3 vNewView;

CVector3 vUp = m_vUpVector;

float cosTheta = (float)cos(angle);

float sinTheta = (float)sin(angle);

vNewView.x = (cosTheta + (1 - cosTheta) * x * x) * vUp.x;

vNewView.x += ((1 - cosTheta) * x * y - z * sinTheta) * vUp.y;

vNewView.x += ((1 - cosTheta) * x * z + y * sinTheta) * vUp.z;

vNewView.y = ((1 - cosTheta) * x * y + z * sinTheta) * vUp.x;

vNewView.y += (cosTheta + (1 - cosTheta) * y * y) * vUp.y;

vNewView.y += ((1 - cosTheta) * y * z - x * sinTheta) * vUp.z;

vNewView.z = ((1 - cosTheta) * x * z - y * sinTheta) * vUp.x;

vNewView.z += ((1 - cosTheta) * y * z + x * sinTheta) * vUp.y;

vNewView.z += (cosTheta + (1 - cosTheta) * z * z) * vUp.z;

m_vUpVector = vNewView;

}

* Note : the code of multiplying with the matrix is not mine, i copied it (as it's) from GameTutorials

- Now, u can simply call (in ur VK_RIGHT) something like this

RotateView(-0.1f, 0, 1, 0);

CVector3 vAxis = m_vView - m_vPosition;

vAxis = Normalize(vAxis);

RotateUp(0.1f, vAxis.x, vAxis.y, vAxis.z);

This will give u the desidered effect, of course u can play with -0.1f and 0.1f values to make it looks better, or make it depend on ur straight-forward speed... but this is another story :)...

Hope u (me :D) enjoy it..

Alsalam alikom wa ra7mat allah wa barakatoh

## Comments

## Post a Comment