Entity.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #include "common/entities/Entity.h"
  2. #include "utils/Utils.h"
  3. static constexpr float GRAVITY = -0.08f;
  4. Entity::Entity()
  5. : lastLengthAngle(0.0f), lengthAngle(0.0f), lastWidthAngle(0.0f),
  6. widthAngle(0.0f), size(Vector3(0.5f, 1.8f, 0.5f)),
  7. drag(0.686f, 0.98f, 0.686f), speed(0.1f), jumpPower(0.42f),
  8. onGround(false), skip(false) {
  9. }
  10. void Entity::tick() {
  11. lastPosition = position;
  12. lastLengthAngle = lengthAngle;
  13. lastWidthAngle = widthAngle;
  14. velocity += acceleration;
  15. velocity[0] *= drag[0];
  16. velocity[1] *= drag[1];
  17. velocity[2] *= drag[2];
  18. acceleration = Vector3(0.0f, GRAVITY, 0.0f);
  19. }
  20. void Entity::addForce(const Vector3& force) {
  21. acceleration += force;
  22. }
  23. const Vector3& Entity::getVelocity() const {
  24. return velocity;
  25. }
  26. void Entity::move(const Vector3& v) {
  27. position += v;
  28. onGround = v[1] == 0.0f && velocity[1] < 0.0f;
  29. velocity = v;
  30. }
  31. Box Entity::getBox() const {
  32. return Box(size).offset(position);
  33. }
  34. bool Entity::isOnGround() const {
  35. return onGround;
  36. }
  37. void Entity::setPosition(const Vector3& pos) {
  38. setPosition(pos, lengthAngle, widthAngle);
  39. }
  40. void Entity::setPosition(const Vector3& pos, float lengthAngle,
  41. float widthAngle) {
  42. Entity::lastPosition = pos;
  43. Entity::position = pos;
  44. Entity::lastLengthAngle = lengthAngle;
  45. Entity::lengthAngle = lengthAngle;
  46. Entity::lastWidthAngle = widthAngle;
  47. Entity::widthAngle = widthAngle;
  48. Entity::velocity = Vector3();
  49. Entity::acceleration = Vector3();
  50. }
  51. Vector3 Entity::getRenderPosition(float lag) const {
  52. return Utils::interpolate(lastPosition, position, lag) +
  53. Vector3(size[0] * 0.5f, size[1], size[2] * 0.5f);
  54. }
  55. Quaternion Entity::getRotation() const {
  56. return getRenderRotation(1.0f);
  57. }
  58. Quaternion Entity::getRenderRotation(float lag) const {
  59. float lAngle = Utils::interpolate(lastLengthAngle, lengthAngle, lag);
  60. float wAngle = Utils::interpolate(lastWidthAngle, widthAngle, lag);
  61. Quaternion q(Vector3(0.0f, 1.0f, 0.0f), lAngle);
  62. q *= Quaternion(Vector3(1.0f, 0.0f, 0.0f), wAngle);
  63. return q;
  64. }
  65. void Entity::addLengthAngle(float angle) {
  66. lengthAngle += angle;
  67. constexpr float full = 360.0f;
  68. while(lengthAngle < 0.0f) {
  69. lengthAngle += full;
  70. lastLengthAngle += full;
  71. }
  72. while(lengthAngle >= full) {
  73. lengthAngle -= full;
  74. lastLengthAngle -= full;
  75. }
  76. }
  77. void Entity::addWidthAngle(float angle) {
  78. constexpr float border = 89.9f;
  79. widthAngle += angle;
  80. widthAngle = std::min(std::max(widthAngle + angle, -border), border);
  81. }
  82. float Entity::getMaxXZVelocity() const {
  83. float f = std::min(drag[0], drag[2]);
  84. return f * speed / (1 - f);
  85. }
  86. void Entity::jump() {
  87. if(onGround) {
  88. acceleration[1] += (jumpPower / drag[1] - GRAVITY);
  89. }
  90. }