src/supertux/physic.cpp

Go to the documentation of this file.
00001 //  SuperTux
00002 //  Copyright (C) 2004 Tobias Glaesser <tobi.web@gmx.de>
00003 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
00004 //
00005 //  This program is free software: you can redistribute it and/or modify
00006 //  it under the terms of the GNU General Public License as published by
00007 //  the Free Software Foundation, either version 3 of the License, or
00008 //  (at your option) any later version.
00009 //
00010 //  This program is distributed in the hope that it will be useful,
00011 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 //  GNU General Public License for more details.
00014 //
00015 //  You should have received a copy of the GNU General Public License
00016 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00017 
00018 #include "supertux/physic.hpp"
00019 
00020 #include "supertux/sector.hpp"
00021 
00022 Physic::Physic() :
00023   ax(0), ay(0), 
00024   vx(0), vy(0), 
00025   gravity_enabled_flag(true), 
00026   gravity_modifier(1.0f)
00027 {
00028 }
00029 
00030 Physic::~Physic()
00031 {
00032 }
00033 
00034 void
00035 Physic::reset()
00036 {
00037   ax = ay = vx = vy = 0;
00038   gravity_enabled_flag = true;
00039 }
00040 
00041 void
00042 Physic::set_velocity_x(float nvx)
00043 {
00044   vx = nvx;
00045 }
00046 
00047 void
00048 Physic::set_velocity_y(float nvy)
00049 {
00050   vy = nvy;
00051 }
00052 
00053 void
00054 Physic::set_velocity(float nvx, float nvy)
00055 {
00056   vx = nvx;
00057   vy = nvy;
00058 }
00059 
00060 void
00061 Physic::set_velocity(const Vector& vector)
00062 {
00063   vx = vector.x;
00064   vy = vector.y;
00065 }
00066 
00067 void Physic::inverse_velocity_x()
00068 {
00069   vx = -vx;
00070 }
00071 
00072 void Physic::inverse_velocity_y()
00073 {
00074   vy = -vy;
00075 }
00076 
00077 float
00078 Physic::get_velocity_x() const
00079 {
00080   return vx;
00081 }
00082 
00083 float
00084 Physic::get_velocity_y() const
00085 {
00086   return vy;
00087 }
00088 
00089 Vector
00090 Physic::get_velocity() const
00091 {
00092   return Vector(vx, vy);
00093 }
00094 
00095 void
00096 Physic::set_acceleration_x(float nax)
00097 {
00098   ax = nax;
00099 }
00100 
00101 void
00102 Physic::set_acceleration_y(float nay)
00103 {
00104   ay = nay;
00105 }
00106 
00107 void
00108 Physic::set_acceleration(float nax, float nay)
00109 {
00110   ax = nax;
00111   ay = nay;
00112 }
00113 
00114 float
00115 Physic::get_acceleration_x() const
00116 {
00117   return ax;
00118 }
00119 
00120 float
00121 Physic::get_acceleration_y() const
00122 {
00123   return ay;
00124 }
00125 
00126 Vector
00127 Physic::get_acceleration() const
00128 {
00129   return Vector(ax, ay);
00130 }
00131 
00132 void
00133 Physic::enable_gravity(bool enable_gravity)
00134 {
00135   gravity_enabled_flag = enable_gravity;
00136 }
00137 
00138 bool
00139 Physic::gravity_enabled() const
00140 {
00141   return gravity_enabled_flag;
00142 }
00143 
00144 void
00145 Physic::set_gravity_modifier(float gravity_modifier)
00146 {
00147   this->gravity_modifier = gravity_modifier;
00148 }
00149 
00150 Vector
00151 Physic::get_movement(float elapsed_time)
00152 {
00153   float grav = gravity_enabled_flag ? (Sector::current()->get_gravity() * gravity_modifier * 100.0f) : 0;
00154 
00155   // Semi-implicit Euler integration
00156   // with constant acceleration, this will result in a position delta of
00157   // v t + .5 a t (t+elapsed_time) at total time t
00158   vx += ax * elapsed_time;
00159   vy += (ay + grav) * elapsed_time;
00160   Vector result(vx * elapsed_time, vy * elapsed_time);
00161 
00162   return result;
00163 }
00164 
00165 /* EOF */

Generated on Mon Jun 9 03:38:23 2014 for SuperTux by  doxygen 1.5.1