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