- Physic C++ rewrite (Matze Braun)
[supertux.git] / src / physic.cpp
1 //
2 // C Implementation: physic
3 //
4 // Description:
5 //
6 //
7 // Author: Tobias Glaesser <tobi.web@gmx.de>, (C) 2004
8 //
9 // Copyright: See COPYING file that comes with this distribution
10 //
11 //
12 #include <stdio.h>
13
14 #include "scene.h"
15 #include "defines.h"
16 #include "physic.h"
17 #include "timer.h"
18
19 float gravity;
20
21 Physic::Physic()
22     : ax(0), ay(0), vx(0), vy(0), gravity_enabled(true)
23 {
24 }
25
26 Physic::~Physic()
27 {
28 }
29
30 void
31 Physic::reset()
32 {
33     ax = ay = vx = vy = 0;
34     gravity_enabled = true;
35 }
36
37 void
38 Physic::set_velocity(float nvx, float nvy)
39 {
40     vx = nvx;
41     vy = -nvy;
42 }
43
44 float
45 Physic::get_velocity_x()
46 {
47     return vx;
48 }
49
50 float
51 Physic::get_velocity_y()
52 {
53     return -vy;
54 }
55
56 void
57 Physic::set_acceleration(float nax, float nay)
58 {
59     ax = nax;
60     ay = -nay;
61 }
62
63 float
64 Physic::get_acceleration_x()
65 {
66     return ax;
67 }
68
69 float
70 Physic::get_acceleration_y()
71 {
72     return -ay;
73 }
74
75 void
76 Physic::enable_gravity(bool enable_gravity)
77 {
78     gravity_enabled = enable_gravity;
79 }
80
81 void
82 Physic::apply(float &x, float &y)
83 {
84     float grav;
85     if(gravity_enabled)
86         grav = gravity / 100.0;
87     else
88         grav = 0;
89
90     x += vx * frame_ratio + ax * frame_ratio * frame_ratio;
91     y += vy * frame_ratio + (ay + grav) * frame_ratio * frame_ratio;
92     vx += ax * frame_ratio;
93     vy += (ay + grav) * frame_ratio;
94 }