Headers should now be correctly handled and get installed in $(includedir)/supertux/ .
[supertux.git] / lib / math / physic.cpp
1 //  $Id$
2 // 
3 //  SuperTux
4 //  Copyright (C) 2004 Tobias Glaesser <tobi.web@gmx.de>
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 // 
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 //  02111-1307, USA.
20
21 #include <cstdio>
22
23 #include "math/physic.h"
24 #include "special/timer.h"
25
26 using namespace SuperTux;
27
28 Physic::Physic()
29     : ax(0), ay(0), vx(0), vy(0), gravity_enabled(true)
30 {
31 }
32
33 Physic::~Physic()
34 {
35 }
36
37 void
38 Physic::reset()
39 {
40     ax = ay = vx = vy = 0;
41     gravity_enabled = true;
42 }
43
44 void
45 Physic::set_velocity_x(float nvx)
46 {
47   vx = nvx;
48 }
49
50 void
51 Physic::set_velocity_y(float nvy)
52 {
53   vy = -nvy;
54 }
55
56 void
57 Physic::set_velocity(float nvx, float nvy)
58 {
59   vx = nvx;
60   vy = -nvy;
61 }
62
63 void Physic::inverse_velocity_x()
64 {
65 vx = -vx;
66 }
67
68 void Physic::inverse_velocity_y()
69 {
70 vy = -vy;
71 }
72
73 float
74 Physic::get_velocity_x()
75 {
76     return vx;
77 }
78
79 float
80 Physic::get_velocity_y()
81 {
82     return -vy;
83 }
84
85 void
86 Physic::set_acceleration_x(float nax)
87 {
88   ax = nax;
89 }
90
91 void
92 Physic::set_acceleration_y(float nay)
93 {
94   ay = -nay;
95 }
96
97 void
98 Physic::set_acceleration(float nax, float nay)
99 {
100     ax = nax;
101     ay = -nay;
102 }
103
104 float
105 Physic::get_acceleration_x()
106 {
107     return ax;
108 }
109
110 float
111 Physic::get_acceleration_y()
112 {
113     return -ay;
114 }
115
116 void
117 Physic::enable_gravity(bool enable_gravity)
118 {
119   gravity_enabled = enable_gravity;
120 }
121
122 void
123 Physic::apply(float elapsed_time, float &x, float &y, float gravity)
124 {
125   float grav;
126   if(gravity_enabled)
127     grav = gravity / 100.0;
128   else
129     grav = 0;
130
131   x += vx * elapsed_time + ax * elapsed_time * elapsed_time;
132   y += vy * elapsed_time + (ay + grav) * elapsed_time * elapsed_time;
133   vx += ax * elapsed_time;
134   vy += (ay + grav) * elapsed_time;
135 }
136
137 void
138 Physic::apply(Vector& vector, float elapsed_time, float gravity)
139 {
140   apply(elapsed_time, vector.x, vector.y, gravity);
141 }
142