4 #include "JellyPrerequisites.h"
9 namespace JellyPhysics
{
11 class CollisionCallback
{
13 virtual bool collisionFilter (Body
*bA
, int bodyApm
, Body
*bodyB
, int bodyBpm1
, int bodyBpm2
, Vector2 hitPt
, float normalVel
) { return true; }
20 MaterialPair () : Collide(true), Elasticity(0.7f
), Friction(0.3f
), Callback(0) {}
25 CollisionCallback
*Callback
;
34 void setWorldLimits (const Vector2
&min
, const Vector2
&max
);
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
; }
60 struct BodyCollisionInfo
{
61 void Clear () { bodyA
= bodyB
= 0; bodyApm
= bodyBpmA
= bodyBpmB
= -1; hitPt
= norm
= Vector2::Zero
; edgeD
= penetration
= 0.0f
; }
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);
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;
98 typedef std::vector
<Body
*> BodyList
;
103 Vector2 mWorldGridStep
;
105 float mPenetrationThreshold
;
106 int mPenetrationCount
;
108 MaterialPair
*mMaterialPairs
;
109 MaterialPair mDefaultMatPair
;
112 std::vector
<BodyCollisionInfo
> mCollisionList
;