physic.hpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // SuperTux
  2. // Copyright (C) 2004 Tobias Glaesser <tobi.web@gmx.de>
  3. // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
  4. //
  5. // This program is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. #ifndef HEADER_SUPERTUX_SUPERTUX_PHYSIC_HPP
  18. #define HEADER_SUPERTUX_SUPERTUX_PHYSIC_HPP
  19. #include "math/vector.hpp"
  20. /// Physics engine.
  21. /** This is a very simplistic physics engine handling accelerated and constant
  22. * movement along with gravity.
  23. */
  24. class Physic final
  25. {
  26. public:
  27. Physic();
  28. /// Resets all velocities and accelerations to 0.
  29. void reset();
  30. /// Sets velocity to a fixed value.
  31. void set_velocity(float nvx, float nvy);
  32. void set_velocity(const Vector& vector);
  33. void set_velocity_x(float nvx) { vx = nvx; }
  34. void set_velocity_y(float nvy) { vy = nvy; }
  35. /// Velocity inversion.
  36. void inverse_velocity_x() { vx = -vx; }
  37. void inverse_velocity_y() { vy = -vy; }
  38. Vector get_velocity() const { return Vector(vx, vy); }
  39. float get_velocity_x() const { return vx; }
  40. float get_velocity_y() const { return vy; }
  41. /// Set acceleration.
  42. /** Sets acceleration applied to the object. (Note that gravity is
  43. * eventually added to the vertical acceleration)
  44. */
  45. void set_acceleration(float nax, float nay);
  46. void set_acceleration(const Vector& vector);
  47. void set_acceleration_x(float nax) { ax = nax; }
  48. void set_acceleration_y(float nay) { ay = nay; }
  49. Vector get_acceleration() const { return Vector(ax, ay); }
  50. float get_acceleration_x() const { return ax; }
  51. float get_acceleration_y() const { return ay; }
  52. /// Enables or disables handling of gravity.
  53. void enable_gravity(bool enable) { gravity_enabled_flag = enable; }
  54. bool gravity_enabled() const { return gravity_enabled_flag; }
  55. /** Set gravity modifier factor to apply to object when enabled */
  56. void set_gravity_modifier(float modifier) { gravity_modifier = modifier; }
  57. float get_gravity_modifier() const { return gravity_modifier; }
  58. Vector get_movement(float dt_sec);
  59. private:
  60. /** horizontal and vertical acceleration */
  61. float ax, ay;
  62. /** horizontal and vertical velocity */
  63. float vx, vy;
  64. /** should we respect gravity in our calculations? */
  65. bool gravity_enabled_flag;
  66. /** gravity modifier is multiplied with the sectors gravity */
  67. float gravity_modifier;
  68. };
  69. #endif
  70. /* EOF */