I'm making a zombie-shooting game and trying to mimic some of the mechanics of The Last of Us, but with a mouse and keyboard. When the player presses S, the camera zooms over the character's shoulder and the mouse can then be used to target a zombie. For the most part, it works. Except that for the life of me I can't seem to restrict the camera's rotation. The following are my mouse event methods. The weird thing is that sometimes the vertical rotation behaves perfectly (and blocks at both ends), and, most of the times it doesn't.
public void mouseMoved(MouseEvent e) {
if (initialX == 100101 && initialY == 100101) {
initialX = e.getX();
initialY = e.getY();
}
if (aiming) {
float deltaX = e.getX()-initialX;
float deltaY = e.getY()-initialY;
float addX = deltaX-lastDeltaX;
float addY = deltaY-lastDeltaY;
if ((addX > 0.00f && cameraDisplacementY < Math.PI*.3) || (addX < 0.00f && cameraDisplacementY > -Math.PI*.3))
cameraDisplacementY += addX*.02f;//HORIZONTAL ROTATION
if ((addY > 0.00f && cameraDisplacementX < Math.PI*.1) || (addY < 0.00f && cameraDisplacementX > -Math.PI*.1))
cameraDisplacementX += addY*.02f;//VERTICAL ROTATION
lastDeltaX = deltaX;
lastDeltaY = deltaY;
// if (cameraDisplacementY > 1f || cameraDisplacementY < -1f) {
hero.getRoot().rotateY(cameraDisplacementY);
gun.rotateY(cameraDisplacementY);
heroSphere.rotateY(cameraDisplacementY);
cameraDisplacementY = 0.00f;
// }
}
}
public void mouseDragged(MouseEvent e) {}
public void mouseExited(MouseEvent e) {
robot.mouseMove(this.getWidth()/2, this.getHeight()/2);
initialX = e.getX();
initialY = e.getY();
}
public void mouseClicked(MouseEvent e) {
if (aiming)
fireWeapon = true;
}
private void fire() {
if (bullet != null)
theWorld.removeObject(bullet);
bullet = new Bullet(gun, theCamera);
bullet.build();
theWorld.addObject(bullet);
if (buffer.usesRenderer(IRenderer.RENDERER_OPENGL))
bullet.compileAndStrip();
soundQueue = new SoundQueue();
soundQueue.add(new SoundAndTime(shotgunBlast, 0l));
soundQueue.add(new SoundAndTime(shotgunShellsFalling, 700l));
soundQueue.add(new SoundAndTime(shotgunReload, 1100l));
soundQueue.activateQueue();
fireWeapon = false;//JUST FIRED
}