package electrosphere.controls; import org.joml.Vector3d; import org.joml.Vector3f; import electrosphere.entity.EntityUtils; import electrosphere.entity.types.camera.CameraEntityUtils; import electrosphere.game.client.targeting.crosshair.Crosshair; import electrosphere.main.Globals; import electrosphere.main.Main; import electrosphere.renderer.ui.events.MouseEvent; public class CameraHandler { float mouseSensitivity = .1f; float cameraSpeed; float yaw = 150; float pitch = 50; Vector3f cameraRotationVector = new Vector3f(); Vector3f radialOffset = new Vector3f(0,1,0); public void handleMouseEvent(MouseEvent event){ if(Globals.controlHandler != null && !Globals.controlHandler.isMouseVisible()){ yaw = yaw + event.getDeltaX() * mouseSensitivity; pitch = pitch - event.getDeltaY() * mouseSensitivity; if (pitch > 100.0f) { pitch = 100.0f; } if (pitch < -99.0f) { pitch = -99.0f; } } updateGlobalCamera(); } public void updateRadialOffset(Vector3f offset){ radialOffset = offset; } public void updateGlobalCamera(){ cameraSpeed = 2.5f * Main.deltaFrames; if(Crosshair.getCrosshairActive()){ // if(Globals.playerCharacter != null){ // Vector3d charPos = EntityUtils.getPosition(Globals.playerCharacter); // CameraEntityUtils.setCameraCenter(Globals.playerCamera, new Vector3f((float)charPos.x,(float)charPos.y,(float)charPos.z)); // } Vector3d characterPos = EntityUtils.getPosition(Globals.playerCharacter); Vector3d targetPos = Crosshair.getTargetPosition(); Vector3d diffed = new Vector3d(targetPos).sub(characterPos).mul(-1).normalize(); cameraRotationVector.set((float)diffed.x, 0.5f, (float)diffed.z).normalize(); yaw = (float)Math.toDegrees(Math.atan2(diffed.z, diffed.x)); CameraEntityUtils.setCameraPitch(Globals.playerCamera, pitch); CameraEntityUtils.setCameraYaw(Globals.playerCamera, yaw); } else { CameraEntityUtils.setCameraPitch(Globals.playerCamera, pitch); CameraEntityUtils.setCameraYaw(Globals.playerCamera, yaw); // if(Globals.playerCharacter != null){ // Vector3d charPos = EntityUtils.getPosition(Globals.playerCharacter); // CameraEntityUtils.setCameraCenter(Globals.playerCamera, new Vector3f((float)charPos.x,(float)charPos.y,(float)charPos.z)); // } cameraRotationVector.x = 0 + (float) Math.cos(yaw / 180.0f * Math.PI) * 1; cameraRotationVector.y = 0 + (float) Math.sin(pitch / 180.0f * Math.PI) * 1; cameraRotationVector.z = 0 + (float) Math.sin(yaw / 180.0f * Math.PI) * 1; cameraRotationVector.normalize(); } //update view matrix offset float xFactor = (float)Math.cos(yaw / 180.0f * Math.PI); float yFactor = (float)Math.sin(yaw / 180.0f * Math.PI); // Vector3f radialOffset = CameraEntityUtils.getOrbitalCameraRadialOffset(Globals.playerCamera); Vector3f trueOffset = new Vector3f(radialOffset).mul(xFactor,1.0f,yFactor); CameraEntityUtils.setOrbitalCameraRadialOffset(Globals.playerCamera, trueOffset); // float cam_Player_Orbit_Magnitude = CameraEntityUtils.getCameraOrbitRadius(Globals.playerCamera); cameraRotationVector.mul(CameraEntityUtils.getOrbitalCameraDistance(Globals.playerCamera)); CameraEntityUtils.setCameraEye(Globals.playerCamera, cameraRotationVector); Globals.viewMatrix = CameraEntityUtils.getCameraViewMatrix(Globals.playerCamera); } public float getYaw(){ return yaw; } public float getPitch(){ return pitch; } }