Rube Goldberg Machine 1.0
This is the base code for Rube Goldberg designed for the CS296 Software Systems Lab
Public Member Functions | Protected Attributes | Friends
cs296::base_sim_t Class Reference

the base_sim_t class is used to simulate the Box2D world. inherited from the b2ContactListener. More...

#include <cs296_base.hpp>

Inheritance diagram for cs296::base_sim_t:
Inheritance graph
[legend]
Collaboration diagram for cs296::base_sim_t:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 base_sim_t ()
virtual ~base_sim_t ()
 Virtual destructors - amazing objects. First the base_sim_t object will be destroyed and then its base class b2ContactListener.
void set_text_line (int32 line)
void draw_title (int x, int y, const char *string)
virtual void step (settings_t *settings)
virtual void keyboard (unsigned char key)
virtual void keyboard_up (unsigned char key)
void shift_mouse_down (const b2Vec2 &p)
virtual void mouse_down (const b2Vec2 &p)
virtual void mouse_up (const b2Vec2 &p)
void mouse_move (const b2Vec2 &p)
virtual void joint_destroyed (b2Joint *joint)
virtual void begin_contact (b2Contact *contact)
virtual void end_contact (b2Contact *contact)
virtual void pre_solve (b2Contact *contact, const b2Manifold *oldManifold)
virtual void post_solve (const b2Contact *contact, const b2ContactImpulse *impulse)
b2World * get_world (void)
 [Lab04] Returns Box2D world Object since its a protected member.

Protected Attributes

b2Body * m_ground_body
b2AABB m_world_AABB
contact_point_t m_points [k_max_contact_points]
int32 m_point_count
debug_draw_t m_debug_draw
int32 m_text_line
b2World * m_world
 Box2D World Object.
int32 m_step_count
b2Profile m_max_profile
b2Profile m_total_profile

Friends

class contact_listener_t

Detailed Description

the base_sim_t class is used to simulate the Box2D world. inherited from the b2ContactListener.

The base_sim_t class simulates the Box2D world. The Constraint solver is used to track the position and velocity of each object. Also, it detects collision of the bodies. b2ContactListener is used to receive the contact data.

Definition at line 141 of file cs296_base.hpp.


Constructor & Destructor Documentation

base_sim_t::base_sim_t ( )

Definition at line 25 of file cs296_base.cpp.

{
    b2Vec2 gravity;
    gravity.Set(0.0f, -10.0f);
    m_world = new b2World(gravity);

    m_text_line = 30;

    m_point_count = 0;
    
    m_world->SetDebugDraw(&m_debug_draw);
    
    m_step_count = 0;

    b2BodyDef body_def;
    m_ground_body = m_world->CreateBody(&body_def);

    memset(&m_max_profile, 0, sizeof(b2Profile));
    memset(&m_total_profile, 0, sizeof(b2Profile));
}
base_sim_t::~base_sim_t ( ) [virtual]

Virtual destructors - amazing objects. First the base_sim_t object will be destroyed and then its base class b2ContactListener.

Definition at line 46 of file cs296_base.cpp.

{
    // By deleting the world, we delete the bomb, mouse joint, etc.
    delete m_world;
    m_world = NULL;
}

Member Function Documentation

virtual void cs296::base_sim_t::begin_contact ( b2Contact *  contact) [inline, virtual]

Definition at line 168 of file cs296_base.hpp.

{ B2_NOT_USED(contact); }
void base_sim_t::draw_title ( int  x,
int  y,
const char *  string 
)

Definition at line 83 of file cs296_base.cpp.

{
    m_debug_draw.DrawString(x, y, string);
}
virtual void cs296::base_sim_t::end_contact ( b2Contact *  contact) [inline, virtual]

Definition at line 169 of file cs296_base.hpp.

{ B2_NOT_USED(contact); }
b2World* cs296::base_sim_t::get_world ( void  ) [inline]

[Lab04] Returns Box2D world Object since its a protected member.

Definition at line 178 of file cs296_base.hpp.

    {
      return m_world;
    }  
virtual void cs296::base_sim_t::joint_destroyed ( b2Joint *  joint) [inline, virtual]

Definition at line 165 of file cs296_base.hpp.

{ B2_NOT_USED(joint); }
virtual void cs296::base_sim_t::keyboard ( unsigned char  key) [inline, virtual]

Definition at line 155 of file cs296_base.hpp.

{ B2_NOT_USED(key); }
virtual void cs296::base_sim_t::keyboard_up ( unsigned char  key) [inline, virtual]

Definition at line 156 of file cs296_base.hpp.

{ B2_NOT_USED(key); }
virtual void cs296::base_sim_t::mouse_down ( const b2Vec2 &  p) [inline, virtual]

Definition at line 159 of file cs296_base.hpp.

{ B2_NOT_USED(p); }
void cs296::base_sim_t::mouse_move ( const b2Vec2 &  p) [inline]

Definition at line 161 of file cs296_base.hpp.

{ B2_NOT_USED(p); }
virtual void cs296::base_sim_t::mouse_up ( const b2Vec2 &  p) [inline, virtual]

Definition at line 160 of file cs296_base.hpp.

{ B2_NOT_USED(p); }
virtual void cs296::base_sim_t::post_solve ( const b2Contact *  contact,
const b2ContactImpulse *  impulse 
) [inline, virtual]

Definition at line 171 of file cs296_base.hpp.

    {
      B2_NOT_USED(contact);
      B2_NOT_USED(impulse);
    }
void base_sim_t::pre_solve ( b2Contact *  contact,
const b2Manifold *  oldManifold 
) [virtual]

Definition at line 53 of file cs296_base.cpp.

{
  const b2Manifold* manifold = contact->GetManifold();
  
  if (manifold->pointCount == 0)
    {
      return;
    }
  
  b2Fixture* fixtureA = contact->GetFixtureA();
  b2Fixture* fixtureB = contact->GetFixtureB();
  
  b2PointState state1[b2_maxManifoldPoints], state2[b2_maxManifoldPoints];
  b2GetPointStates(state1, state2, oldManifold, manifold);
  
  b2WorldManifold world_manifold;
  contact->GetWorldManifold(&world_manifold);
  
  for (int32 i = 0; i < manifold->pointCount && m_point_count < k_max_contact_points; ++i)
    {
      contact_point_t* cp = m_points + m_point_count;
      cp->fixtureA = fixtureA;
      cp->fixtureB = fixtureB;
      cp->position = world_manifold.points[i];
      cp->normal = world_manifold.normal;
      cp->state = state2[i];
      ++m_point_count;
    }
}
void cs296::base_sim_t::set_text_line ( int32  line) [inline]

Definition at line 150 of file cs296_base.hpp.

{ m_text_line = line; }
void cs296::base_sim_t::shift_mouse_down ( const b2Vec2 &  p) [inline]

Definition at line 158 of file cs296_base.hpp.

{ B2_NOT_USED(p); }
void base_sim_t::step ( settings_t settings) [virtual]

Definition at line 88 of file cs296_base.cpp.

{
  float32 time_step = settings->hz > 0.0f ? 1.0f / settings->hz : float32(0.0f);

  if (settings->pause)
    {
      if (settings->single_step)
    {
      settings->single_step = 0;
    }
      else
    {
      time_step = 0.0f;
    }
      
      m_debug_draw.DrawString(5, m_text_line, "****PAUSED****");
      m_text_line += 15;
    }
  
  uint32 flags = 0;
  flags += settings->draw_shapes            * b2Draw::e_shapeBit;
  flags += settings->draw_joints            * b2Draw::e_jointBit;
  flags += settings->draw_AABBs         * b2Draw::e_aabbBit;
  flags += settings->draw_pairs         * b2Draw::e_pairBit;
  flags += settings->draw_COMs              * b2Draw::e_centerOfMassBit;
  m_debug_draw.SetFlags(flags);
  
  m_world->SetWarmStarting(settings->enable_warm_starting > 0);
  m_world->SetContinuousPhysics(settings->enable_continuous > 0);
  m_world->SetSubStepping(settings->enable_sub_stepping > 0);
  
  m_point_count = 0;
  
  m_world->Step(time_step, settings->velocity_iterations, settings->position_iterations);
  
  m_world->DrawDebugData();
  
  if (time_step > 0.0f)
    {
      ++m_step_count;
    }
  
  if (settings->draw_stats)
    {
      int32 body_count = m_world->GetBodyCount();
      int32 contact_count = m_world->GetContactCount();
      int32 joint_count = m_world->GetJointCount();
      m_debug_draw.DrawString(5, m_text_line, "bodies/contacts/joints = %d/%d/%d", body_count, contact_count, joint_count);
      m_text_line += 15;
      
      int32 proxy_count = m_world->GetProxyCount();
      int32 height = m_world->GetTreeHeight();
      int32 balance = m_world->GetTreeBalance();
      float32 quality = m_world->GetTreeQuality();
      m_debug_draw.DrawString(5, m_text_line, "proxies/height/balance/quality = %d/%d/%d/%g", proxy_count, height, balance, quality);
      m_text_line += 15;
    }
  
  // Track maximum profile times
  {
    const b2Profile& p = m_world->GetProfile();
    m_max_profile.step = b2Max(m_max_profile.step, p.step);
    m_max_profile.collide = b2Max(m_max_profile.collide, p.collide);
    m_max_profile.solve = b2Max(m_max_profile.solve, p.solve);
    m_max_profile.solveInit = b2Max(m_max_profile.solveInit, p.solveInit);
    m_max_profile.solveVelocity = b2Max(m_max_profile.solveVelocity, p.solveVelocity);
    m_max_profile.solvePosition = b2Max(m_max_profile.solvePosition, p.solvePosition);
    m_max_profile.solveTOI = b2Max(m_max_profile.solveTOI, p.solveTOI);
    m_max_profile.broadphase = b2Max(m_max_profile.broadphase, p.broadphase);
    
    m_total_profile.step += p.step;
    m_total_profile.collide += p.collide;
    m_total_profile.solve += p.solve;
    m_total_profile.solveInit += p.solveInit;
    m_total_profile.solveVelocity += p.solveVelocity;
    m_total_profile.solvePosition += p.solvePosition;
    m_total_profile.solveTOI += p.solveTOI;
    m_total_profile.broadphase += p.broadphase;
  }
  
  if (settings->draw_profile)
    {
      const b2Profile& p = m_world->GetProfile();
      
      b2Profile ave_profile;
      memset(&ave_profile, 0, sizeof(b2Profile));
      if (m_step_count > 0)
    {
      float32 scale = 1.0f / m_step_count;
      ave_profile.step = scale * m_total_profile.step;
      ave_profile.collide = scale * m_total_profile.collide;
      ave_profile.solve = scale * m_total_profile.solve;
      ave_profile.solveInit = scale * m_total_profile.solveInit;
      ave_profile.solveVelocity = scale * m_total_profile.solveVelocity;
      ave_profile.solvePosition = scale * m_total_profile.solvePosition;
      ave_profile.solveTOI = scale * m_total_profile.solveTOI;
      ave_profile.broadphase = scale * m_total_profile.broadphase;
    }
      
      m_debug_draw.DrawString(5, m_text_line, "step [ave] (max) = %5.2f [%6.2f] (%6.2f)", p.step, ave_profile.step, m_max_profile.step);
      m_text_line += 15;
      m_debug_draw.DrawString(5, m_text_line, "collide [ave] (max) = %5.2f [%6.2f] (%6.2f)", p.collide, ave_profile.collide, m_max_profile.collide);
      m_text_line += 15;
      m_debug_draw.DrawString(5, m_text_line, "solve [ave] (max) = %5.2f [%6.2f] (%6.2f)", p.solve, ave_profile.solve, m_max_profile.solve);
      m_text_line += 15;
      m_debug_draw.DrawString(5, m_text_line, "solve init [ave] (max) = %5.2f [%6.2f] (%6.2f)", p.solveInit, ave_profile.solveInit, m_max_profile.solveInit);
      m_text_line += 15;
      m_debug_draw.DrawString(5, m_text_line, "solve velocity [ave] (max) = %5.2f [%6.2f] (%6.2f)", p.solveVelocity, ave_profile.solveVelocity, m_max_profile.solveVelocity);
      m_text_line += 15;
      m_debug_draw.DrawString(5, m_text_line, "solve position [ave] (max) = %5.2f [%6.2f] (%6.2f)", p.solvePosition, ave_profile.solvePosition, m_max_profile.solvePosition);
      m_text_line += 15;
      m_debug_draw.DrawString(5, m_text_line, "solveTOI [ave] (max) = %5.2f [%6.2f] (%6.2f)", p.solveTOI, ave_profile.solveTOI, m_max_profile.solveTOI);
      m_text_line += 15;
      m_debug_draw.DrawString(5, m_text_line, "broad-phase [ave] (max) = %5.2f [%6.2f] (%6.2f)", p.broadphase, ave_profile.broadphase, m_max_profile.broadphase);
      m_text_line += 15;
    }
    
  if (settings->draw_contact_points)
    {
      //const float32 k_impulseScale = 0.1f;
      const float32 k_axis_scale = 0.3f;
      
      for (int32 i = 0; i < m_point_count; ++i)
    {
      contact_point_t* point = m_points + i;
      
      if (point->state == b2_addState)
        {
          // Add
          m_debug_draw.DrawPoint(point->position, 10.0f, b2Color(0.3f, 0.95f, 0.3f));
        }
      else if (point->state == b2_persistState)
        {
          // Persist
          m_debug_draw.DrawPoint(point->position, 5.0f, b2Color(0.3f, 0.3f, 0.95f));
        }
      
      if (settings->draw_contact_normals == 1)
        {
          b2Vec2 p1 = point->position;
          b2Vec2 p2 = p1 + k_axis_scale * point->normal;
          m_debug_draw.DrawSegment(p1, p2, b2Color(0.9f, 0.9f, 0.9f));
        }
      else if (settings->draw_contact_forces == 1)
        {
          //b2Vec2 p1 = point->position;
          //b2Vec2 p2 = p1 + k_forceScale * point->normalForce * point->normal;
          //DrawSegment(p1, p2, b2Color(0.9f, 0.9f, 0.3f));
        }
      
      if (settings->draw_friction_forces == 1)
        {
          //b2Vec2 tangent = b2Cross(point->normal, 1.0f);
          //b2Vec2 p1 = point->position;
          //b2Vec2 p2 = p1 + k_forceScale * point->tangentForce * tangent;
          //DrawSegment(p1, p2, b2Color(0.9f, 0.9f, 0.3f));
        }
    }
    }
}

Friends And Related Function Documentation

friend class contact_listener_t [friend]

How are protected members different from private memebers of a class in C++ ? Private members can't be inherited while protected members can be inherited.

What are Friend classes? Friend Classes can access the class' private and protected members. This is for some objects to make things

Definition at line 194 of file cs296_base.hpp.


Member Data Documentation

Definition at line 201 of file cs296_base.hpp.

b2Body* cs296::base_sim_t::m_ground_body [protected]

Definition at line 196 of file cs296_base.hpp.

b2Profile cs296::base_sim_t::m_max_profile [protected]

Definition at line 210 of file cs296_base.hpp.

Definition at line 199 of file cs296_base.hpp.

Definition at line 198 of file cs296_base.hpp.

Definition at line 208 of file cs296_base.hpp.

int32 cs296::base_sim_t::m_text_line [protected]

Definition at line 202 of file cs296_base.hpp.

b2Profile cs296::base_sim_t::m_total_profile [protected]

Definition at line 211 of file cs296_base.hpp.

b2World* cs296::base_sim_t::m_world [protected]

Box2D World Object.

The Box2D Simulation is run in the b2World object. It is initialized in the constructor

Definition at line 206 of file cs296_base.hpp.

b2AABB cs296::base_sim_t::m_world_AABB [protected]

Definition at line 197 of file cs296_base.hpp.


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Friends Defines