BicyclePlatform Class Reference

Used to construct a pair of bicycle platforms: If one is pushed down, the other one rises. More...

#include <bicycle_platform.hpp>

Inherits MovingSprite.

List of all members.

Public Member Functions

 BicyclePlatform (const Reader &reader)
 BicyclePlatform (BicyclePlatform *master)
virtual ~BicyclePlatform ()
virtual HitResponse collision (GameObject &other, const CollisionHit &hit)
 this function is called when the object collided with any other object
virtual void update (float elapsed_time)
 This function is called once per frame and allows the object to update it's state.

Protected Attributes

BicyclePlatform * master
 pointer to BicyclePlatform that does movement calculation
BicyclePlatform * slave
 pointer to BicyclePlatform that reacts to master platform's movement calculation
Vector center
 pivot point
float radius
 radius of circle
float angle
 current angle
float angular_speed
 angular speed in rad per second
std::set< GameObject * > contacts
 objects that are currently pushing on the platform
float momentum

Private Member Functions

 BicyclePlatform (const BicyclePlatform &)
 angular momentum in rad per second per second
BicyclePlatform & operator= (const BicyclePlatform &)


Detailed Description

Used to construct a pair of bicycle platforms: If one is pushed down, the other one rises.

Definition at line 25 of file bicycle_platform.hpp.


Constructor & Destructor Documentation

BicyclePlatform::BicyclePlatform ( const Reader &  reader  ) 

Definition at line 26 of file bicycle_platform.cpp.

References center, and MovingObject::get_pos().

Referenced by update().

00026                                                      :
00027   MovingSprite(reader, LAYER_OBJECTS, COLGROUP_STATIC), 
00028   master(0),
00029   slave(0), 
00030   center(),
00031   radius(128), 
00032   angle(0), 
00033   angular_speed(0), 
00034   contacts(),
00035   momentum(0)
00036 {
00037   center = get_pos();
00038 }

BicyclePlatform::BicyclePlatform ( BicyclePlatform *  master  ) 

Definition at line 40 of file bicycle_platform.cpp.

References MovingObject::get_bbox(), MovingObject::get_pos(), Rectf::get_width(), master, MovingObject::set_pos(), and slave.

00040                                                         :
00041   MovingSprite(*master), 
00042   master(master), 
00043   slave(this), 
00044   center(master->center), 
00045   radius(master->radius), 
00046   angle(master->angle + M_PI), 
00047   angular_speed(0), 
00048   contacts(),
00049   momentum(0)
00050 {
00051   set_pos(get_pos() + Vector(master->get_bbox().get_width(), 0));
00052   master->master = master;
00053   master->slave = this;
00054 }

BicyclePlatform::~BicyclePlatform (  )  [virtual]

Definition at line 56 of file bicycle_platform.cpp.

References master, and slave.

00057 {
00058   if ((this == master) && (master)) {
00059     slave->master = 0;
00060     slave->slave = 0;
00061   }
00062   if ((master) && (this == slave)) {
00063     master->master = 0;
00064     master->slave = 0;
00065   }
00066   master = 0;
00067   slave = 0;
00068 }

BicyclePlatform::BicyclePlatform ( const BicyclePlatform &   )  [private]

angular momentum in rad per second per second


Member Function Documentation

HitResponse BicyclePlatform::collision ( GameObject &  other,
const CollisionHit &  hit 
) [virtual]

this function is called when the object collided with any other object

Implements MovingObject.

Definition at line 71 of file bicycle_platform.cpp.

References contacts, FORCE_MOVE, MovingObject::get_bbox(), Player::get_grabbed_object(), Player::is_big(), momentum, Rectf::p2, and Vector::y.

00072 {
00073 
00074   // somehow the hit parameter does not get filled in, so to determine (hit.top == true) we do this:
00075   MovingObject* mo = dynamic_cast<MovingObject*>(&other);
00076   if (!mo) return FORCE_MOVE;
00077   if ((mo->get_bbox().p2.y) > (get_bbox().p1.y + 2)) return FORCE_MOVE;
00078 
00079   Player* pl = dynamic_cast<Player*>(mo);
00080   if (pl) {
00081     if (pl->is_big()) momentum += 1;
00082     Portable* po = pl->get_grabbed_object();
00083     MovingObject* pomo = dynamic_cast<MovingObject*>(po);
00084     if (contacts.insert(pomo).second) momentum += 1;
00085   }
00086 
00087   if (contacts.insert(&other).second) momentum += 1;
00088   return FORCE_MOVE;
00089 }

void BicyclePlatform::update ( float  elapsed_time  )  [virtual]

This function is called once per frame and allows the object to update it's state.

The elapsed_time is the time since the last frame in seconds and should be the base for all timed calculations (don't use SDL_GetTicks directly as this will fail in pause mode)

Reimplemented from MovingSprite.

Definition at line 92 of file bicycle_platform.cpp.

References Sector::add_object(), angle, angular_speed, MovingObject::bbox, BicyclePlatform(), center, contacts, Sector::current(), MovingObject::dest, MovingObject::get_pos(), Rectf::get_size(), master, momentum, MovingObject::movement, radius, and slave.

00093 {
00094   if (!slave) {
00095     Sector::current()->add_object(new BicyclePlatform(this));
00096     return;
00097   }
00098   if (!master) {
00099     return;
00100   }
00101   if (this == slave) {
00102     angle = master->angle + M_PI;
00103     while (angle < 0) { angle += 2*M_PI; } 
00104     while (angle > 2*M_PI) { angle -= 2*M_PI; } 
00105     Vector dest = center + Vector(cosf(angle), sinf(angle)) * radius - (bbox.get_size() * 0.5);
00106     movement = dest - get_pos();
00107   }
00108   if (this == master) {
00109     float momentum_diff = momentum - slave->momentum;
00110     contacts.clear(); momentum = 0;
00111     slave->contacts.clear(); slave->momentum = 0;
00112 
00113     float angular_momentum = cosf(angle) * momentum_diff;
00114 
00115     angular_speed += (angular_momentum * elapsed_time) * M_PI;
00116     angular_speed *= 1 - elapsed_time * 0.2;
00117     angle += angular_speed * elapsed_time;
00118     while (angle < 0) { angle += 2*M_PI; } 
00119     while (angle > 2*M_PI) { angle -= 2*M_PI; } 
00120     angular_speed = std::min(std::max(angular_speed, static_cast<float>(-128*M_PI*elapsed_time)), static_cast<float>(128*M_PI*elapsed_time));
00121     Vector dest = center + Vector(cosf(angle), sinf(angle)) * radius - (bbox.get_size() * 0.5);
00122     movement = dest - get_pos();
00123 
00124     center += Vector(angular_speed, 0) * elapsed_time * 32;
00125     slave->center += Vector(angular_speed, 0) * elapsed_time * 32;
00126 
00127   }
00128 }

BicyclePlatform& BicyclePlatform::operator= ( const BicyclePlatform &   )  [private]


Member Data Documentation

BicyclePlatform* BicyclePlatform::master [protected]

pointer to BicyclePlatform that does movement calculation

Definition at line 36 of file bicycle_platform.hpp.

Referenced by BicyclePlatform(), update(), and ~BicyclePlatform().

BicyclePlatform* BicyclePlatform::slave [protected]

pointer to BicyclePlatform that reacts to master platform's movement calculation

Definition at line 37 of file bicycle_platform.hpp.

Referenced by BicyclePlatform(), update(), and ~BicyclePlatform().

Vector BicyclePlatform::center [protected]

pivot point

Definition at line 38 of file bicycle_platform.hpp.

Referenced by BicyclePlatform(), and update().

float BicyclePlatform::radius [protected]

radius of circle

Definition at line 39 of file bicycle_platform.hpp.

Referenced by update().

float BicyclePlatform::angle [protected]

current angle

Definition at line 40 of file bicycle_platform.hpp.

Referenced by update().

float BicyclePlatform::angular_speed [protected]

angular speed in rad per second

Definition at line 41 of file bicycle_platform.hpp.

Referenced by update().

std::set<GameObject*> BicyclePlatform::contacts [protected]

objects that are currently pushing on the platform

Definition at line 42 of file bicycle_platform.hpp.

Referenced by collision(), and update().

float BicyclePlatform::momentum [protected]

Definition at line 43 of file bicycle_platform.hpp.

Referenced by collision(), and update().


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