• Create Account

# NatalieBerry88

Member Since 25 Nov 2011
Offline Last Active Dec 02 2011 02:28 PM

### In Topic: 3d Open GL Game - How to fix a object to the camera =)

25 November 2011 - 10:18 PM

I found some other code that helped:

```void CCamera::RotateAroundPoint(CVector3 vCenter, float angle, float x, float y, float z)
{
CVector3 vNewPosition;

// To rotate our position around a point, what we need to do is find
// a vector from our position to the center point we will be rotating around.
// Once we get this vector, then we rotate it along the specified axis with
// the specified degree.  Finally the new vector is added center point that we
// rotated around (vCenter) to become our new position.  That's all it takes.

// Get the vVector from our position to the center we are rotating around
CVector3 vPos = m_vPosition - vCenter;

// Calculate the sine and cosine of the angle once
float cosTheta = (float)cos(angle);
float sinTheta = (float)sin(angle);

// Find the new x position for the new rotated point
vNewPosition.x  = (cosTheta + (1 - cosTheta) * x * x)		* vPos.x;
vNewPosition.x += ((1 - cosTheta) * x * y - z * sinTheta)	* vPos.y;
vNewPosition.x += ((1 - cosTheta) * x * z + y * sinTheta)	* vPos.z;

// Find the new y position for the new rotated point
vNewPosition.y  = ((1 - cosTheta) * x * y + z * sinTheta)	* vPos.x;
vNewPosition.y += (cosTheta + (1 - cosTheta) * y * y)		* vPos.y;
vNewPosition.y += ((1 - cosTheta) * y * z - x * sinTheta)	* vPos.z;

// Find the new z position for the new rotated point
vNewPosition.z  = ((1 - cosTheta) * x * z - y * sinTheta)	* vPos.x;
vNewPosition.z += ((1 - cosTheta) * y * z + x * sinTheta)	* vPos.y;
vNewPosition.z += (cosTheta + (1 - cosTheta) * z * z)		* vPos.z;

// Now we just add the newly rotated vector to our position to set
// our new rotated position of our camera.
m_vPosition = vCenter + vNewPosition;
}```

Then in the main.cpp I added the key states:

```// We made 2 changes from the Camera2 tutorial.  Instead of gCamera.RotateView()
// we use our new RotateAroundPoint() function.  We pass in the camera's view
// point.  This will be the point that we rotate our camera position around.

if(GetKeyState(VK_LEFT) & 0x80) {			// If we hit the LEFT arrow key

// We want to rotate around the Y axis so we pass in a positive Y speed
g_Camera.RotateAroundPoint(g_Camera.m_vView, kSpeed, 0, 1, 0);
}

if(GetKeyState(VK_RIGHT) & 0x80) {			// If we hit the RIGHT arrow key

// Use a negative Y speed to rotate around the Y axis
g_Camera.RotateAroundPoint(g_Camera.m_vView, -kSpeed, 0, 1, 0);
}```

The only problem is that I added in the RenderScene() function
``` gPlayer.DrawPlayer();
glTranslatef(g_Camera.m_vView.x, 0, g_Camera.m_vView.z);```

However as you see above I have a player class, so I'm not so sure how to point glTranslatef to only the player.. at the moment it compiles but the whole world turns around weirdly

Netalie xxx

PARTNERS