Rube Goldberg Machine 1.0
This is the base code for Rube Goldberg designed for the CS296 Software Systems Lab
src/cs296_base.hpp
Go to the documentation of this file.
00001 /*
00002 * Copyright (c) 2006-2009 Erin Catto http://www.box2d.org
00003 *
00004 * This software is provided 'as-is', without any express or implied
00005 * warranty.  In no event will the authors be held liable for any damages
00006 * arising from the use of this software.
00007 * Permission is granted to anyone to use this software for any purpose,
00008 * including commercial applications, and to alter it and redistribute it
00009 * freely, subject to the following restrictions:
00010 * 1. The origin of this software must not be misrepresented; you must not
00011 * claim that you wrote the original software. If you use this software
00012 * in a product, an acknowledgment in the product documentation would be
00013 * appreciated but is not required.
00014 * 2. Altered source versions must be plainly marked as such, and must not be
00015 * misrepresented as being the original software.
00016 * 3. This notice may not be removed or altered from any source distribution.
00017 */
00018 
00019 /* 
00020  * Base code for CS 296 Software Systems Lab 
00021  * Department of Computer Science and Engineering, IIT Bombay
00022  * Instructor: Parag Chaudhuri
00023  */
00024 
00025 
00026 #ifndef _CS296BASE_HPP_
00027 #define _CS296BASE_HPP_
00028 
00029 #include "render.hpp"
00030 #include <Box2D/Box2D.h>
00031 #include <cstdlib>
00032 
00033 #define RAND_LIMIT 32767
00034 
00035 namespace cs296
00036 {
00037 
00039   class base_sim_t;
00040   struct settings_t;
00041   
00043   typedef base_sim_t* sim_create_fcn(); 
00044 
00046 
00048   struct settings_t
00049   {
00052     settings_t() :
00053       
00054       view_center(0.0f, 20.0f),
00056       hz(60.0f),
00059       velocity_iterations(8),
00062       position_iterations(3),
00063       draw_shapes(1),
00064       draw_joints(1),
00065       draw_AABBs(0),
00066       draw_pairs(0),
00067       draw_contact_points(0),
00068       draw_contact_normals(0),
00069       draw_contact_forces(0),
00070       draw_friction_forces(0),
00071       draw_COMs(0),
00072       draw_stats(0),
00073       draw_profile(0),
00074       enable_warm_starting(1),
00075       enable_continuous(1),
00076       enable_sub_stepping(0),
00077       pause(0),
00078       single_step(0)
00079     {}
00080     
00081     b2Vec2 view_center;
00083     float32 hz;
00086     int32 velocity_iterations;
00089     int32 position_iterations;
00090     int32 draw_shapes;
00091     int32 draw_joints;
00092     int32 draw_AABBs;
00093     int32 draw_pairs;
00094     int32 draw_contact_points;
00095     int32 draw_contact_normals;
00096     int32 draw_contact_forces;
00097     int32 draw_friction_forces;
00098     int32 draw_COMs;
00099     int32 draw_stats;
00100     int32 draw_profile;
00101     int32 enable_warm_starting;
00102     int32 enable_continuous;
00103     int32 enable_sub_stepping;
00104     int32 pause;
00105     int32 single_step;
00106   };
00107   
00109 
00111   struct sim_t
00112   {
00113     const char *name;
00114     sim_create_fcn *create_fcn;
00115 
00116     sim_t(const char *_name, sim_create_fcn *_create_fcn): 
00117       name(_name), create_fcn(_create_fcn) {;}
00118   };
00119   
00120   extern sim_t *sim;
00121   
00122   
00123   const int32 k_max_contact_points = 2048;  
00124   
00126 
00129   struct contact_point_t
00130   {
00131     b2Fixture* fixtureA;
00132     b2Fixture* fixtureB;
00133     b2Vec2 normal;
00134     b2Vec2 position;
00135     b2PointState state;
00136   };
00137   
00139 
00141   class base_sim_t : public b2ContactListener
00142   {
00143   public:
00144     
00145     base_sim_t();
00146 
00148     virtual ~base_sim_t();
00149     
00150     void set_text_line(int32 line) { m_text_line = line; }
00151     void draw_title(int x, int y, const char *string);
00152     
00153     virtual void step(settings_t* settings);
00154 
00155     virtual void keyboard(unsigned char key) { B2_NOT_USED(key); }
00156     virtual void keyboard_up(unsigned char key) { B2_NOT_USED(key); }
00157 
00158     void shift_mouse_down(const b2Vec2& p) { B2_NOT_USED(p); }
00159     virtual void mouse_down(const b2Vec2& p) { B2_NOT_USED(p); }
00160     virtual void mouse_up(const b2Vec2& p) { B2_NOT_USED(p); }
00161     void mouse_move(const b2Vec2& p) { B2_NOT_USED(p); }
00162 
00163     
00164     // Let derived tests know that a joint was destroyed.
00165     virtual void joint_destroyed(b2Joint* joint) { B2_NOT_USED(joint); }
00166     
00167     // Callbacks for derived classes.
00168     virtual void begin_contact(b2Contact* contact) { B2_NOT_USED(contact); }
00169     virtual void end_contact(b2Contact* contact) { B2_NOT_USED(contact); }
00170     virtual void pre_solve(b2Contact* contact, const b2Manifold* oldManifold);
00171     virtual void post_solve(const b2Contact* contact, const b2ContactImpulse* impulse)
00172     {
00173       B2_NOT_USED(contact);
00174       B2_NOT_USED(impulse);
00175     }
00176     
00178     inline b2World* get_world(void)
00179     {
00180       return m_world;
00181     }  
00182     
00183     
00184 
00188   protected:
00189 
00194     friend class contact_listener_t;
00195     
00196     b2Body* m_ground_body;
00197     b2AABB m_world_AABB;
00198     contact_point_t m_points[k_max_contact_points];
00199     int32 m_point_count;
00200 
00201     debug_draw_t m_debug_draw;
00202     int32 m_text_line;
00203     
00205 
00206     b2World* m_world;           
00207 
00208     int32 m_step_count;
00209     
00210     b2Profile m_max_profile;
00211     b2Profile m_total_profile;
00212   };
00213 }
00214 
00215 #endif
 All Classes Namespaces Files Functions Variables Typedefs Friends Defines