bae4dd3a05cb78776836306bfa3892f5aafffc5d
[supertux.git] / lib / math / physic.h
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 #ifndef SUPERTUX_PHYSIC_H
22 #define SUPERTUX_PHYSIC_H
23
24 #include "math/vector.h"
25
26 namespace SuperTux
27   {
28
29   /// Physics engine.
30   /** This is a very simplistic physics engine handling accelerated and constant
31     * movement along with gravity.
32     */
33   class Physic
34     {
35     public:
36       Physic();
37       ~Physic();
38
39       /// Resets all velocities and accelerations to 0.
40       void reset();
41
42       /// Sets velocity to a fixed value.
43       void set_velocity(float vx, float vy);
44
45       void set_velocity_x(float vx);
46       void set_velocity_y(float vy);
47
48       /// Velocities invertion.
49       void inverse_velocity_x();
50       void inverse_velocity_y();
51
52       float get_velocity_x();
53       float get_velocity_y();
54
55       /// Set acceleration.
56       /** Sets acceleration applied to the object. (Note that gravity is
57        * eventually added to the vertical acceleration)
58        */
59       void set_acceleration(float ax, float ay);
60
61       void set_acceleration_x(float ax);
62       void set_acceleration_y(float ay);
63
64       float get_acceleration_x();
65       float get_acceleration_y();
66
67       /// Enables or disables handling of gravity.
68       void enable_gravity(bool gravity_enabled);
69
70       /// Applies the physical simulation to given x and y coordinates.
71       void apply(float frame_ratio, float &x, float &y, float gravity = 10.0f);
72
73       /// applies the physical simulation to given x and y coordinates.
74       void apply(Vector& vector, float frame_ratio, float gravity = 10.0f);
75
76     private:
77       /// horizontal and vertical acceleration
78       float ax, ay;
79       /// horizontal and vertical velocity
80       float vx, vy;
81       /// should we respect gravity in out calculations?
82       bool gravity_enabled;
83     };
84
85 } //namespace SuperTux
86
87 #endif /*SUPERTUX_PHYSIC_H*/