Made code -Wshadow clean, missed a bunch of issues in the last commit
[supertux.git] / src / supertux / physic.cpp
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 Physic::~Physic()
31 {
32 }
33
34 void
35 Physic::reset()
36 {
37   ax = ay = vx = vy = 0;
38   gravity_enabled_flag = true;
39 }
40
41 void
42 Physic::set_velocity_x(float nvx)
43 {
44   vx = nvx;
45 }
46
47 void
48 Physic::set_velocity_y(float nvy)
49 {
50   vy = nvy;
51 }
52
53 void
54 Physic::set_velocity(float nvx, float nvy)
55 {
56   vx = nvx;
57   vy = nvy;
58 }
59
60 void
61 Physic::set_velocity(const Vector& vector)
62 {
63   vx = vector.x;
64   vy = vector.y;
65 }
66
67 void Physic::inverse_velocity_x()
68 {
69   vx = -vx;
70 }
71
72 void Physic::inverse_velocity_y()
73 {
74   vy = -vy;
75 }
76
77 float
78 Physic::get_velocity_x() const
79 {
80   return vx;
81 }
82
83 float
84 Physic::get_velocity_y() const
85 {
86   return vy;
87 }
88
89 Vector
90 Physic::get_velocity() const
91 {
92   return Vector(vx, vy);
93 }
94
95 void
96 Physic::set_acceleration_x(float nax)
97 {
98   ax = nax;
99 }
100
101 void
102 Physic::set_acceleration_y(float nay)
103 {
104   ay = nay;
105 }
106
107 void
108 Physic::set_acceleration(float nax, float nay)
109 {
110   ax = nax;
111   ay = nay;
112 }
113
114 float
115 Physic::get_acceleration_x() const
116 {
117   return ax;
118 }
119
120 float
121 Physic::get_acceleration_y() const
122 {
123   return ay;
124 }
125
126 Vector
127 Physic::get_acceleration() const
128 {
129   return Vector(ax, ay);
130 }
131
132 void
133 Physic::enable_gravity(bool enable_gravity_)
134 {
135   gravity_enabled_flag = enable_gravity_;
136 }
137
138 bool
139 Physic::gravity_enabled() const
140 {
141   return gravity_enabled_flag;
142 }
143
144 void
145 Physic::set_gravity_modifier(float gravity_modifier_)
146 {
147   this->gravity_modifier = gravity_modifier_;
148 }
149
150 Vector
151 Physic::get_movement(float elapsed_time)
152 {
153   float grav = gravity_enabled_flag ? (Sector::current()->get_gravity() * gravity_modifier * 100.0f) : 0;
154
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);
161
162   return result;
163 }
164
165 /* EOF */