Jump to content



Help with FPS camera?

  • You cannot reply to this topic
4 replies to this topic

#1 Colby_   Members   -  Reputation: 100

Like
0Likes
Like

Posted 16 February 2012 - 08:11 PM

I've followed tutorials on how to do this to the letter, but I can't seem to get it to move around correctly. For example, W should go forward, and forward is defined by which direction the camera is pointing.

Heres the main loop:

float x = 0, y = 0, z = 0, pit = 0, yaw = 0;
	    int size = 1;
	    do {
		    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
		    if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) {
			    Display.destroy();
			    System.exit(0);
		    }
		    if (Keyboard.isKeyDown(Keyboard.KEY_A)) {
			    x -= 0.04 * (float) Math.sin(Math.toRadians(yaw - 90));
			    z += 0.04 * (float) Math.cos(Math.toRadians(yaw - 90));
		    }
		    if (Keyboard.isKeyDown(Keyboard.KEY_D)) {
			    x -= 0.04 * (float) Math.sin(Math.toRadians(yaw + 90));
			    z += 0.04 * (float) Math.cos(Math.toRadians(yaw + 90));
		    }
		    if (Keyboard.isKeyDown(Keyboard.KEY_W)) {
			    x -= 0.04 * (float) Math.sin(Math.toRadians(yaw));
			    z += 0.04 * (float) Math.cos(Math.toRadians(yaw));
		    }
		    if (Keyboard.isKeyDown(Keyboard.KEY_S)) {
			    x += 0.04 * (float) Math.sin(Math.toRadians(yaw));
			    z -= 0.04 * (float) Math.cos(Math.toRadians(yaw));
		    }
		    yaw += Mouse.getDX();
		    pit += Mouse.getDY();
		    Mouse.setGrabbed(true);
		    glLoadIdentity();
		    GL11.glRotatef(pit / 3, 1.0f, 0.0f, 0.0f);
		    GL11.glRotatef(yaw / 3, 0.0f, 1.0f, 0.0f);
		    glTranslatef(x, y, z);

          //Some test world data
		    drawCube(size, crate);
		    glTranslatef(0.0f, 0.0f, -2.0f);
		    drawCube(size, crate);
		    glTranslatef(0.0f, 0.0f, -2.0f);
		    drawCube(size, crate);
		    glTranslatef(0.0f, 0.0f, -2.0f);
		    drawCube(size, crate);
		    glTranslatef(0.0f, 0.0f, -2.0f);
		    drawCube(size, crate);
		    glTranslatef(0.0f, 0.0f, -2.0f);
		    drawCube(size, crate);
		    glTranslatef(0.0f, 0.0f, -2.0f);
		    drawCube(size, crate);
		    Display.update();
	    } while (true);


Ad:

#2 dpadam450   Members   -  Reputation: 184

Like
0Likes
Like

Posted 16 February 2012 - 08:59 PM

Well your math looks wrong, but first:

1.) pit/ 3 should be pit/ 3.0, a float divided by an integer is a resultant integer. Therfore after division your decimal portion is chopped off.
2.) to understand better design, your .04 should be moved to a single variable called "speed", that way you can just change speed instead of the 8 spots you have .04 in your code.

As for the math, with an angle of zero you are looking at minus z axis. So your direction x should be 0 and z should be -1. You should try writing what the vectors should be on paper for 0,90,180 etc degrees and figure out how to get those. That plus and minus of 90 seems wrong. Caclulate a forward and right vector, then move +forward/-forward +right/-right, and your code will be less lines, and once you know those 2 vectors are correct, then you will be fine.
http://www.ultimategamedevelopment.com - My game development DVD tutorials. Learn to make complete 2D/3D games step by step.

http://www.youtube.c...ev?feature=mhee - Follow my video blog on making a 3D flight sim game.

#3 Colby_   Members   -  Reputation: 100

Like
0Likes
Like

Posted 16 February 2012 - 09:41 PM

View Postdpadam450, on 16 February 2012 - 08:59 PM, said:

Well your math looks wrong, but first:

1.) pit/ 3 should be pit/ 3.0, a float divided by an integer is a resultant integer. Therfore after division your decimal portion is chopped off.
2.) to understand better design, your .04 should be moved to a single variable called "speed", that way you can just change speed instead of the 8 spots you have .04 in your code.

As for the math, with an angle of zero you are looking at minus z axis. So your direction x should be 0 and z should be -1. You should try writing what the vectors should be on paper for 0,90,180 etc degrees and figure out how to get those. That plus and minus of 90 seems wrong. Caclulate a forward and right vector, then move +forward/-forward +right/-right, and your code will be less lines, and once you know those 2 vectors are correct, then you will be fine.
Thanks but I honestly don't understand what you're telling me about the math.

#4 dpadam450   Members   -  Reputation: 184

Like
0Likes
Like

Posted 16 February 2012 - 10:44 PM

....
if (Keyboard.isKeyDown(Keyboard.KEY_A)) {
x -= 0.04 * (float) Math.sin(Math.toRadians(yaw - 90));
z += 0.04 * (float) Math.cos(Math.toRadians(yaw - 90));
}

....

With no rotation, at startup, if you hit the W key, you move forward. With no rotation, in opengl your forward vector is x = 0, z = -1
So if your rotation is 0 degrees, you are putting in 0-90, so sin(-90) = -1. In your code then you are doing x -= .04*-1, but we already established there is no x movement because by default no rotation we are looking down the z axis.

We knows sin(0) = 0, so that seems to work at least for that case. and cos(0) = 1, so we want x=0,z=-1
float forwardX = sin(angle);
float forwardZ = cos(0)*-1;

if(WKEY)
x += forwardX;
z += forwardZ;
http://www.ultimategamedevelopment.com - My game development DVD tutorials. Learn to make complete 2D/3D games step by step.

http://www.youtube.c...ev?feature=mhee - Follow my video blog on making a 3D flight sim game.

#5 Colby_   Members   -  Reputation: 100

Like
0Likes
Like

Posted 21 February 2012 - 06:13 PM

Thanks man i got it with the following code: (For anyone else needing it)

            if (Keyboard.isKeyDown(Keyboard.KEY_A)) {
                float yRotRad = (float) Math.toRadians(yaw);
                x += -0.04f * Math.cos(yRotRad);
                z += -0.04f * Math.sin(yRotRad);
            }
            if (Keyboard.isKeyDown(Keyboard.KEY_D)) {
                float yRotRad = (float) Math.toRadians(yaw);
                x += 0.04f * Math.cos(yRotRad);
                z += 0.04f * Math.sin(yRotRad);
            }
            if (Keyboard.isKeyDown(Keyboard.KEY_W)) {
                float pitchFactor = (float) Math.cos(Math.toRadians(pit));
                x += (0.04F * (Math.sin(Math.sin(Math.toRadians(yaw))))) * pitchFactor;

                y += 0.04F * (Math.sin(Math.toRadians(pit))) * -1.0f;

                float yawFactor = (float) (Math.cos(Math.toRadians(pit)));
                z += (0.04 * (Math.cos(Math.toRadians(yaw))) * -1.0f) * yawFactor;
            }
            if (Keyboard.isKeyDown(Keyboard.KEY_S)) {
                float pitchFactor = (float) Math.cos(Math.toRadians(pit));
                x -= (0.04F * (Math.sin(Math.sin(Math.toRadians(yaw))))) * pitchFactor;

                y -= 0.04F * (Math.sin(Math.toRadians(pit))) * -1.0f;

                float yawFactor = (float) (Math.cos(Math.toRadians(pit)));
                z -= (0.04 * (Math.cos(Math.toRadians(yaw))) * -1.0f) * yawFactor;
            }







We are working on generating results for this topic
PARTNERS