2 // Copyright (C) 2004 Tobias Glaesser <tobi.web@gmx.de>
3 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
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.
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.
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/>.
18 #include "supertux/physic.hpp"
20 #include "supertux/sector.hpp"
25 gravity_enabled_flag(true),
26 gravity_modifier(1.0f)
37 ax = ay = vx = vy = 0;
38 gravity_enabled_flag = true;
42 Physic::set_velocity_x(float nvx)
48 Physic::set_velocity_y(float nvy)
54 Physic::set_velocity(float nvx, float nvy)
61 Physic::set_velocity(const Vector& vector)
67 void Physic::inverse_velocity_x()
72 void Physic::inverse_velocity_y()
78 Physic::get_velocity_x() const
84 Physic::get_velocity_y() const
90 Physic::get_velocity() const
92 return Vector(vx, vy);
96 Physic::set_acceleration_x(float nax)
102 Physic::set_acceleration_y(float nay)
108 Physic::set_acceleration(float nax, float nay)
115 Physic::get_acceleration_x() const
121 Physic::get_acceleration_y() const
127 Physic::get_acceleration() const
129 return Vector(ax, ay);
133 Physic::enable_gravity(bool enable_gravity)
135 gravity_enabled_flag = enable_gravity;
139 Physic::gravity_enabled() const
141 return gravity_enabled_flag;
145 Physic::set_gravity_modifier(float gravity_modifier)
147 this->gravity_modifier = gravity_modifier;
151 Physic::get_movement(float elapsed_time)
153 float grav = gravity_enabled_flag ? (Sector::current()->get_gravity() * gravity_modifier * 100.0f) : 0;
155 // Semi-implicit Euler integration
156 // with constant acceleration, this will result in a position delta of
157 // v t + .5 a t (t+elapsed_time) at total time t
158 vx += ax * elapsed_time;
159 vy += (ay + grav) * elapsed_time;
160 Vector result(vx * elapsed_time, vy * elapsed_time);