Camera.java 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package eu.tankernn.gameEngine.entities;
  2. import org.lwjgl.util.vector.Vector3f;
  3. /**
  4. * Camera for determining the view frustum when rendering.
  5. *
  6. * @author Frans
  7. */
  8. public class Camera implements Positionable {
  9. protected Vector3f position = new Vector3f(0, 10, 0);
  10. protected float pitch = 20;
  11. protected float yaw;
  12. protected float roll;
  13. public Camera(Vector3f position) {
  14. this.position = position;
  15. }
  16. public Camera() {
  17. }
  18. /**
  19. * Points the camera so that that <code>entity</code> is in the center.
  20. *
  21. * @param entity The object to point towards
  22. */
  23. public void pointToEntity(Positionable entity) {
  24. Vector3f targetPos = entity.getPosition();
  25. Vector3f delta = new Vector3f();
  26. Vector3f.sub(position, targetPos, delta);
  27. this.yaw = (float) (Math.atan2(-delta.x, delta.z) * 180 / Math.PI);
  28. float distance = (float) Math.sqrt(Math.pow(delta.x, 2) + Math.pow(delta.z, 2));
  29. this.pitch = (float) (Math.atan2(delta.y, distance) * 180 / Math.PI);
  30. }
  31. public void update() {
  32. }
  33. public Vector3f getPosition() {
  34. return position;
  35. }
  36. public float getPitch() {
  37. return pitch;
  38. }
  39. public float getYaw() {
  40. return yaw;
  41. }
  42. public float getRoll() {
  43. return roll;
  44. }
  45. /**
  46. * Inverts the pitch of the camera, used for water rendering.
  47. */
  48. public void invertPitch() {
  49. this.pitch = -pitch;
  50. }
  51. /**
  52. * Inverts the roll of the camera, used for water rendering.
  53. */
  54. public void invertRoll() {
  55. this.roll = -roll;
  56. }
  57. }