AppEasy Core SDK  1.5.0
Cross platform mobile and desktop app and game development SDK - The easy way to make apps
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Defines
CzBox2d.h
Go to the documentation of this file.
00001 // 
00002 //
00003 // AppEasy SDK - Cross Platform Multi-purpose Game and App Engine
00004 //
00005 // Developed by Matthew Hopwood of Pocketeers Limited - www.pocketeers.co.uk
00006 //
00007 // For updates, tutorials and more details check out www.appeasymobile.com
00008 //
00009 // This code is provided free of charge and without any warranty whatsoever. You must ensure that this whole notice is present in all files and derivatives, so the reader knows its origin.
00010 // If you use this SDK in your product then please ensure that you credit AppEasy's usage appropriately. Please see www.appeasymobile.com for licensing details and support
00011 //
00012 //
00013 
00014 #if !defined(_CZ_BOX2D_H_)
00015 #define _CZ_BOX2D_H_
00016 
00017 #include "CzUtil.h"
00018 #include "CzString.h"
00019 #include "CzXoml.h"
00020 #include "CzShapes.h"
00021 #include "CzSlotArray.h"
00022 #include "CzBox2dMaterial.h"
00023 #include "CzBox2dJoint.h"
00024 #include "CzBox2dFixture.h"
00025 
00026 #include "Box2D/Box2D.h"
00027 
00028 
00029 //
00030 //
00031 //
00032 //
00033 // CzBox2dCcollisionCallback - Box2D scene collision callback
00034 //
00035 //
00036 //
00037 //
00038 class CzBox2dCcollisionCallback : public b2ContactListener
00039 {
00040 public:
00041     void                BeginContact(b2Contact* contact);       // Called when one body begins to collide with another
00042     void                EndContact(b2Contact* contact);         // Called when one body drops colliding with another
00043 };
00044 
00045 // Glopbally defined collision collback used to marshal cross actor collision calls
00046 extern CzBox2dCcollisionCallback    g_CzBox2dCcollisionCallback;
00047 
00048 
00049 //
00050 //
00051 //
00052 //
00053 // CzBox2dCollidable - Represents a collection of begin and end contact collisions. Objects that are part of the physics system usually derive from this class
00054 //
00055 //
00056 //
00057 //
00058 //#define CZ_BOX2D_KEEP_CONTACTS
00059 class CzBox2dCollidable
00060 {
00061 public:
00062     // Properties
00063 protected:
00064     void*           UserData;                                   // Contains user data (usually container actor)
00065     CzSlotArray<CzBox2dCollidable*> CollisionsStarted;          // List of collidables that have just come into contact with this collidable
00066     CzSlotArray<CzBox2dCollidable*> CollisionsEnded;            // List of collidables that have just lost contact with this collidable
00067 #if defined(CZ_BOX2D_KEEP_CONTACTS)
00068     CzSlotArray<CzBox2dCollidable*> CollisionContacts;          // List of collidables that this one is in collision with
00069 #endif // CZ_BOX2D_KEEP_CONTACTS
00070 public:
00071     virtual void    addCollisionStarted(CzBox2dCollidable* with)
00072     {
00073         CollisionsStarted.add(with);
00074 #if defined(CZ_BOX2D_KEEP_CONTACTS)
00075         CollisionContacts.add(with);
00076 #endif // CZ_BOX2D_KEEP_CONTACTS
00077     }
00078     void            clearCollisionsStarted()            { CollisionsStarted.clear(); }
00079     int             getCollisionsStartedCount()         { return CollisionsStarted.count(); }
00080     virtual void    addCollisionEnded(CzBox2dCollidable* with)
00081     {
00082         CollisionsEnded.add(with);
00083 #if defined(CZ_BOX2D_KEEP_CONTACTS)
00084         CollisionContacts.remove(with);
00085 #endif // CZ_BOX2D_KEEP_CONTACTS
00086     }
00087     void            clearCollisionsEnded()              { CollisionsEnded.clear(); }
00088     int             getCollisionsEndedCount()           { return CollisionsEnded.count(); }
00089 #if defined(CZ_BOX2D_KEEP_CONTACTS)
00090     void            clearCollisionContacts()            { CollisionContacts.clear(); }
00091     int             getCollisionContactsCount()         { return CollisionContacts.count(); }
00092 #endif // CZ_BOX2D_KEEP_CONTACTS
00093 
00094     void            clearAllCollisions()
00095     {
00096         CollisionsStarted.clear();
00097         CollisionsEnded.clear();
00098 #if defined(CZ_BOX2D_KEEP_CONTACTS)
00099         clearCollisionContacts();
00100 #endif // CZ_BOX2D_KEEP_CONTACTS
00101     }
00102     CzSlotArray<CzBox2dCollidable*>& getCollisionsStarted()     { return CollisionsStarted; }
00103     CzSlotArray<CzBox2dCollidable*>& getCollisionsEnded()       { return CollisionsEnded; }
00104 #if defined(CZ_BOX2D_KEEP_CONTACTS)
00105     CzSlotArray<CzBox2dCollidable*>& getCollisionContacts()     { return CollisionContacts; }
00106 #endif // CZ_BOX2D_KEEP_CONTACTS
00107     void                setUserData(void* user_data)    { UserData = user_data; }
00108     void*               getUserData()                   { return UserData; }
00109     // End of properties
00110 
00111     CzBox2dCollidable() : UserData(NULL) {}
00112 
00113     virtual ~CzBox2dCollidable()
00114     {
00115         clearAllCollisions();
00116     }
00117 };
00118 
00119 
00120 //
00121 //
00122 //
00123 //
00124 // CzBox2dWorld - Box2D physics world
00125 //
00126 //
00127 //
00128 //
00129 class CzBox2dWorld
00130 {
00131 public:
00132 
00133 protected:
00134     // Properties
00135     b2World*                World;                  // Physical world
00136     CzVec2                  WorldScale;             // Scaling between physical and viusal worlds (set to 0, 0 to disable physics update)
00137     int                     VelocityIterations;     // Number of internal iterations used when computing velocities
00138     int                     PositionIterations;     // Number of internal iterations used when computing positions
00139     CzVec2                  Gravity;                // Scene gravity
00140     float                   TimeStep;               // Physics time step in seconds
00141 public:
00142     void                    setWorldScale(float x, float y)         { WorldScale.x = x; WorldScale.y = y; }
00143     CzVec2                  getWorldScale()                         { return WorldScale; }
00144     b2World*                getWorld()                              { return World; }
00145     void                    setVelocityIterations(int count)        { VelocityIterations = count; }
00146     int                     getVelocityIterations() const           { return VelocityIterations; }
00147     void                    setPositionIterations(int count)        { PositionIterations = count; }
00148     int                     getPositionIterations() const           { return PositionIterations; }
00149     void                    setGravity(float x, float y);
00150     CzVec2                  getGravity() const                      { return Gravity; }
00151     // Properties end
00152 
00153 public:
00154     CzBox2dWorld() : World(NULL), WorldScale(10.0f, 10.0f), VelocityIterations(6), PositionIterations(3), Gravity(0.0f, 15.0f) {}
00155     virtual ~CzBox2dWorld();
00156 
00157     // World initialisation
00158     virtual void            InitWorld(bool doSleep = true);
00159 
00160     // World update
00161     virtual void            UpdateWorld(float dt);
00162 
00163     // Box2D world to pixel conversions
00164     float           WorldToPixelX(float x)                      { return x * WorldScale.x; }
00165     float           WorldToPixelY(float y)                      { return y * WorldScale.y; }
00166     float           PixelToWorldX(float x)                      { return x / WorldScale.x; }
00167     float           PixelToWorldY(float y)                      { return y / WorldScale.y; }
00168 
00169 };
00170 
00171 
00172 //
00173 //
00174 //
00175 //
00176 // CzBox2dBody - Box2D physics body
00177 //
00178 //
00179 //
00180 //
00181  class CzActor;
00182 class CzBox2dBody :  public CzBox2dCollidable
00183 {
00184 public:
00185 protected:
00186     // Properties
00187     CzBox2dWorld*           World;                  // World that this body is attached to
00188     b2Body*                 Body;                   // Physical body
00189     CzVector<b2Fixture*>    Fixtures;               // The physical fixtures (managed by Box2D)
00190     CzBox2dMaterial*        BodyMaterial;           // Physical body attributes that are applied to this actors body
00191     CzVector<CzShape*>      BodyShapes;             // Physical body shapes
00192     CzVector<IzBox2dJoint*> Joints;                 // Physical joints
00193     int                     CollisionFlags;         // Collision flags
00194 public:
00195     void                setWorld(CzBox2dWorld* world)           { World = world; }
00196     CzBox2dWorld*       getWorld()                              { return World; }
00197     b2Body*             getBody()                               { return Body; }
00198     b2Fixture*          addFixture(CzBox2dMaterial* body_mat, float width, float height, float com_x, float com_y);
00199     b2Fixture*          addFixture(CzShape* body_shape, CzBox2dMaterial* body_mat, float com_x, float com_y);
00200     int                 getFixturesCount()                      { return Fixtures.size(); }
00201     b2Fixture*          getFixture(int index)                   { return Fixtures[index]; }
00202     void                DestroyFixtures();
00203     void                setBodyMaterial(CzBox2dMaterial* mat);  
00204     CzBox2dMaterial*    getBodyMaterial()                       { return BodyMaterial; }
00205     CzVector<IzBox2dJoint*>& getJointsList()                    { return Joints; }
00206     void                addJoint(IzBox2dJoint* joint)           { Joints.push_back(joint); }
00207     IzBox2dJoint*       getJoint(int index);
00208     IzBox2dJoint*       findJoint(unsigned int name_hash);
00209     bool                removeJoint(IzBox2dJoint* joint);
00210     void                setAsSensor(bool enable);
00211     bool                isSensor() const;
00212     void                setCollisionFlags(int category_flags, int mask_flags, int collision_group);
00213     int                 getCollisionFlags() const               { return CollisionFlags; }
00214     int                 getCollisionCategory() const;
00215     int                 getCollisionMask() const;
00216     int                 getCollisionGroup() const;
00217     void                SetAwake(bool awake)                    { Body->SetAwake(awake); }
00218     bool                IsAwake() const                         { return Body->IsAwake(); }
00219     void                SetActive(bool active)                  { Body->SetActive(active); }
00220     bool                IsActive() const                        { return Body->IsActive(); }
00221     // Properties end
00222 
00223 public:
00224     CzBox2dBody() : CzBox2dCollidable(), World(NULL), Body(NULL), BodyMaterial(NULL), CollisionFlags(0)         {}
00225     virtual ~CzBox2dBody();
00226 
00227     // Initialise the physical body
00228     void                InitBody(CzBox2dWorld* world, CzShape* body_shape, CzBox2dMaterial* body_mat, CzVec2* pos, float angle, float com_x, float com_y);
00229 
00230     void                ReleaseBody();
00231 
00232     // Force application
00233     void                ApplyForce(float force_x, float force_y, float world_pos_x, float world_pos_y);
00234     void                ApplyForceToCenter(float force_x, float force_y);
00235     void                ApplyTorque(float torque);
00236     void                ApplyLinearImpulse(float impulse_x, float impulse_y, float world_pos_x, float world_pos_y);
00237     void                ApplyAngularImpulse(float impulse);
00238 };
00239 
00240 
00241 #endif // _CZ_BOX2D_H_