body.h 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. * 2D Physic Engine
  3. * body.h:
  4. * Based on pikuma.com Learn Game Physics Engine Programming course.
  5. * Copyright (c) 2022 986-Studio. All rights reserved.
  6. *
  7. * Created by Manoël Trapier on 07/06/2022.
  8. */
  9. #ifndef PHYSICENGINE_PHYSICS_BODY_H
  10. #define PHYSICENGINE_PHYSICS_BODY_H
  11. #include <vector>
  12. #include <physics/vec2.h>
  13. #include <physics/shape.h>
  14. class body
  15. {
  16. public:
  17. vec2 position;
  18. vec2 acceleration;
  19. vec2 velocity;
  20. vec2 sum_of_forces;
  21. vec2 frame_sum;
  22. double mass;
  23. double invMass;
  24. double rotation;
  25. shape *shp;
  26. uint32_t colour;
  27. /* Only used for debug display */
  28. std::vector<vec2> forces;
  29. public:
  30. body(const shape &s, double x, double y, double mass) : position(x, y), mass(mass), rotation(0)
  31. {
  32. this->colour = 0xFFFFFFFF;
  33. this->shp = s.clone();
  34. this->invMass = 0;
  35. if (mass != 0.0)
  36. {
  37. this->invMass = 1 / mass;
  38. }
  39. };
  40. ~body()
  41. {
  42. delete this->shp;
  43. };
  44. void setColour(uint32_t colour)
  45. {
  46. this->colour = colour;
  47. };
  48. void clearForces()
  49. {
  50. this->sum_of_forces = vec2(0, 0);
  51. }
  52. void forceDebug(bool showVelocity, bool showSum, bool showAll);
  53. void addForce(const vec2 &force);
  54. void integrate(double dt);
  55. };
  56. #endif /* PHYSICENGINE_PHYSICS_BODY_H */