
Advertisement
This topic is 1842 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.
If you intended to correct an error in the post then please contact us.
Recommended Posts
Nick Karasch 171
Disclaimer: This code is an ugly mess that has been getting abused for a few days. It is not pretty, I'm aware. I'm just desperate to get it working, then the beautification process will occur.
Also, the delta time is no longer working as intended. I know how to fix it, but there is no point when I can't get rotations working.
I'm having a tough time here. My translations work perfectly. However, as soon as I start adding rotations in, things go nuts. X axis rotations alone are fine, Y axis alone are fine but when I have both activated I end up spinning and rolling out of control. I'm also pretty sure that the rotations are all more like revolutions around the original starting point.
Any insight would be great. I was stole this code http://www.lloydgoodall.com/tutorials/firstpersoncameracontrolwithlwjgl/, using the built in matrix stack and it worked great. A lot of the code below is ugly remnants of it
package karasch.lwjgl.utility; import java.io.FileNotFoundException; import java.io.IOException; import karasch.lwjgl.entitysetup.IEntity; import org.lwjgl.input.Mouse; import org.lwjgl.util.vector.Matrix4f; import org.lwjgl.util.vector.Vector3f; public class View implements IEntity { private Vector3f position; private float x, y, z, yaw, pitch; private float moveSpeed, mouseSensitivity, deltaMoveSpeed; int dx, dy; static Matrix4f viewMatrix; public View(float x, float y, float z) throws FileNotFoundException, IOException { moveSpeed = 0.00001f; dx = Mouse.getDX(); dy = Mouse.getDY(); yaw = 0; pitch = 0; position = new Vector3f(x, y, z); mouseSensitivity = 0.0000025f; viewMatrix = new Matrix4f(); viewMatrix.setIdentity(); } // increment the camera's current yaw rotation public void yaw(float amount) { // increment the yaw by the amount param yaw += amount; } // increment the camera's current yaw rotation public void pitch(float amount) { // increment the pitch by the amount param pitch += amount; } public void applyTransformations() { viewMatrix.rotate(pitch, new Vector3f(1, 0, 0)); viewMatrix.rotate(yaw, new Vector3f(0, 1, 0)); viewMatrix.translate(position); System.out.println(viewMatrix); } public void update(int delta) { deltaMoveSpeed = moveSpeed * delta; dx = Mouse.getDX(); dy = Mouse.getDY(); this.yaw(dx * mouseSensitivity); this.pitch(dy * mouseSensitivity); if (Input.dPressed) { strafeRight(deltaMoveSpeed); } if (Input.aPressed) { strafeLeft(deltaMoveSpeed); } if (Input.wPressed) { walkForward(deltaMoveSpeed); } if (Input.sPressed) { walkBackward(deltaMoveSpeed); } if (Input.controlPressed) { moveDown(deltaMoveSpeed); } if (Input.spacePressed) { moveUp(deltaMoveSpeed); } } public static Matrix4f getViewMatrix() { return viewMatrix; } // moves the camera forward relative to its current rotation (yaw) // moves the camera forward relative to its current rotation (yaw) public void walkForward(float distance) { position.x = distance * (float) Math.sin(Math.toRadians(yaw)); position.z += distance * (float) Math.cos(Math.toRadians(yaw)); } // moves the camera backward relative to its current rotation (yaw) public void walkBackward(float distance) { position.x += distance * (float) Math.sin(Math.toRadians(yaw)); position.z = distance * (float) Math.cos(Math.toRadians(yaw)); } public void moveUp(float distance) { position.y = distance; } public void moveDown(float distance) { position.y += distance; } // strafes the camera left relitive to its current rotation (yaw) public void strafeLeft(float distance) { position.x = distance * (float) Math.sin(Math.toRadians(yaw  90)); position.z += distance * (float) Math.cos(Math.toRadians(yaw  90)); } // strafes the camera right relitive to its current rotation (yaw) public void strafeRight(float distance) { position.x = distance * (float) Math.sin(Math.toRadians(yaw + 90)); position.z += distance * (float) Math.cos(Math.toRadians(yaw + 90)); } public Vector3f getPosition() { return position; } public float getX() { return x; } public void setX(float x) { this.x = x; } public float getY() { return y; } public void setY(float y) { this.y = y; } public float getZ() { return z; } public void setZ(float z) { this.z = z; } @Override public void destroy() { } @Override public void render() { this.applyTransformations(); } }
__SKYe 1784
Does just one rotation work as expected?
Try changing the order of the rotations, doing the yaw first and the pitch last.
Also, do you apply the transformations on an identity matrix each time you render the scene? I think it is necessary to do so, else you're doing translations over rotations over translations, etc.

Advertisement
You could read this http://www.arcsynthesis.org/gltut/
Rotation, perspective and translation matrices are explained there. You also have different space transformations too (model to world, word to view, etc). And quaternion rotation for when you get bored of matrices :D
It may not be easy to understand, depending on the familiarity you have with linear algebra (even then, the books starts to apply it to graphics so it may get complicated anyway).
Share this post
Link to post
Share on other sites