476565f9762037dceffbfddbf2c68261e3aecc72
[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(float nvx, float nvy)
48 {
49     vx = nvx;
50     vy = -nvy;
51 }
52
53 void Physic::inverse_velocity_x()
54 {
55 vx = -vx;
56 }
57
58 void Physic::inverse_velocity_y()
59 {
60 vy = -vy;
61 }
62
63 float
64 Physic::get_velocity_x()
65 {
66     return vx;
67 }
68
69 float
70 Physic::get_velocity_y()
71 {
72     return -vy;
73 }
74
75 void
76 Physic::set_acceleration(float nax, float nay)
77 {
78     ax = nax;
79     ay = -nay;
80 }
81
82 float
83 Physic::get_acceleration_x()
84 {
85     return ax;
86 }
87
88 float
89 Physic::get_acceleration_y()
90 {
91     return -ay;
92 }
93
94 void
95 Physic::enable_gravity(bool enable_gravity)
96 {
97   gravity_enabled = enable_gravity;
98 }
99
100 void
101 Physic::apply(float frame_ratio, float &x, float &y)
102 {
103   float gravity = World::current()->get_level()->gravity;
104   float grav;
105   if(gravity_enabled)
106     grav = gravity / 100.0;
107   else
108     grav = 0;
109
110   x += vx * frame_ratio + ax * frame_ratio * frame_ratio;
111   y += vy * frame_ratio + (ay + grav) * frame_ratio * frame_ratio;
112   vx += ax * frame_ratio;
113   vy += (ay + grav) * frame_ratio;
114 }