particle.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. * 2D Physic Engine
  3. * particules.cpp:
  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. #include <graphics.h>
  10. #include <physics/particle.h>
  11. uint32_t forceColours[] =
  12. {
  13. 0xFF0000FF, 0xFF00FF00, 0xFFFF0000,
  14. 0xFF00FFFF, 0xFFFF00FF, 0xFFFFFF00,
  15. 0xFFFFFFFF,
  16. };
  17. void particle::integrate(double dt)
  18. {
  19. this->acceleration = this->sum_of_forces * this->invMass;
  20. this->velocity += this->acceleration * dt;
  21. this->position += this->velocity * dt;
  22. this->frame_sum = this->sum_of_forces;
  23. this->clearForces();
  24. }
  25. void particle::addForce(const vec2 &force)
  26. {
  27. this->sum_of_forces += force;
  28. this->forces.push_back(force);
  29. }
  30. void particle::forceDebug(bool showVelocity, bool showSum, bool showAll)
  31. {
  32. int c = 0;
  33. if (showAll)
  34. {
  35. for (auto f: this->forces)
  36. {
  37. graphics::draw::arrow(this->position.x, this->position.y, f.x, f.y, forceColours[c]);
  38. c = (c + 1) % 7;
  39. }
  40. }
  41. if (showSum)
  42. {
  43. graphics::draw::arrow(this->position.x, this->position.y,
  44. this->frame_sum.x, this->frame_sum.y,
  45. 0xFF00FFCC);
  46. }
  47. if (showVelocity)
  48. {
  49. graphics::draw::arrow(this->position.x, this->position.y,
  50. this->velocity.x, this->velocity.y,
  51. 0xFF9966ff);
  52. }
  53. this->forces.clear();
  54. }