Entity.cpp 2.5 KB

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