cosmetix
[k8-jellyphysics.git] / src / jelly / World.h
blobe645900346b52830e64aeb8d2cc16034655f55f7
1 #ifndef _WORLD_H
2 #define _WORLD_H
4 #include "JellyPrerequisites.h"
5 #include "Body.h"
6 #include "Vector2.h"
9 namespace JellyPhysics {
11 class CollisionCallback {
12 public:
13 virtual bool collisionFilter (Body *bA, int bodyApm, Body *bodyB, int bodyBpm1, int bodyBpm2, Vector2 hitPt, float normalVel) { return true; }
17 class World {
18 public:
19 struct MaterialPair {
20 MaterialPair () : Collide(true), Elasticity(0.7f), Friction(0.3f), Callback(0) {}
22 bool Collide;
23 float Elasticity;
24 float Friction;
25 CollisionCallback *Callback;
28 public:
29 World ();
30 ~World ();
32 void killing ();
34 void setWorldLimits (const Vector2 &min, const Vector2 &max);
36 int addMaterial ();
38 void setMaterialPairCollide (int a, int b, bool collide);
39 void setMaterialPairData (int a, int b, float friction, float elasticity);
40 void setMaterialPairFilterCallback (int a, int b, CollisionCallback *c);
42 int bodyCount (void) const { return (int)mBodies.size(); }
43 void addBody (Body *b);
44 void removeBody (Body *b);
45 Body *getBody (int index);
47 void getClosestPointMass (const Vector2 &pt, int &bodyID, int &pmID);
48 Body *getBodyContaining (const Vector2 &pt);
50 void update (float elapsed);
52 int materialCount () const { return mMaterialCount; }
54 float getPenetrationThreshold () const { return mPenetrationThreshold; }
55 void setPenetrationThreshold (float val) { mPenetrationThreshold = val; }
57 int penetrationCount () const { return mPenetrationCount; }
59 private:
60 struct BodyCollisionInfo {
61 void Clear () { bodyA = bodyB = 0; bodyApm = bodyBpmA = bodyBpmB = -1; hitPt = norm = Vector2::Zero; edgeD = penetration = 0.0f; }
62 void Log () const {
63 //printf("BCI bodyA:%d bodyB:%d bApm:%d bBpmA:%d, bBpmB:%d\n", bodyA, bodyB, bodyApm, bodyBpmA, bodyBpmB);
64 //printf(" hitPt[%f][%f] edgeD:%f norm[%f][%f] penetration:%f\n",
65 // hitPt.X, hitPt.Y, edgeD, norm.X, norm.Y, penetration);
68 Body *bodyA;
69 Body *bodyB;
71 int bodyApm;
72 int bodyBpmA;
73 int bodyBpmB;
75 Vector2 hitPt;
76 float edgeD;
77 Vector2 norm;
78 float penetration;
81 private:
82 void updateBodyBitmask (Body *b);
83 void sortBodyBoundaries ();
85 void _goNarrowCheck (Body *bI, Body *bJ);
86 void bodyCollide (Body *bA, Body *bB, std::vector<BodyCollisionInfo> &infoList);
87 void _handleCollisions ();
89 void _checkAndMoveBoundary (Body::BodyBoundary *bb);
90 void _removeBoundary (Body::BodyBoundary *me);
91 void _addBoundaryAfter (Body::BodyBoundary *me, Body::BodyBoundary *toAfterMe);
92 void _addBoundaryBefore (Body::BodyBoundary *me, Body::BodyBoundary *toBeforeMe);
94 void _logMaterialCollide () const;
95 void _logBoundaries () const;
97 private:
98 typedef std::vector<Body *> BodyList;
100 BodyList mBodies;
101 AABB mWorldLimits;
102 Vector2 mWorldSize;
103 Vector2 mWorldGridStep;
105 float mPenetrationThreshold;
106 int mPenetrationCount;
108 MaterialPair *mMaterialPairs;
109 MaterialPair mDefaultMatPair;
110 int mMaterialCount;
112 std::vector<BodyCollisionInfo> mCollisionList;
118 #endif