Archived

This topic is now archived and is closed to further replies.

Terminus

Camera setup in a first person game

Recommended Posts

Terminus    122
Hi, I''m sure this question has been asked a thousand times before, but after a bit of searching I can''t seem to find a topic that exactly answers my question, so I decided to make a new topic. I am a Beginner-Intermediate programmer making a simple first person shooter. I''m having problems figuring out how to make my camera follow the crosshair. I''m using OpenGL, and gluLookAt() to setup the camera. Basically my problem is that when the user presses the ''D'' button, I want the camera to rotate to the right, and the reverse for the ''A'' button. How do I get my lookX, lookY, and lookZ coordinates to go to the right places? All I can think of is to set up a virtual sphere around the camera and have my lookX, Y, and Z coords be confined to the sphere''s surface. Then, when I press the key to turn right, I could have the camera move 1 degree right along the sphere (as an example), and have a function automatically calculate the look-at coords based on what part of the sphere I''m facing. The only thing is that I have no idea how to implement that. So if anyone has a clue what I''m talking about could you give me a hand? I could be completely wrong about the sphere thing and there''s probably a much better way to do it, so if you know of one let me know. Thanks a lot! Oh, and yes I know I could just rotate the camera using glRotatef(), but I''d rather not get into that and stick to gluLookAt() if at all possible.

Share this post


Link to post
Share on other sites
__fold    202
If you don''t understand how to do it. An easy solution is to push the modelview matrix, load the identity matrix, rotate the scene as with glRotatef(), read back the modelview matrix from the card and then pop the matrix. Now you have a matrix that rotates a point around a vector. Start to rotate around the up vector (0,1,0) by doing matrix * point and make sure you understand what''s happening. Then it''s not that difficult to extend that to rotate as you want.

When you understand how that at all works you can easily optimize just by looking at what you''ve done, not that that it will give you any performance gains but anyway.

Share this post


Link to post
Share on other sites
Sphax    122
Hi Terminus,

I have implemented my camera class exactly the way you''re talking about, the camera position is always on a "virtual" sphere, and it''s position can be represented by two angles and a radius. Try to shearch for "sphere coordinates" on the net, you''ll find some articles about that.

Share this post


Link to post
Share on other sites
rm3    122
Can''t you just use one angle, and choose the look-at point using sine and cosine?

I''m not sure I understand why sphere coordinates are needed

Share this post


Link to post
Share on other sites
benryves    1999
Are you allowing yourself to look up/down as well as left/right? A simple way to create a "look at" set of coords for just left/right looking is:

Look.x = Player.x + sin(xLooking)
Look.y = Player.y + sin(yLooking)

I''ve a full set of ''em as functions for Visual Basic 6 that I worked out:

CreateXVectorFromView(xLooking, yLooking)
CreateYVectorFromView(xLooking, yLooking)
CreateZVectorFromView(xLooking, yLooking)

If you like, I''ll post ''em here.

Share this post


Link to post
Share on other sites