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
18#ifndef HEADER_SUPERTUX_SUPERTUX_PHYSIC_HPP
19#define HEADER_SUPERTUX_SUPERTUX_PHYSIC_HPP
20
21#include "math/vector.hpp"
22
23/// Physics engine.
24/** This is a very simplistic physics engine handling accelerated and constant
25 * movement along with gravity.
26 */
27class Physic final
28{
29public:
30 Physic();
31
32 /// Resets all velocities and accelerations to 0.
33 void reset();
34
35 /// Sets velocity to a fixed value.
36 void set_velocity(float nvx, float nvy);
37 void set_velocity(const Vector& vector);
38
39 void set_velocity_x(float nvx) { vx = nvx; }
40 void set_velocity_y(float nvy) { vy = nvy; }
41
42 /// Velocity inversion.
43 void inverse_velocity_x() { vx = -vx; }
44 void inverse_velocity_y() { vy = -vy; }
45
46 Vector get_velocity() const { return Vector(vx, vy); }
47 float get_velocity_x() const { return vx; }
48 float get_velocity_y() const { return vy; }
49
50 /// Set acceleration.
51 /** Sets acceleration applied to the object. (Note that gravity is
52 * eventually added to the vertical acceleration)
53 */
54 void set_acceleration(float nax, float nay);
55
56 void set_acceleration_x(float nax) { ax = nax; }
57 void set_acceleration_y(float nay) { ay = nay; }
58
59 Vector get_acceleration() const { return Vector(ax, ay); }
60 float get_acceleration_x() const { return ax; }
61 float get_acceleration_y() const { return ay; }
62
63 /// Enables or disables handling of gravity.
64 void enable_gravity(bool enable) { gravity_enabled_flag = enable; }
65 bool gravity_enabled() const { return gravity_enabled_flag; }
66
67 /** Set gravity modifier factor to apply to object when enabled */
68 void set_gravity_modifier(float modifier) { gravity_modifier = modifier; }
69
70 Vector get_movement(float dt_sec);
71
72private:
73 /** horizontal and vertical acceleration */
74 float ax, ay;
75
76 /** horizontal and vertical velocity */
77 float vx, vy;
78
79 /** should we respect gravity in our calculations? */
80 bool gravity_enabled_flag;
81
82 /** gravity modifier is multiplied with the sectors gravity */
83 float gravity_modifier;
84};
85
86#endif
87
88/* EOF */
89