Colby_ 100 Report post Posted February 17, 2012 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: [code]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);[/code] 0 Share this post Link to post Share on other sites
dpadam450 2357 Report post Posted February 17, 2012 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. 0 Share this post Link to post Share on other sites
Colby_ 100 Report post Posted February 17, 2012 [quote name='dpadam450' timestamp='1329447558' post='4913806'] 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. [/quote] Thanks but I honestly don't understand what you're telling me about the math. 0 Share this post Link to post Share on other sites
dpadam450 2357 Report post Posted February 17, 2012 .... 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; 0 Share this post Link to post Share on other sites
Colby_ 100 Report post Posted February 22, 2012 Thanks man i got it with the following code: (For anyone else needing it) [code] 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; }[/code] 0 Share this post Link to post Share on other sites