e9fe94d97c8df58b06d023e6c589bb9f0915d049
[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 Physic::inverse_velocity_x()
60 {
61 vx = -vx;
62 }
63
64 void Physic::inverse_velocity_y()
65 {
66 vy = -vy;
67 }
68
69 float
70 Physic::get_velocity_x()
71 {
72     return vx;
73 }
74
75 float
76 Physic::get_velocity_y()
77 {
78     return -vy;
79 }
80
81 void
82 Physic::set_acceleration_x(float nax)
83 {
84   ax = nax;
85 }
86
87 void
88 Physic::set_acceleration_y(float nay)
89 {
90   ay = -nay;
91 }
92
93 void
94 Physic::set_acceleration(float nax, float nay)
95 {
96     ax = nax;
97     ay = -nay;
98 }
99
100 float
101 Physic::get_acceleration_x()
102 {
103     return ax;
104 }
105
106 float
107 Physic::get_acceleration_y()
108 {
109     return -ay;
110 }
111
112 void
113 Physic::enable_gravity(bool enable_gravity)
114 {
115   gravity_enabled_flag = enable_gravity;
116 }
117
118 bool
119 Physic::gravity_enabled() const
120 {
121   return gravity_enabled_flag;
122 }
123
124 Vector
125 Physic::get_movement(float elapsed_time)
126 {
127   float grav = gravity_enabled_flag ? 1000 : 0;
128   
129   Vector result(
130       vx * elapsed_time + ax * elapsed_time * elapsed_time,
131       vy * elapsed_time + (ay + grav) * elapsed_time * elapsed_time
132   );
133   vx += ax * elapsed_time;
134   vy += (ay + grav) * elapsed_time;  
135
136   return result;
137 }
138