Physic Class Reference

This is a very simplistic physics engine handling accelerated and constant movement along with gravity. More...

#include <physic.hpp>

List of all members.

Public Member Functions

 Physic ()
 ~Physic ()
void reset ()
 Resets all velocities and accelerations to 0.
void set_velocity (float vx, float vy)
 Sets velocity to a fixed value.
void set_velocity (const Vector &vector)
void set_velocity_x (float vx)
void set_velocity_y (float vy)
void inverse_velocity_x ()
 Velocity inversion.
void inverse_velocity_y ()
Vector get_velocity () const
float get_velocity_x () const
float get_velocity_y () const
void set_acceleration (float ax, float ay)
 Sets acceleration applied to the object.
void set_acceleration_x (float ax)
void set_acceleration_y (float ay)
Vector get_acceleration () const
float get_acceleration_x () const
float get_acceleration_y () const
void enable_gravity (bool gravity_enabled)
 Enables or disables handling of gravity.
bool gravity_enabled () const
void set_gravity_modifier (float gravity)
 Set gravity modifier factor to apply to object when enabled.
Vector get_movement (float elapsed_time)

Private Attributes

float ax
 horizontal and vertical acceleration
float ay
float vx
 horizontal and vertical velocity
float vy
bool gravity_enabled_flag
 should we respect gravity in our calculations?
float gravity_modifier
 gravity modifier is multiplied with the sectors gravity


Detailed Description

This is a very simplistic physics engine handling accelerated and constant movement along with gravity.

Definition at line 27 of file physic.hpp.


Constructor & Destructor Documentation

Physic::Physic (  ) 

Definition at line 22 of file physic.cpp.

00022                :
00023   ax(0), ay(0), 
00024   vx(0), vy(0), 
00025   gravity_enabled_flag(true), 
00026   gravity_modifier(1.0f)
00027 {
00028 }

Physic::~Physic (  ) 

Definition at line 30 of file physic.cpp.

00031 {
00032 }


Member Function Documentation

void Physic::reset (  ) 

Resets all velocities and accelerations to 0.

Definition at line 35 of file physic.cpp.

References ax, ay, gravity_enabled_flag, vx, and vy.

Referenced by Player::init(), Player::move(), ScriptedObject::set_pos(), and IceCrusher::set_state().

00036 {
00037   ax = ay = vx = vy = 0;
00038   gravity_enabled_flag = true;
00039 }

void Physic::set_velocity ( float  vx,
float  vy 
)

Sets velocity to a fixed value.

Definition at line 54 of file physic.cpp.

References vx, and vy.

Referenced by Player::add_velocity(), WalkingBadguy::add_velocity(), Rock::collision_solid(), Player::handle_horizontal_input(), Player::handle_input_climbing(), Player::handle_input_ghost(), MoleRock::initialize(), Player::kill(), OneUp::OneUp(), Star::Star(), Player::start_climbing(), Player::stop_climbing(), Rock::ungrab(), and ScriptedObject::update().

00055 {
00056   vx = nvx;
00057   vy = nvy;
00058 }

void Physic::set_velocity ( const Vector vector  ) 

Definition at line 61 of file physic.cpp.

References vx, vy, Vector::x, and Vector::y.

00062 {
00063   vx = vector.x;
00064   vy = vector.y;
00065 }

void Physic::set_velocity_x ( float  vx  ) 

Definition at line 42 of file physic.cpp.

References vx.

Referenced by Yeti::active_update(), WalkingBadguy::active_update(), Snail::active_update(), Plant::active_update(), Kugelblitz::active_update(), CaptainSnowball::active_update(), AngryStone::active_update(), Player::add_velocity(), AngryStone::AngryStone(), Player::apply_friction(), Yeti::be_angry(), Snail::be_flat(), Snail::be_kicked(), Bullet::Bullet(), Stumpy::collision_badguy(), Plant::collision_badguy(), Star::collision_solid(), ScriptedObject::collision_solid(), Rock::collision_solid(), PowerUp::collision_solid(), Player::collision_solid(), GrowUp::collision_solid(), Bullet::collision_solid(), Toad::collision_solid(), Stumpy::collision_solid(), Snail::collision_solid(), SkyDive::collision_solid(), SkullyHop::collision_solid(), Plant::collision_solid(), Owl::collision_solid(), MrIceBlock::collision_solid(), CaptainSnowball::collision_solid(), BouncingSnowball::collision_solid(), BadGuy::collision_solid(), AngryStone::collision_solid(), Haywire::collision_squished(), Player::deactivate(), FallingCoin::FallingCoin(), WalkingBadguy::freeze(), SkyDive::grab(), GrowUp::GrowUp(), Kugelblitz::hit(), Zeekling::initialize(), WalkingBadguy::initialize(), Totem::initialize(), Stumpy::initialize(), SSpiky::initialize(), Plant::initialize(), Owl::initialize(), Kugelblitz::initialize(), KamikazeSnowball::initialize(), Dart::initialize(), BouncingSnowball::initialize(), Yeti::jump_down(), Yeti::jump_up(), Player::kill(), KamikazeSnowball::kill_collision(), BadGuy::kill_squished(), Zeekling::onBumpHorizontal(), Yeti::run(), Toad::set_state(), SkullyHop::set_state(), MrIceBlock::set_state(), Stalactite::squish(), Totem::synchronize_with(), Yeti::take_hit(), WalkingBadguy::turn_around(), Rock::update(), Player::update(), and Player::walk().

00043 {
00044   vx = nvx;
00045 }

void Physic::set_velocity_y ( float  vy  ) 

Definition at line 48 of file physic.cpp.

References vy.

Referenced by Zeekling::active_update(), Yeti::active_update(), Totem::active_update(), SpiderMite::active_update(), Snail::active_update(), FlyingSnowBall::active_update(), Fish::active_update(), CaptainSnowball::active_update(), AngryStone::active_update(), Player::add_velocity(), AngryStone::AngryStone(), Yeti::be_angry(), Snail::be_flat(), Snail::be_kicked(), Player::bounce(), CaptainSnowball::CaptainSnowball(), Trampoline::collision(), PushButton::collision(), Stumpy::collision_badguy(), Star::collision_solid(), ScriptedObject::collision_solid(), Rock::collision_solid(), PowerUp::collision_solid(), Player::collision_solid(), GrowUp::collision_solid(), Bullet::collision_solid(), Yeti::collision_solid(), WalkingBadguy::collision_solid(), Totem::collision_solid(), Toad::collision_solid(), Stumpy::collision_solid(), Stalactite::collision_solid(), SpiderMite::collision_solid(), Snail::collision_solid(), SkullyHop::collision_solid(), Plant::collision_solid(), Owl::collision_solid(), MrIceBlock::collision_solid(), KamikazeSnowball::collision_solid(), FlyingSnowBall::collision_solid(), BouncingSnowball::collision_solid(), Bomb::collision_solid(), BadGuy::collision_solid(), AngryStone::collision_solid(), Snowman::collision_squished(), FlyingSnowBall::collision_squished(), Player::deactivate(), Player::do_jump(), GrowUp::do_jump(), FallingCoin::FallingCoin(), Jumpy::freeze(), SkyDive::grab(), Player::handle_vertical_input(), Kugelblitz::hit(), Jumpy::hit(), Fish::hit(), SpiderMite::initialize(), Kugelblitz::initialize(), Fish::jump(), Yeti::jump_down(), Totem::jump_off(), Yeti::jump_up(), KamikazeSnowball::kill_collision(), BadGuy::kill_fall(), BadGuy::kill_squished(), Zeekling::onBumpHorizontal(), Zeekling::onBumpVertical(), Yeti::run(), Toad::set_state(), SkullyHop::set_state(), MrIceBlock::set_state(), WalkingBadguy::set_velocity_y(), Stalactite::squish(), YetiStalactite::start_shaking(), Fish::start_waiting(), Totem::synchronize_with(), Yeti::take_hit(), SkyDive::ungrab(), and Player::update().

00049 {
00050   vy = nvy;
00051 }

void Physic::inverse_velocity_x (  ) 

Velocity inversion.

Definition at line 67 of file physic.cpp.

References vx.

00068 {
00069   vx = -vx;
00070 }

void Physic::inverse_velocity_y (  ) 

Definition at line 72 of file physic.cpp.

References vy.

00073 {
00074   vy = -vy;
00075 }

Vector Physic::get_velocity (  )  const

Definition at line 90 of file physic.cpp.

References vx, and vy.

Referenced by Player::add_velocity(), WalkingBadguy::add_velocity(), and Player::get_velocity().

00091 {
00092   return Vector(vx, vy);
00093 }

float Physic::get_velocity_x (  )  const

Definition at line 78 of file physic.cpp.

References vx.

Referenced by Zeekling::active_update(), WalkingBadguy::active_update(), Snail::active_update(), Player::add_velocity(), Player::apply_friction(), Plant::collision_badguy(), Star::collision_solid(), PowerUp::collision_solid(), GrowUp::collision_solid(), Bullet::collision_solid(), Toad::collision_solid(), Snail::collision_solid(), SkullyHop::collision_solid(), Plant::collision_solid(), MrIceBlock::collision_solid(), BouncingSnowball::collision_solid(), Player::draw(), ScriptedObject::get_velocity_x(), Player::handle_horizontal_input(), Player::handle_input(), Player::handle_vertical_input(), Totem::synchronize_with(), WalkingBadguy::turn_around(), and Player::update().

00079 {
00080   return vx;
00081 }

float Physic::get_velocity_y (  )  const

Definition at line 84 of file physic.cpp.

References vy.

Referenced by Toad::active_update(), FlyingSnowBall::active_update(), Fish::active_update(), Player::add_velocity(), Trampoline::collision(), Rock::collision(), PushButton::collision(), ScriptedObject::collision_solid(), Player::collision_solid(), GrowUp::collision_solid(), Bullet::collision_solid(), WalkingBadguy::collision_solid(), SkullyHop::collision_solid(), Fish::collision_tile(), Player::do_duck(), Jumpy::freeze(), Fish::freeze(), ScriptedObject::get_velocity_y(), WalkingBadguy::get_velocity_y(), Player::handle_horizontal_input(), Player::handle_vertical_input(), and Totem::synchronize_with().

00085 {
00086   return vy;
00087 }

void Physic::set_acceleration ( float  ax,
float  ay 
)

Sets acceleration applied to the object.

(Note that gravity is eventually added to the vertical acceleration)

Definition at line 108 of file physic.cpp.

References ax, and ay.

Referenced by Player::handle_horizontal_input(), Player::handle_input_climbing(), Player::handle_input_ghost(), Player::kill(), Player::start_climbing(), and Player::stop_climbing().

00109 {
00110   ax = nax;
00111   ay = nay;
00112 }

void Physic::set_acceleration_x ( float  ax  ) 

Definition at line 96 of file physic.cpp.

References ax.

Referenced by WalkingBadguy::active_update(), Player::apply_friction(), Haywire::collision_squished(), Player::deactivate(), WalkingBadguy::initialize(), and WalkingBadguy::turn_around().

00097 {
00098   ax = nax;
00099 }

void Physic::set_acceleration_y ( float  ay  ) 

Definition at line 102 of file physic.cpp.

References ay.

Referenced by FlyingSnowBall::active_update(), Snowman::collision_squished(), FlyingSnowBall::collision_squished(), Player::deactivate(), SkyDive::grab(), Player::handle_vertical_input(), BadGuy::kill_fall(), and SkyDive::ungrab().

00103 {
00104   ay = nay;
00105 }

Vector Physic::get_acceleration (  )  const

Definition at line 127 of file physic.cpp.

References ax, and ay.

00128 {
00129   return Vector(ax, ay);
00130 }

float Physic::get_acceleration_x (  )  const

Definition at line 115 of file physic.cpp.

References ax.

Referenced by Player::handle_horizontal_input(), and WalkingBadguy::turn_around().

00116 {
00117   return ax;
00118 }

float Physic::get_acceleration_y (  )  const

Definition at line 121 of file physic.cpp.

References ay.

Referenced by Player::handle_horizontal_input().

00122 {
00123   return ay;
00124 }

void Physic::enable_gravity ( bool  gravity_enabled  ) 

Enables or disables handling of gravity.

Definition at line 133 of file physic.cpp.

References gravity_enabled_flag.

Referenced by Stalactite::active_update(), Fish::active_update(), AngryStone::active_update(), AngryStone::AngryStone(), AngryStone::collision_solid(), Snowman::collision_squished(), Dart::Dart(), UnstableTile::fall_down(), Fish::Fish(), FlyingSnowBall::FlyingSnowBall(), SkyDive::grab(), GrowUp::GrowUp(), Owl::initialize(), KamikazeSnowball::initialize(), Fish::jump(), Player::kill(), KamikazeSnowball::kill_collision(), BadGuy::kill_fall(), BadGuy::kill_squished(), Kugelblitz::Kugelblitz(), Mole::Mole(), MoleRock::MoleRock(), PowerUp::PowerUp(), Root::Root(), Player::set_ghost_mode(), IceCrusher::set_state(), UnstableTile::slow_fall(), SpiderMite::SpiderMite(), Stalactite::squish(), Player::start_climbing(), Fish::start_waiting(), Player::stop_climbing(), Yeti::take_hit(), SkyDive::ungrab(), UnstableTile::UnstableTile(), SkullTile::update(), and Zeekling::Zeekling().

00134 {
00135   gravity_enabled_flag = enable_gravity;
00136 }

bool Physic::gravity_enabled (  )  const

Definition at line 139 of file physic.cpp.

References gravity_enabled_flag.

00140 {
00141   return gravity_enabled_flag;
00142 }

void Physic::set_gravity_modifier ( float  gravity  ) 

Set gravity modifier factor to apply to object when enabled.

Definition at line 145 of file physic.cpp.

Referenced by Player::do_jump_apex(), Player::early_jump_apex(), UnstableTile::fall_down(), Player::kill(), UnstableTile::slow_fall(), and UnstableTile::UnstableTile().

00146 {
00147   this->gravity_modifier = gravity_modifier;
00148 }

Vector Physic::get_movement ( float  elapsed_time  ) 

Definition at line 151 of file physic.cpp.

References ax, ay, Sector::current(), Sector::get_gravity(), gravity_enabled_flag, gravity_modifier, vx, and vy.

Referenced by Yeti::active_update(), Stalactite::active_update(), SpiderMite::active_update(), SkyDive::active_update(), FlyingSnowBall::active_update(), Bomb::active_update(), BadGuy::active_update(), UnstableTile::update(), Star::update(), SkullTile::update(), ScriptedObject::update(), Rock::update(), PowerUp::update(), Player::update(), OneUp::update(), IceCrusher::update(), GrowUp::update(), FallingCoin::update(), Bullet::update(), and BadGuy::update().

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 }


Member Data Documentation

float Physic::ax [private]

horizontal and vertical acceleration

Definition at line 75 of file physic.hpp.

Referenced by get_acceleration(), get_acceleration_x(), get_movement(), reset(), set_acceleration(), and set_acceleration_x().

float Physic::ay [private]

Definition at line 75 of file physic.hpp.

Referenced by get_acceleration(), get_acceleration_y(), get_movement(), reset(), set_acceleration(), and set_acceleration_y().

float Physic::vx [private]

horizontal and vertical velocity

Definition at line 78 of file physic.hpp.

Referenced by get_movement(), get_velocity(), get_velocity_x(), inverse_velocity_x(), reset(), set_velocity(), and set_velocity_x().

float Physic::vy [private]

Definition at line 78 of file physic.hpp.

Referenced by get_movement(), get_velocity(), get_velocity_y(), inverse_velocity_y(), reset(), set_velocity(), and set_velocity_y().

bool Physic::gravity_enabled_flag [private]

should we respect gravity in our calculations?

Definition at line 81 of file physic.hpp.

Referenced by enable_gravity(), get_movement(), gravity_enabled(), and reset().

float Physic::gravity_modifier [private]

gravity modifier is multiplied with the sectors gravity

Definition at line 84 of file physic.hpp.

Referenced by get_movement().


The documentation for this class was generated from the following files:
Generated on Mon Jun 9 03:38:33 2014 for SuperTux by  doxygen 1.5.1