edd976cd38e9c1bad6c3b176bd2f374bcef67ee3
[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 "world.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(float nax, float nay)
89 {
90     ax = nax;
91     ay = -nay;
92 }
93
94 float
95 Physic::get_acceleration_x()
96 {
97     return ax;
98 }
99
100 float
101 Physic::get_acceleration_y()
102 {
103     return -ay;
104 }
105
106 void
107 Physic::enable_gravity(bool enable_gravity)
108 {
109   gravity_enabled = enable_gravity;
110 }
111
112 void
113 Physic::apply(float frame_ratio, float &x, float &y)
114 {
115   float gravity = World::current()->get_level()->gravity;
116   float grav;
117   if(gravity_enabled)
118     grav = gravity / 100.0;
119   else
120     grav = 0;
121
122   x += vx * frame_ratio + ax * frame_ratio * frame_ratio;
123   y += vy * frame_ratio + (ay + grav) * frame_ratio * frame_ratio;
124   vx += ax * frame_ratio;
125   vy += (ay + grav) * frame_ratio;
126 }