physics.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #pragma once
  2. #include <glm/glm.hpp>
  3. #include <vector>
  4. #include <unordered_map>
  5. constexpr int TYPE_CIRCLE = 0;
  6. constexpr int TYPE_BOX = 1;
  7. constexpr int TYPE_CILINDRU = 2;
  8. constexpr static float MAX_VELOCITY = 10'000;
  9. constexpr static float MAX_ACCELERATION = 10'000;
  10. constexpr static float MAX_AIR_DRAG = 100;
  11. struct PhysicsObject
  12. {
  13. //the position represents the center of the object.
  14. // the shape is the width height depth for cube and for circle it is radius.
  15. glm::vec3 position = {}; // 12 bytes
  16. glm::vec3 shape = {};
  17. glm::vec3 velocity = {};
  18. glm::vec3 acceleration = {};
  19. glm::vec3 collisionDir = {};
  20. float mass = 1;
  21. float bouncyness = 0.9;
  22. int type = 0;
  23. float staticFriction = 0.4;
  24. float dynamicFriction = 0.3;
  25. bool collidesBottom = 0;
  26. bool collidesSide = 0;
  27. glm::vec3 collisionVector = {};
  28. glm::vec3 getMin()
  29. {
  30. if (type == TYPE_CIRCLE)
  31. {
  32. glm::vec3 rez = position;
  33. rez -= glm::vec3(shape.x, shape.x, shape.x);
  34. return rez;
  35. }
  36. else if (type == TYPE_BOX)
  37. {
  38. glm::vec3 rez = position;
  39. rez -= glm::vec3(shape) / 2.f;
  40. return rez;
  41. }
  42. else if (type == TYPE_CILINDRU)
  43. {
  44. glm::vec3 rez = position;
  45. rez -= glm::vec3(shape.x, shape.y / 2.f, shape.x);
  46. return rez;
  47. }
  48. }
  49. glm::vec3 getMax()
  50. {
  51. if (type == TYPE_CIRCLE)
  52. {
  53. glm::vec3 rez = position;
  54. rez += glm::vec3(shape.x, shape.x, shape.x);
  55. return rez;
  56. }
  57. else if (type == TYPE_BOX)
  58. {
  59. glm::vec3 rez = position;
  60. rez += glm::vec3(shape) / 2.f;
  61. return rez;
  62. }if (type == TYPE_CILINDRU)
  63. {
  64. glm::vec3 rez = position;
  65. rez += glm::vec3(shape.x, shape.y / 2.f, shape.x);
  66. return rez;
  67. }
  68. }
  69. };
  70. PhysicsObject createBall(glm::vec3 pos, float r);
  71. PhysicsObject createBox(glm::vec3 pos, glm::vec3 size);
  72. PhysicsObject createCilindru(glm::vec3 pos, float r, float h);
  73. struct Simulator
  74. {
  75. std::unordered_map<int, PhysicsObject> bodies;
  76. void updateForces(PhysicsObject &object, float deltaTime);
  77. int id = 1;
  78. int getIdAndIncrement()
  79. {
  80. return id++;
  81. }
  82. glm::vec3 boxDimensions = {200, 100, 200};
  83. void update(float deltaTime);
  84. };