Sign in to follow this  
AfroFire

Camera Idea

Recommended Posts

Hey Gamedevers - Getting ready to go to bed and I was thinking about creating a camera class - Thing is, I've never done it before, and havn't researched it much. However, since I am going to bed right now, I thought I'd post this idea that I have in order to make a camera and see what you guys think about it when I wake up. If I am just totally missing something, feel free to point it out and point out some recommendation :) Anyways, the basic summary of my incomplete idea is this: Imagine a sphere of vertices around the player, now imagine that each vertex is a point that can be given to gluLookAt(...) in order to move the camera. So if the player moves the mouse to the left, it moves to the points to the left, and if the camera is moved right, it looks at the points to the right. Here is a sphere I generated using sin/cos awhile back, it gives a vague idea of what im trying to aim for: Once more just for my tired mind to reiterate, each of those vertexes on the "sphere" is a point that gluLookAt can point to in order to move the viewport around. Anyone, hope you understand, very tired, I want to sleeeeep. Thank you muchos.

Share this post


Link to post
Share on other sites
Is this camera for a first-person or third-person game? If it's the former, then I think it's quite OK, but you have to totally revise it if it's the latter (in that case you must move the camera around the player so the 'center' will remain same while the 'eye' changes position).

I'm guessing that all the sin/cos stuff was to make your idea clear - if it was then it's all good. But I just want to point out here that the actual implementation would be very easy if you just keep track of the player's position and direction with vector's. Then the vector you pass for gluLookAt's 'center' coordinates would be player.position + player.direction * factor, 'eye' would be player.position ... and you'd be done!

Share this post


Link to post
Share on other sites
Lol, to be honest - I had never looked at the gluLookAt function before so I didn't know what was required to make it point at a particular position. However, that looks to be very easy :)


What about when I push the "moveForward" button, Won't I have to use sin/cos to give what direction is really forward?

Share this post


Link to post
Share on other sites
I'm not too fond of the idea, really. It seems like you're restricting your camera rotation to an arbitrary grid, that you then have to store somewhere.

I find it a lot easier to store camera rotation as Euler angles, and just update them as the camera changes orientation. Every time they change, create a new view rotation matrix (yaw-pitch-roll order works well). Multiply by the camera-space basis, and extract your forward, right, and up vectors. I clamp the pitch to [-89°,89°], which seems reasonable for most games (except maybe flight simulators).

Share this post


Link to post
Share on other sites
Going ahead with my vote of using vectors, the only thing you need to do is create a functions that 'rotates' your vector about an arbitrary axis. You could do this using quaternions or matrices, I have such a function of my own - if you care for it just holler.

When the user moves the mouse, rotate the player.direction vector, and your simple camera class will take care of the rest. When he moves forward, player.position += player.direction * dt, that's it ... and you update the camera position and direction as I mentioned in my first post in this thread.

I don't want you to get biased or anything - this is just the way I do it, but I can guarantee you this - it works and it's pretty easy.

Share this post


Link to post
Share on other sites
To elaborate on what we discussed last night --

A good compromise is to store the 4 primary vectors of the camera -- look, up, right, and position. (Right can and will need to be regenerated every time these vectors change. Also, the look vector is not the look-at vector.) Then you can very easily build the matrix and pass it to OGL. Actual manipulation of the camera involves a number of matrix constructions and rotations, mainly constructing matrices to rotate the primary vectors around axes. You can have relative and non-relative rotations around world, local, and arbitrary axes very easily this way, along with absolute and relative movement.

Share this post


Link to post
Share on other sites
Here's my camera code from a GLUT based app (not a game, more like a 3D editor)

void SetLookAt() {
// do orbit camera
vEye.x = fEyeRadius * cosf(fLat) * cosf(fLon);
vEye.y = fEyeRadius * cosf(fLat) * sinf(fLon);
vEye.z = fEyeRadius * sinf(fLat);
glLoadIdentity();
gluLookAt(vEye.x, vEye.y, vEye.z, 0, 0, 0, 0, 0, -1);
}


'fLat' and 'fLon' are updated as the mouse moves around the screen with the middle mouse button held down (using
glutMotionFunc). fLat is constrained to the range (-0.75, 0.75). The 'up' axis is along the negative z-axis, the object being looked at is always at (0, 0, 0).

It's essentially the same as your method except I calculate the vector on the fly. 'sin' and 'cos' aren't so expensive that it's worth precalculating and storing them for results I need at most 60 times a second.

Share this post


Link to post
Share on other sites
This is how i do it in my application

void CameraRotation(D3DXMATRIX Final,float rx,float ry,float rz,
D3DXVECTOR3& right,D3DXVECTOR3& up,D3DXVECTOR3& look,D3DXVECTOR3& position)
{
D3DXMATRIX T;
D3DXMATRIX T2;
D3DXMATRIX T3;
D3DXMatrixRotation(&T,D3DXToRadian(x));
D3DXMatrixRotation(&T2,D3DXToRadian(y));
D3DXMatrixRotation(&T3,D3DXToRadian(z));
Final=T*T2*T3;
//----------------------------------
//if you are using Open Gl instead of D3D just take
//The transpose Matrix of the Final
//-----------------------------------
right=D3DXVECTOR3(Final._11,Final._12,Final._13);
up=D3DXVECTOR3(Final._21,Final._22,Final._23);
look=D3DXVECTOR3(Final._31,Final._32,Final._33);
position=D3DXVECTOR3(Final._41,Final._42,Final._43);
D3DXVec3Normalize(&look,&look);

D3DXVec3Cross(&right,&up,&look);
D3DXVec3Normalize(&right,&right);
D3DXVec3Cross(&up,&look,&right);
D3DXVec3Normalize(&up,&up);
}

PS: Once this Function return you can used the vectors along with position to move camera around anywhere you want

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