- Yet another try in the endless quest for perfect collision detection.
[supertux.git] / src / 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 #include <config.h>
21
22 #include "physic.hpp"
23
24 Physic::Physic()
25     : ax(0), ay(0), vx(0), vy(0), gravity_enabled_flag(true)
26 {
27 }
28
29 Physic::~Physic()
30 {
31 }
32
33 void
34 Physic::reset()
35 {
36     ax = ay = vx = vy = 0;
37     gravity_enabled_flag = true;
38 }
39
40 void
41 Physic::set_velocity_x(float nvx)
42 {
43   vx = nvx;
44 }
45
46 void
47 Physic::set_velocity_y(float nvy)
48 {
49   vy = -nvy;
50 }
51
52 void
53 Physic::set_velocity(float nvx, float nvy)
54 {
55   vx = nvx;
56   vy = -nvy;
57 }
58
59 void
60 Physic::set_velocity(const Vector& vector)
61 {
62   vx = vector.x;
63   vy = vector.y;
64 }
65
66 void Physic::inverse_velocity_x()
67 {
68   vx = -vx;
69 }
70
71 void Physic::inverse_velocity_y()
72 {
73   vy = -vy;
74 }
75
76 float
77 Physic::get_velocity_x() const
78 {
79     return vx;
80 }
81
82 float
83 Physic::get_velocity_y() const
84 {
85     return -vy;
86 }
87
88 Vector
89 Physic::get_velocity() const
90 {
91   return Vector(vx, -vy);
92 }
93
94 void
95 Physic::set_acceleration_x(float nax)
96 {
97   ax = nax;
98 }
99
100 void
101 Physic::set_acceleration_y(float nay)
102 {
103   ay = -nay;
104 }
105
106 void
107 Physic::set_acceleration(float nax, float nay)
108 {
109   ax = nax;
110   ay = -nay;
111 }
112
113 float
114 Physic::get_acceleration_x() const
115 {
116   return ax;
117 }
118
119 float
120 Physic::get_acceleration_y() const
121 {
122   return -ay;
123 }
124
125 Vector
126 Physic::get_acceleration() const
127 {
128   return Vector(ax, -ay);
129 }
130
131 void
132 Physic::enable_gravity(bool enable_gravity)
133 {
134   gravity_enabled_flag = enable_gravity;
135 }
136
137 bool
138 Physic::gravity_enabled() const
139 {
140   return gravity_enabled_flag;
141 }
142
143 Vector
144 Physic::get_movement(float elapsed_time)
145 {
146   float grav = gravity_enabled_flag ? 1000 : 0;
147   
148   Vector result(
149       vx * elapsed_time + ax * elapsed_time * elapsed_time,
150       vy * elapsed_time + (ay + grav) * elapsed_time * elapsed_time
151   );
152   vx += ax * elapsed_time;
153   vy += (ay + grav) * elapsed_time;  
154
155   return result;
156 }
157