[Super-tux-commit] supertux/lib/math physic.cpp,NONE,1.1 physic.h,NONE,1.1 vector.cpp,NONE,1.1 vecto
Brought to you by:
wkendrick
From: Tobias G. <to...@us...> - 2004-07-20 17:51:46
|
Update of /cvsroot/super-tux/supertux/lib/math In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv19557/lib/math Added Files: physic.cpp physic.h vector.cpp vector.h Log Message: Generated SuperTux libtool library containing more general source, that could prove useful for other applications/games. Caution: It's not yet SuperTux independed, more work on this will follow, that's just the first step. The file structure isn't fixed, better ideas will surely find there way in it! --- NEW FILE: vector.cpp --- // $Id: vector.cpp,v 1.1 2004/07/20 17:51:36 tobgle Exp $ // // SuperTux - A Jump'n Run // Copyright (C) 2004 Matthias Braun <ma...@br... // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License // as published by the Free Software Foundation; either version 2 // of the License, or (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include <cmath> #include "math/vector.h" Vector Vector::unit() const { return *this / norm(); } float Vector::norm() const { return sqrt(x*x + y*y); } --- NEW FILE: vector.h --- // $Id: vector.h,v 1.1 2004/07/20 17:51:36 tobgle Exp $ // // SuperTux - A Jump'n Run // Copyright (C) 2004 Matthias Braun <ma...@br... // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License // as published by the Free Software Foundation; either version 2 // of the License, or (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #ifndef SUPERTUX_VECTOR_H #define SUPERTUX_VECTOR_H class Vector { public: Vector(float nx, float ny) : x(nx), y(ny) { } Vector(const Vector& other) : x(other.x), y(other.y) { } Vector() : x(0), y(0) { } bool operator ==(const Vector& other) const { return x == other.x && y == other.y; } const Vector& operator=(const Vector& other) { x = other.x; y = other.y; return *this; } Vector operator+(const Vector& other) const { return Vector(x + other.x, y + other.y); } Vector operator-(const Vector& other) const { return Vector(x - other.x, y - other.y); } Vector operator*(float s) const { return Vector(x * s, y * s); } Vector operator/(float s) const { return Vector(x / s, y / s); } Vector operator-() const { return Vector(-x, -y); } const Vector& operator +=(const Vector& other) { x += other.x; y += other.y; return *this; } // scalar product of 2 vectors float operator*(const Vector& other) const { return x*other.x + y*other.y; } float norm() const; Vector unit() const; // ... add the other operators as needed, I'm too lazy now ... float x, y; // leave this public, get/set methods just give me headaches // for such simple stuff :) }; #endif /*SUPERTUX_VECTOR_H*/ --- NEW FILE: physic.cpp --- // $Id: physic.cpp,v 1.1 2004/07/20 17:51:36 tobgle Exp $ // // SuperTux // Copyright (C) 2004 Tobias Glaesser <tob...@gm...> // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License // as published by the Free Software Foundation; either version 2 // of the License, or (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA // 02111-1307, USA. #include <cstdio> #include "math/physic.h" #include "special/timer.h" Physic::Physic() : ax(0), ay(0), vx(0), vy(0), gravity_enabled(true) { } Physic::~Physic() { } void Physic::reset() { ax = ay = vx = vy = 0; gravity_enabled = true; } void Physic::set_velocity_x(float nvx) { vx = nvx; } void Physic::set_velocity_y(float nvy) { vy = -nvy; } void Physic::set_velocity(float nvx, float nvy) { vx = nvx; vy = -nvy; } void Physic::inverse_velocity_x() { vx = -vx; } void Physic::inverse_velocity_y() { vy = -vy; } float Physic::get_velocity_x() { return vx; } float Physic::get_velocity_y() { return -vy; } void Physic::set_acceleration_x(float nax) { ax = nax; } void Physic::set_acceleration_y(float nay) { ay = -nay; } void Physic::set_acceleration(float nax, float nay) { ax = nax; ay = -nay; } float Physic::get_acceleration_x() { return ax; } float Physic::get_acceleration_y() { return -ay; } void Physic::enable_gravity(bool enable_gravity) { gravity_enabled = enable_gravity; } void Physic::apply(float elapsed_time, float &x, float &y, float& gravity) { float grav; if(gravity_enabled) grav = gravity / 100.0; else grav = 0; x += vx * elapsed_time + ax * elapsed_time * elapsed_time; y += vy * elapsed_time + (ay + grav) * elapsed_time * elapsed_time; vx += ax * elapsed_time; vy += (ay + grav) * elapsed_time; } void Physic::apply(Vector& vector, float elapsed_time, float& gravity) { apply(elapsed_time, vector.x, vector.y, gravity); } --- NEW FILE: physic.h --- // $Id: physic.h,v 1.1 2004/07/20 17:51:36 tobgle Exp $ // // SuperTux // Copyright (C) 2004 Tobias Glaesser <tob...@gm...> // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License // as published by the Free Software Foundation; either version 2 // of the License, or (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA // 02111-1307, USA. #ifndef SUPERTUX_PHYSIC_H #define SUPERTUX_PHYSIC_H #include "math/vector.h" /** This is a very simplistic physics engine handling accelerated and constant * movement along with gravity. */ class Physic { public: Physic(); ~Physic(); /** resets all velocities and accelerations to 0 */ void reset(); /** sets velocity to a fixed value */ void set_velocity(float vx, float vy); void set_velocity_x(float vx); void set_velocity_y(float vy); /** velocities invertion */ void inverse_velocity_x(); void inverse_velocity_y(); float get_velocity_x(); float get_velocity_y(); /** sets acceleration applied to the object. (Note that gravity is * eventually added to the vertical acceleration) */ void set_acceleration(float ax, float ay); void set_acceleration_x(float ax); void set_acceleration_y(float ay); float get_acceleration_x(); float get_acceleration_y(); /** enables or disables handling of gravity */ void enable_gravity(bool gravity_enabled); /** applies the physical simulation to given x and y coordinates */ void apply(float frame_ratio, float &x, float &y, float& gravity = 10); /** applies the physical simulation to given x and y coordinates */ void apply(Vector& vector, float frame_ratio, float& gravity = 10); private: /// horizontal and vertical acceleration float ax, ay; /// horizontal and vertical velocity float vx, vy; /// should we respect gravity in out calculations? bool gravity_enabled; }; #endif /*SUPERTUX_PHYSIC_H*/ |