[Super-tux-commit] supertux/lib/math physic.cpp,1.2,1.3 physic.h,1.3,1.4 vector.cpp,1.1,1.2 vector.h
Brought to you by:
wkendrick
From: Tobias G. <to...@us...> - 2004-07-21 16:52:01
|
Update of /cvsroot/super-tux/supertux/lib/math In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv32238/lib/math Modified Files: physic.cpp physic.h vector.cpp vector.h Log Message: The SuperTux library features a SuperTux namespace now. + minor Bugfixes and cleanups Index: vector.cpp =================================================================== RCS file: /cvsroot/super-tux/supertux/lib/math/vector.cpp,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -r1.1 -r1.2 --- vector.cpp 20 Jul 2004 17:51:36 -0000 1.1 +++ vector.cpp 21 Jul 2004 16:51:52 -0000 1.2 @@ -21,6 +21,8 @@ #include "math/vector.h" +using namespace SuperTux; + Vector Vector::unit() const { return *this / norm(); Index: vector.h =================================================================== RCS file: /cvsroot/super-tux/supertux/lib/math/vector.h,v retrieving revision 1.2 retrieving revision 1.3 diff -u -d -r1.2 -r1.3 --- vector.h 20 Jul 2004 20:21:16 -0000 1.2 +++ vector.h 21 Jul 2004 16:51:52 -0000 1.3 @@ -20,79 +20,84 @@ #ifndef SUPERTUX_VECTOR_H #define SUPERTUX_VECTOR_H -/// 2D Vector. -/** Simple two dimensional vector. */ -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 +namespace SuperTux { - return x == other.x && y == other.y; - } - const Vector& operator=(const Vector& other) - { - x = other.x; - y = other.y; - return *this; - } + /// 2D Vector. + /** Simple two dimensional vector. */ + 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) + { } - Vector operator+(const Vector& other) const - { - return Vector(x + other.x, y + other.y); - } + bool operator ==(const Vector& other) const + { + return x == other.x && y == other.y; + } - Vector operator-(const Vector& other) const - { - return Vector(x - other.x, y - other.y); - } + const Vector& operator=(const Vector& other) + { + x = other.x; + y = other.y; + return *this; + } - Vector operator*(float s) const - { - return Vector(x * s, y * s); - } + 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-(const Vector& other) const + { + return Vector(x - other.x, y - other.y); + } - Vector operator-() const - { - return Vector(-x, -y); - } + Vector operator*(float s) const + { + return Vector(x * s, y * s); + } - const Vector& operator +=(const Vector& other) - { - x += other.x; - y += other.y; - return *this; - } + Vector operator/(float s) const + { + return Vector(x / s, y / s); + } - /// Scalar product of 2 vectors - float operator*(const Vector& other) const - { - return x*other.x + y*other.y; - } + Vector operator-() const + { + return Vector(-x, -y); + } - float norm() const; - Vector unit() const; + const Vector& operator +=(const Vector& other) + { + x += other.x; + y += other.y; + return *this; + } - // ... add the other operators as needed, I'm too lazy now ... + /// Scalar product of 2 vectors + float operator*(const Vector& other) const + { + return x*other.x + y*other.y; + } - float x, y; // leave this public, get/set methods just give me headaches - // for such simple stuff :) -}; + 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 :) + }; + +} //namespace SuperTux #endif /*SUPERTUX_VECTOR_H*/ Index: physic.cpp =================================================================== RCS file: /cvsroot/super-tux/supertux/lib/math/physic.cpp,v retrieving revision 1.2 retrieving revision 1.3 diff -u -d -r1.2 -r1.3 --- physic.cpp 20 Jul 2004 22:55:50 -0000 1.2 +++ physic.cpp 21 Jul 2004 16:51:51 -0000 1.3 @@ -23,6 +23,8 @@ #include "math/physic.h" #include "special/timer.h" +using namespace SuperTux; + Physic::Physic() : ax(0), ay(0), vx(0), vy(0), gravity_enabled(true) { Index: physic.h =================================================================== RCS file: /cvsroot/super-tux/supertux/lib/math/physic.h,v retrieving revision 1.3 retrieving revision 1.4 diff -u -d -r1.3 -r1.4 --- physic.h 20 Jul 2004 22:55:50 -0000 1.3 +++ physic.h 21 Jul 2004 16:51:52 -0000 1.4 @@ -23,60 +23,65 @@ #include "math/vector.h" -/// Physics engine. -/** This is a very simplistic physics engine handling accelerated and constant - * movement along with gravity. - */ -class Physic -{ -public: - Physic(); - ~Physic(); +namespace SuperTux + { - /// Resets all velocities and accelerations to 0. - void reset(); + /// Physics engine. + /** This is a very simplistic physics engine handling accelerated and constant + * movement along with gravity. + */ + class Physic + { + public: + Physic(); + ~Physic(); - /// Sets velocity to a fixed value. - void set_velocity(float vx, float vy); + /// Resets all velocities and accelerations to 0. + void reset(); - void set_velocity_x(float vx); - void set_velocity_y(float vy); + /// Sets velocity to a fixed value. + void set_velocity(float vx, float vy); - /// Velocities invertion. - void inverse_velocity_x(); - void inverse_velocity_y(); + void set_velocity_x(float vx); + void set_velocity_y(float vy); - float get_velocity_x(); - float get_velocity_y(); - - /// Set acceleration. - /** Sets acceleration applied to the object. (Note that gravity is - * eventually added to the vertical acceleration) - */ - void set_acceleration(float ax, float ay); + /// Velocities invertion. + void inverse_velocity_x(); + void inverse_velocity_y(); - void set_acceleration_x(float ax); - void set_acceleration_y(float ay); + float get_velocity_x(); + float get_velocity_y(); - float get_acceleration_x(); - float get_acceleration_y(); + /// Set acceleration. + /** Sets acceleration applied to the object. (Note that gravity is + * eventually added to the vertical acceleration) + */ + void set_acceleration(float ax, float ay); - /// Enables or disables handling of gravity. - void enable_gravity(bool gravity_enabled); + void set_acceleration_x(float ax); + void set_acceleration_y(float ay); - /// Applies the physical simulation to given x and y coordinates. - void apply(float frame_ratio, float &x, float &y, float gravity = 10.0f); + float get_acceleration_x(); + float get_acceleration_y(); - /// applies the physical simulation to given x and y coordinates. - void apply(Vector& vector, float frame_ratio, float gravity = 10.0f); + /// Enables or disables handling of gravity. + void enable_gravity(bool gravity_enabled); -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; -}; + /// Applies the physical simulation to given x and y coordinates. + void apply(float frame_ratio, float &x, float &y, float gravity = 10.0f); + + /// applies the physical simulation to given x and y coordinates. + void apply(Vector& vector, float frame_ratio, float gravity = 10.0f); + + 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; + }; + +} //namespace SuperTux #endif /*SUPERTUX_PHYSIC_H*/ |