Capping the mouse and jumping.

Hello everyone,

First of all, I don’t know if my title makes sense. But I have followed http://www.lloydgoodall.com/tutorials/first-person-camera-control-with-lwjgl/ in order to get a sort of FPS camera going.

It works aaalmost perfectly, but if you look for example too much up, I end up looking upside down, and I can spin all the way around. Does anyone know how to “cap” the axis at a specific point to prevent this?

Yet another question, I am using this code to move also. But is there anyone who knows how to jump? I guess there is a lot of velocity and stuff involved, but can anyone give a good example?

Thanks,

EDIT: Kind of fixed the capping now by adding if statements in the lookThrough method inside the Camera class. Just checked if the axis hit a specific angle and increasing / decreasing the pitch by respective angle. Quick and dirty fix and the camera is jittering, but it works.

For the vector part, simple jumping is easy. Have a 2D vector and set the y component to something like -12. Then create another variable called gravity and set it to maybe 0.5. Every frame add the vector to the gravity variable.

For the jumping part I add this code in my class that handles input:

if(Keyboard.isKeyDown(Keyboard.KEY_SPACE)){
            jumping = true;
            if(jumping){
            	
            }
        }

Now for the interesting. What should be inside

if(jumping)

I have a camera class with a 3D vector for position. How can I use the Y-axis for jumping in it? The camera position is defined by:

camera.position.y

Please help.

You need to limit the pitch, or whatever you want to call it.

for example:


pitch = Math.min(Math.max(yaw+mouseDiff, MIN_PITCH), MAX_PITCH);

As for jumping, just subtract a number from your z and bound this number between 0 and a number, add a number to this and bound it every step to create gravity.


void doJump() {
 if (z == 0) z -= 10;
}

and in your step:


z = Math.min(0, z + gravity);

Now where does the pitch come in? Can you help me in forms of the code I provided? I suck at this.

EDIT: Oh, thought the pitch was jump related. That is what got me confused.

EDIT 2: Got it working with your code! Thank you :slight_smile: He is jumping very fast though. Can I fix this?

Yep, just tweak the numbers.

The first piece of code I posted was to help you fix the mouse limiting problem