518ce8349b7556cb1bcca0b222198feb5d5b2f68
[supertux.git] / src / physic.cpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2004 Tobias Glaesser <tobi.web@gmx.de>
5 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
6 //
7 //  This program is free software; you can redistribute it and/or
8 //  modify it under the terms of the GNU General Public License
9 //  as published by the Free Software Foundation; either version 2
10 //  of the License, or (at your option) any later version.
11 //
12 //  This program is distributed in the hope that it will be useful,
13 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
14 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 //  GNU General Public License for more details.
16 //
17 //  You should have received a copy of the GNU General Public License
18 //  along with this program; if not, write to the Free Software
19 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 //  02111-1307, USA.
21 #include <config.h>
22
23 #include "physic.hpp"
24
25 Physic::Physic()
26     : ax(0), ay(0), vx(0), vy(0), gravity_enabled_flag(true)
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 Vector
145 Physic::get_movement(float elapsed_time)
146 {
147   float grav = gravity_enabled_flag ? 1000 : 0;
148   
149   Vector result(
150       vx * elapsed_time + ax * elapsed_time * elapsed_time,
151       vy * elapsed_time + (ay + grav) * elapsed_time * elapsed_time
152   );
153   vx += ax * elapsed_time;
154   vy += (ay + grav) * elapsed_time;  
155
156   return result;
157 }
158