A 2D tile-based sandbox game.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PhysicsTrait.h 808B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #pragma once
  2. #include "../traits/BodyTrait.h"
  3. #include "../common.h"
  4. namespace Swan {
  5. struct PhysicsTrait {
  6. struct Physics;
  7. struct Tag {};
  8. virtual Physics &get(Tag) = 0;
  9. struct PhysicsProps {
  10. float mass;
  11. float bounciness = 0;
  12. float mushyness = 2;
  13. };
  14. struct Physics {
  15. Vec2 vel{};
  16. Vec2 force{};
  17. bool onGround = false;
  18. void friction(Vec2 coef = Vec2(400, 50));
  19. void gravity(float mass, Vec2 g = Vec2(0, 20));
  20. void standardForces(float mass) { friction(); gravity(mass); }
  21. void update(
  22. const Swan::Context &ctx, float dt,
  23. BodyTrait::Body &body, const PhysicsProps &props);
  24. };
  25. };
  26. /*
  27. * Physics
  28. */
  29. inline void PhysicsTrait::Physics::friction(Vec2 coef) {
  30. force += -vel * coef;
  31. }
  32. inline void PhysicsTrait::Physics::gravity(float mass, Vec2 g) {
  33. force += g * mass;
  34. }
  35. }