# rotate camera sin and cosine

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

## Recommended Posts

Hy. I find this code that not understand: void CCamera::RotateCamera(float AngleDir, float xSpeed, float ySpeed, float zSpeed) { float xNewLookDirection = 0, yNewLookDirection = 0, zNewLookDirection = 0; float xLookDirection = 0, yLookDirection = 0, zLookDirection = 0; float CosineAngle = 0, SineAngle = 0; // First we will need to calculate the cos and sine of our angle. I creaetd two macros to // do this in the CCamera.h header file called GET_COS and GET_SINE. To use the macros // we just send in the variable we ant to store the results and the angle we need to // calculate. CosineAngle = (float)cos(AngleDir); SineAngle = (float)sin(AngleDir); // Next get the look direction (where we are looking) just like in the move camera function. xLookDirection = xView - xPos; yLookDirection = yView - yPos; zLookDirection = zView - zPos; // Normalize the direction. float dp = 1 /(float)sqrt(xLookDirection * xLookDirection + yLookDirection * yLookDirection + zLookDirection * zLookDirection); xLookDirection *= dp; yLookDirection *= dp; zLookDirection *= dp; // Calculate the new X position. xNewLookDirection = (CosineAngle + (1 - CosineAngle) * xSpeed) * xLookDirection; xNewLookDirection += ((1 - CosineAngle) * xSpeed * ySpeed - zSpeed * SineAngle)* yLookDirection; xNewLookDirection += ((1 - CosineAngle) * xSpeed * zSpeed + ySpeed * SineAngle) * zLookDirection; // Calculate the new Y position. yNewLookDirection = ((1 - CosineAngle) * xSpeed * ySpeed + zSpeed * SineAngle) * xLookDirection; yNewLookDirection += (CosineAngle + (1 - CosineAngle) * ySpeed) * yLookDirection; yNewLookDirection += ((1 - CosineAngle) * ySpeed * zSpeed - xSpeed * SineAngle) * zLookDirection; // Calculate the new Z position. zNewLookDirection = ((1 - CosineAngle) * xSpeed * zSpeed - ySpeed * SineAngle) * xLookDirection; zNewLookDirection += ((1 - CosineAngle) * ySpeed * zSpeed + xSpeed * SineAngle) * yLookDirection; zNewLookDirection += (CosineAngle + (1 - CosineAngle) * zSpeed) * zLookDirection; // Last we add the new rotations to the old view to correctly rotate the camera. xView = xPos + xNewLookDirection; yView = yPos + yNewLookDirection; zView = zPos + zNewLookDirection; } what do for sample : // Calculate the new X position. xNewLookDirection = (CosineAngle + (1 - CosineAngle) * xSpeed) * xLookDirection; ? thanks

##### Share on other sites
Well, first, let's rewrite that code to something a little more readable:
void CCamera::RotateCamera(float AngleDir, const Vector3& speed){    float c = (float)cos(AngleDir);    float s = (float)sin(AngleDir);    // Next get the look direction (where we are looking) just like in the move camera function.    Vector3 lookDirection = normalize(m_view - m_pos);    Matrix rotation(	speed.x * (1 - c) + c, speed.x * speed.y * (1 - c) - speed.z * s, speed.x * speed.z * (1 - c) + speed.y * s, 0,	speed.x * speed.y * (1 - c) + speed.z * s, speed.y * (1 - c) + c, speed.y * speed.z * (1 - c) - speed.x * s, 0,	speed.x * speed.z * (1 - c) - speed.y * s, speed.y * speed.z * (1 - c) + speed.x * s, speed.z * (1 - c) + c, 0,	0, 0, 0, 1    );    // Last we add the new rotations to the old view to correctly rotate the camera    Vector3 newLookDirection = lookDirection * rotation;    m_view = m_pos + newLookDirection;}

Class member variables (view and pos) are now prefixed with m_, those ugly x,y,z triples combined into the Vector3 class, normalize stuffed in its own function, and the cosine and sine shortened to c and s.

But most importantly, all those additions and multiplications have been placed in a matrix. And this is the 'magic' part. This is a rotation matrix, which can be applied to (multiplied with) vectors to rotate them in a certain direction. Read the wikipedia article for more information. You'll recognize the matrix as the last matrix from the equation before the section "Skew parameters".

Note, however, that this implementation is wrong. On the diagonal, the squares of x, y and z should be used.

• 48
• 12
• 10
• 10
• 9
• ### Forum Statistics

• Total Topics
631378
• Total Posts
2999665
×