Generated SuperTux libtool library containing more general source, that could prove...
[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 Physic::Physic()
27     : ax(0), ay(0), vx(0), vy(0), gravity_enabled(true)
28 {
29 }
30
31 Physic::~Physic()
32 {
33 }
34
35 void
36 Physic::reset()
37 {
38     ax = ay = vx = vy = 0;
39     gravity_enabled = true;
40 }
41
42 void
43 Physic::set_velocity_x(float nvx)
44 {
45   vx = nvx;
46 }
47
48 void
49 Physic::set_velocity_y(float nvy)
50 {
51   vy = -nvy;
52 }
53
54 void
55 Physic::set_velocity(float nvx, float nvy)
56 {
57   vx = nvx;
58   vy = -nvy;
59 }
60
61 void Physic::inverse_velocity_x()
62 {
63 vx = -vx;
64 }
65
66 void Physic::inverse_velocity_y()
67 {
68 vy = -vy;
69 }
70
71 float
72 Physic::get_velocity_x()
73 {
74     return vx;
75 }
76
77 float
78 Physic::get_velocity_y()
79 {
80     return -vy;
81 }
82
83 void
84 Physic::set_acceleration_x(float nax)
85 {
86   ax = nax;
87 }
88
89 void
90 Physic::set_acceleration_y(float nay)
91 {
92   ay = -nay;
93 }
94
95 void
96 Physic::set_acceleration(float nax, float nay)
97 {
98     ax = nax;
99     ay = -nay;
100 }
101
102 float
103 Physic::get_acceleration_x()
104 {
105     return ax;
106 }
107
108 float
109 Physic::get_acceleration_y()
110 {
111     return -ay;
112 }
113
114 void
115 Physic::enable_gravity(bool enable_gravity)
116 {
117   gravity_enabled = enable_gravity;
118 }
119
120 void
121 Physic::apply(float elapsed_time, float &x, float &y, float& gravity)
122 {
123   float grav;
124   if(gravity_enabled)
125     grav = gravity / 100.0;
126   else
127     grav = 0;
128
129   x += vx * elapsed_time + ax * elapsed_time * elapsed_time;
130   y += vy * elapsed_time + (ay + grav) * elapsed_time * elapsed_time;
131   vx += ax * elapsed_time;
132   vy += (ay + grav) * elapsed_time;
133 }
134
135 void
136 Physic::apply(Vector& vector, float elapsed_time, float& gravity)
137 {
138   apply(elapsed_time, vector.x, vector.y, gravity);
139 }
140