big refactoring of level and world class. A level is now basically a set of
[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
21 #include <stdio.h>
22
23 #include "scene.h"
24 #include "defines.h"
25 #include "physic.h"
26 #include "timer.h"
27 #include "sector.h"
28 #include "level.h"
29
30 Physic::Physic()
31     : ax(0), ay(0), vx(0), vy(0), gravity_enabled(true)
32 {
33 }
34
35 Physic::~Physic()
36 {
37 }
38
39 void
40 Physic::reset()
41 {
42     ax = ay = vx = vy = 0;
43     gravity_enabled = true;
44 }
45
46 void
47 Physic::set_velocity_x(float nvx)
48 {
49   vx = nvx;
50 }
51
52 void
53 Physic::set_velocity_y(float nvy)
54 {
55   vy = -nvy;
56 }
57
58 void
59 Physic::set_velocity(float nvx, float nvy)
60 {
61   vx = nvx;
62   vy = -nvy;
63 }
64
65 void Physic::inverse_velocity_x()
66 {
67 vx = -vx;
68 }
69
70 void Physic::inverse_velocity_y()
71 {
72 vy = -vy;
73 }
74
75 float
76 Physic::get_velocity_x()
77 {
78     return vx;
79 }
80
81 float
82 Physic::get_velocity_y()
83 {
84     return -vy;
85 }
86
87 void
88 Physic::set_acceleration_x(float nax)
89 {
90   ax = nax;
91 }
92
93 void
94 Physic::set_acceleration_y(float nay)
95 {
96   ay = -nay;
97 }
98
99 void
100 Physic::set_acceleration(float nax, float nay)
101 {
102     ax = nax;
103     ay = -nay;
104 }
105
106 float
107 Physic::get_acceleration_x()
108 {
109     return ax;
110 }
111
112 float
113 Physic::get_acceleration_y()
114 {
115     return -ay;
116 }
117
118 void
119 Physic::enable_gravity(bool enable_gravity)
120 {
121   gravity_enabled = enable_gravity;
122 }
123
124 void
125 Physic::apply(float frame_ratio, float &x, float &y)
126 {
127   float gravity = Sector::current()->gravity;
128   float grav;
129   if(gravity_enabled)
130     grav = gravity / 100.0;
131   else
132     grav = 0;
133
134   x += vx * frame_ratio + ax * frame_ratio * frame_ratio;
135   y += vy * frame_ratio + (ay + grav) * frame_ratio * frame_ratio;
136   vx += ax * frame_ratio;
137   vy += (ay + grav) * frame_ratio;
138 }