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 | #include "supertux/physic.hpp" |
19 | |
20 | #include "supertux/sector.hpp" |
21 | |
22 | Physic::Physic() : |
23 | ax(0), ay(0), |
24 | vx(0), vy(0), |
25 | gravity_enabled_flag(true), |
26 | gravity_modifier(1.0f) |
27 | { |
28 | } |
29 | |
30 | void |
31 | Physic::reset() |
32 | { |
33 | ax = ay = vx = vy = 0; |
34 | gravity_enabled_flag = true; |
35 | } |
36 | |
37 | void |
38 | Physic::set_velocity(float nvx, float nvy) |
39 | { |
40 | vx = nvx; |
41 | vy = nvy; |
42 | } |
43 | |
44 | void |
45 | Physic::set_velocity(const Vector& vector) |
46 | { |
47 | vx = vector.x; |
48 | vy = vector.y; |
49 | } |
50 | |
51 | void |
52 | Physic::set_acceleration(float nax, float nay) |
53 | { |
54 | ax = nax; |
55 | ay = nay; |
56 | } |
57 | |
58 | Vector |
59 | Physic::get_movement(float dt_sec) |
60 | { |
61 | float grav = gravity_enabled_flag ? (Sector::get().get_gravity() * gravity_modifier * 100.0f) : 0; |
62 | |
63 | // Semi-implicit Euler integration |
64 | // with constant acceleration, this will result in a position delta of |
65 | // v t + .5 a t (t+dt_sec) at total time t |
66 | vx += ax * dt_sec; |
67 | vy += (ay + grav) * dt_sec; |
68 | Vector result(vx * dt_sec, vy * dt_sec); |
69 | |
70 | return result; |
71 | } |
72 | |
73 | /* EOF */ |
74 | |