Character.pde 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. class Character
  2. {
  3. private PVector position;
  4. private PVector lookAt;
  5. private float charHeight = 2.0f;
  6. private float yaw;
  7. private float pitch;
  8. private float speed = 0.05f;
  9. private float jumpHeight = 0.05f;
  10. private PVector velocity;
  11. private PVector acceleration;
  12. private boolean isFalling = false;
  13. public Character() {
  14. this.position = new PVector();
  15. this.lookAt = new PVector();
  16. yaw = 0;
  17. pitch = 0;
  18. velocity = new PVector(0, 0, 0);
  19. acceleration = new PVector(0, 0.09, 0);
  20. }
  21. public void updateCamera() {
  22. velocity.add(acceleration);
  23. position.add(velocity);
  24. if (position.y >= -charHeight) {
  25. position.y = -charHeight;
  26. velocity.x = 0;
  27. velocity.y = 0;
  28. velocity.z = 0;
  29. isFalling = false;
  30. }
  31. lookAt.x = (float)(1 * Math.cos(Math.toRadians(yaw)) + position.x);
  32. lookAt.y = (float)(1 * Math.tan(Math.toRadians(pitch)) + position.y);
  33. lookAt.z = (float)(1 * Math.sin(Math.toRadians(yaw)) + position.z);
  34. camera(position.x, position.y, position.z,
  35. lookAt.x, lookAt.y, lookAt.z,
  36. 0, 1, 0);
  37. }
  38. public void move(float delta) {
  39. if (input['W']) {
  40. walkForward(speed * delta);
  41. } else if (input['S']) {
  42. walkBackwards(speed * delta);
  43. }
  44. if (input['A']) {
  45. strafeLeft(speed * delta);
  46. } else if (input['D']) {
  47. strafeRight(speed * delta);
  48. }
  49. if (input[' ']) {
  50. jump(jumpHeight * delta);
  51. }
  52. }
  53. public void setPosition(float x, float y, float z) {
  54. this.position.x = x;
  55. this.position.y = y;
  56. this.position.z = z;
  57. }
  58. public void walkForward(float distance)
  59. {
  60. position.x += distance * (float)Math.cos(Math.toRadians(yaw));
  61. position.z += distance * (float)Math.sin(Math.toRadians(yaw));
  62. }
  63. public void walkBackwards(float distance)
  64. {
  65. position.x -= distance * (float)Math.cos(Math.toRadians(yaw));
  66. position.z -= distance * (float)Math.sin(Math.toRadians(yaw));
  67. }
  68. public void strafeLeft(float distance)
  69. {
  70. position.x += distance * (float)Math.cos(Math.toRadians(yaw-90));
  71. position.z += distance * (float)Math.sin(Math.toRadians(yaw-90));
  72. }
  73. public void strafeRight(float distance)
  74. {
  75. position.x += distance * (float)Math.cos(Math.toRadians(yaw+90));
  76. position.z += distance * (float)Math.sin(Math.toRadians(yaw+90));
  77. }
  78. public void jump(float distance)
  79. {
  80. if (!isFalling) {
  81. velocity.y -= distance;
  82. isFalling = true;
  83. }
  84. }
  85. public void look(float screenX, float screenY) {
  86. yaw = 180 * (screenX - (width / 2)) / (width / 2);
  87. pitch = 90 * (screenY - (height / 2)) / (height / 2);
  88. println(yaw);
  89. println(pitch);
  90. }
  91. }