Last Updated on August 27, 2012 by nghiaho12
Today I was looking around for some quick copy and paste code to add FPS gaming control to a small GLUT application I am writing. Half an hour or so of searching later and I still couldn’t find anything that I liked. A lot of the results were from forums of people trying to roll out their own code. I gave up and resorted to something I didn’t think I’d ever do, use code I wrote during my PhD …
After an hour or so of tinkering I got something that I’m happy with. I got my GLUT demo to do the following:
- W,A,S,D keys for moving and strafing
- Mouse look (default is inverted mouse)
- Mouse button to fly up/down
- SPACEBAR to toggle FPS control mode
- Mouse always stays within the window with the cursor hidden
It has the feel of a proper FPS game.
Here’s what the demo looks like. Interestingly, when taking a screenshot the mouse cursor appears in the image.
The demo code can be used as a copy and paste project to quickly get a viewer running. All you have to do is add your rendering code into the Display() function.
Download
The demo requires freeglut to be installed. You can use CodeBlocks to open up the project or type make if you’re in Linux. If you’re using Windows you’ll have to setup your own Visual Studio project.
If you have any questions just leave a comment and I’ll get back to you.
Thanks! this example showed to me how to manage the mouse.
hello that is what I am looking for ty I also try to write code for this but no chance I did something but it was too stupid you know what ever
can you make a tutorial video for this
A video tutorial is not necessary, the code itself is not that difficult to figure out.
I got an unresolved external symbols error when trying to run this?
Any idea how to run this file in visual studio?
Whats the actual sumbol?
wow thanks for the extremely fast reply..
I got this error :
error LNK2019: unresolved external symbol “public: void __thiscall Camera::Init(void)” (?Init@Camera@@QAEXXZ) referenced in function “public: __thiscall Camera::Camera(void)” (??0Camera@@QAE@XZ) C:\Users\Kenny\Documents\Visual Studio 2012\Projects\FPSMovement\Source.obj
🙂
Add Camera.cpp to your project file.
Well, after stupid trying for a bit, i changed the include “Camera.h” into “Camera.cpp” and it’s working perfectly…
Still weird why include Camera.h not working..
Anyway thanks for the code!! It’s really help me starting with my project!
Cheers!!
Thanks so much I have been trying my own implementation of this and I got horrific stuttering in my camera. I read through your code and saw the just_warped bool in the mouse callback which fixed it up.
the best first person camera tutorial i have ever seen.thanks
Why do you multiply *cos(vertical_angle) when you calculate the direction you are looking to on the z and x axis? It behaves the same without it and I can’t explain the reason.
Thanks!
Ooops, I got why you do it. That way you can do free movement with the camera, Since I am doing two dimensional movement I didn’t realize.
Can I use this camera class for my projects?
Even for commercial use?
Sure go it. But it would be nice if you acknowledge me 🙂
Great Prog!! Thank you SO MUCH, Nghia Ho!!!!!
I also noticed a little glitch: you could not press more keys at once (forward and strafe, for example). So I changed the “end if”s in the Timer function with “if”s and everything works!!!
THANK YOU AGAIN, MAN!!!!
I am running Linux Mint and I had to add this to line 18 of your Makefile to have it compile correctly:
-lX11 -lGL
This is the complete line now with additions in case anyone else has problems compiling.
LIB = -lglut -lGLU -lX11 -lGL
Thanks for this very nice example!