Camera question
i''m trying to get my camera to spin around a flat grass plain...so far i''ve tried using gluLookAt with very little success...could anyone suggest a way to do it, so that the camera is focused right on the middle of the rectangle and looking down from a slightly elevated position...
thanks to anyone who bothers to reply
Velio
P.S. this probably isn''t the best place to post this but does anyone know how to play mp3s from their own program?
Velio
Rotate camera position around the lookAtPos. (Using rotation matrix or do sin/cos stuff yourself)
After rotation, do LookAt( cameraPos, lookAtPos ).
After rotation, do LookAt( cameraPos, lookAtPos ).
Here''s code:
// InitializationrotationCenterPos = Vec(0,0,0);cameraPos = Vec(0,50,100); // Slighly elevated (50) and in front of (100).// Animationwhile(...){ cameraPos -= rotationCenterPos; MakeYRotationMatrix( &m, PI/180 ); // rotate 1 degree every frame. VecMatMul( cameraPos, &m ); cameraPos += rotationCenterPos; MakeViewMatrix( &viewMatrix, &cameraPos, &rotationCenterPos );}
Here is the code I wrote for this problem, I cheat a little by using gluLookAt but that just encapsulates a few Translations/Rotations:
c_fCameraLookAt[0] = c_fBoat->dcore.b_fCrewPos[0] + c_fBoat->dcore.b_fBoatPos[0];
c_fCameraLookAt[1] = c_fBoat->dcore.b_fCrewPos[1] + c_fBoat->dcore.b_fBoatPos[1];
c_fCameraLookAt[2] = c_fBoat->dcore.b_fCrewPos[2] + c_fBoat->dcore.b_fBoatPos[2];
// Shift Back to Origin Coords
c_fCameraPos[0] -= c_fCameraLookAt[0];
c_fCameraPos[1] -= c_fCameraLookAt[1];
c_fCameraPos[2] -= c_fCameraLookAt[2];
// Compute Rotation around Origin
c_fCameraPos[0] = // 0
0.0 -
c_fCameraOffset * sin(c_fCameraAngle) *
cos(c_fCameraElevation);
c_fCameraPos[1] = // 1
0.0 -
c_fCameraOffset * sin(c_fCameraElevation);
c_fCameraPos[2] = // 2
0.0 +
c_fCameraOffset * cos(c_fCameraAngle) *
cos(c_fCameraElevation);
// Shift Back to World Coords
c_fCameraPos[0] += c_fCameraLookAt[0];
c_fCameraPos[1] += c_fCameraLookAt[1];
c_fCameraPos[2] += c_fCameraLookAt[2];
if(!c_fFPerson) // c_fFPerson is tru if Camera is in First Person view
{
gluLookAt(c_fCameraPos[0], // 2
c_fCameraPos[1], // 0
c_fCameraPos[2], // 1
//
c_fCameraLookAt[0],// 0
c_fCameraLookAt[1],// 1
c_fCameraLookAt[2],// 2
//
0,0,1);
}
if(c_fFPerson) // Third Person
{
gluLookAt(c_fCameraLookAt[0],// 0
c_fCameraLookAt[1],// 1
c_fCameraLookAt[2],// 2
//
c_fCameraPos[0], // 2
c_fCameraPos[1], // 0
c_fCameraPos[2], // 1
//
0,0,1);
}
Also Does anyone know how people post code on these forums that looks coloured etc.. (Like in VC++)
A little Explanation! The above code is called to update the camera for every frame. It is used to Follow a boat on a race-coarse (it points at the boats crew). The crew and boat positions are updated every frame by the Dynamics solver, the camera just uses these coords to point at. To be able to follow and rotate around any boat on the coarse I compute all rotations around the z-axis at the origin (0,0,1) and translate the rotation back out to world coords. To jump between First and Third person views I just swap the the Lookat points passed to gluLookAt (A bit of a hack but it works and looks great)
I hope this makes sense because it`s 3:45am and I`m falling asleep, goodnight.
c_fCameraLookAt[0] = c_fBoat->dcore.b_fCrewPos[0] + c_fBoat->dcore.b_fBoatPos[0];
c_fCameraLookAt[1] = c_fBoat->dcore.b_fCrewPos[1] + c_fBoat->dcore.b_fBoatPos[1];
c_fCameraLookAt[2] = c_fBoat->dcore.b_fCrewPos[2] + c_fBoat->dcore.b_fBoatPos[2];
// Shift Back to Origin Coords
c_fCameraPos[0] -= c_fCameraLookAt[0];
c_fCameraPos[1] -= c_fCameraLookAt[1];
c_fCameraPos[2] -= c_fCameraLookAt[2];
// Compute Rotation around Origin
c_fCameraPos[0] = // 0
0.0 -
c_fCameraOffset * sin(c_fCameraAngle) *
cos(c_fCameraElevation);
c_fCameraPos[1] = // 1
0.0 -
c_fCameraOffset * sin(c_fCameraElevation);
c_fCameraPos[2] = // 2
0.0 +
c_fCameraOffset * cos(c_fCameraAngle) *
cos(c_fCameraElevation);
// Shift Back to World Coords
c_fCameraPos[0] += c_fCameraLookAt[0];
c_fCameraPos[1] += c_fCameraLookAt[1];
c_fCameraPos[2] += c_fCameraLookAt[2];
if(!c_fFPerson) // c_fFPerson is tru if Camera is in First Person view
{
gluLookAt(c_fCameraPos[0], // 2
c_fCameraPos[1], // 0
c_fCameraPos[2], // 1
//
c_fCameraLookAt[0],// 0
c_fCameraLookAt[1],// 1
c_fCameraLookAt[2],// 2
//
0,0,1);
}
if(c_fFPerson) // Third Person
{
gluLookAt(c_fCameraLookAt[0],// 0
c_fCameraLookAt[1],// 1
c_fCameraLookAt[2],// 2
//
c_fCameraPos[0], // 2
c_fCameraPos[1], // 0
c_fCameraPos[2], // 1
//
0,0,1);
}
Also Does anyone know how people post code on these forums that looks coloured etc.. (Like in VC++)
A little Explanation! The above code is called to update the camera for every frame. It is used to Follow a boat on a race-coarse (it points at the boats crew). The crew and boat positions are updated every frame by the Dynamics solver, the camera just uses these coords to point at. To be able to follow and rotate around any boat on the coarse I compute all rotations around the z-axis at the origin (0,0,1) and translate the rotation back out to world coords. To jump between First and Third person views I just swap the the Lookat points passed to gluLookAt (A bit of a hack but it works and looks great)
I hope this makes sense because it`s 3:45am and I`m falling asleep, goodnight.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement