chipmunk_structs.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /* Copyright (c) 2013 Scott Lembcke and Howling Moon Software
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to deal
  5. * in the Software without restriction, including without limitation the rights
  6. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. * copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  19. * SOFTWARE.
  20. */
  21. // All of the struct definitions for Chipmunk should be considered part of the private API.
  22. // However, it is very valuable to know the struct sizes for preallocating memory.
  23. #ifndef CHIPMUNK_STRUCTS_H
  24. #define CHIPMUNK_STRUCTS_H
  25. #include "chipmunk/chipmunk.h"
  26. struct cpArray {
  27. int num, max;
  28. void **arr;
  29. };
  30. struct cpBody {
  31. // Integration functions
  32. cpBodyVelocityFunc velocity_func;
  33. cpBodyPositionFunc position_func;
  34. // mass and it's inverse
  35. cpFloat m;
  36. cpFloat m_inv;
  37. // moment of inertia and it's inverse
  38. cpFloat i;
  39. cpFloat i_inv;
  40. // center of gravity
  41. cpVect cog;
  42. // position, velocity, force
  43. cpVect p;
  44. cpVect v;
  45. cpVect f;
  46. // Angle, angular velocity, torque (radians)
  47. cpFloat a;
  48. cpFloat w;
  49. cpFloat t;
  50. cpTransform transform;
  51. cpDataPointer userData;
  52. // "pseudo-velocities" used for eliminating overlap.
  53. // Erin Catto has some papers that talk about what these are.
  54. cpVect v_bias;
  55. cpFloat w_bias;
  56. cpSpace *space;
  57. cpShape *shapeList;
  58. cpArbiter *arbiterList;
  59. cpConstraint *constraintList;
  60. struct {
  61. cpBody *root;
  62. cpBody *next;
  63. cpFloat idleTime;
  64. } sleeping;
  65. };
  66. enum cpArbiterState {
  67. // Arbiter is active and its the first collision.
  68. CP_ARBITER_STATE_FIRST_COLLISION,
  69. // Arbiter is active and its not the first collision.
  70. CP_ARBITER_STATE_NORMAL,
  71. // Collision has been explicitly ignored.
  72. // Either by returning false from a begin collision handler or calling cpArbiterIgnore().
  73. CP_ARBITER_STATE_IGNORE,
  74. // Collison is no longer active. A space will cache an arbiter for up to cpSpace.collisionPersistence more steps.
  75. CP_ARBITER_STATE_CACHED,
  76. // Collison arbiter is invalid because one of the shapes was removed.
  77. CP_ARBITER_STATE_INVALIDATED,
  78. };
  79. struct cpArbiterThread {
  80. struct cpArbiter *next, *prev;
  81. };
  82. struct cpContact {
  83. cpVect r1, r2;
  84. cpFloat nMass, tMass;
  85. cpFloat bounce; // TODO: look for an alternate bounce solution.
  86. cpFloat jnAcc, jtAcc, jBias;
  87. cpFloat bias;
  88. cpHashValue hash;
  89. };
  90. struct cpCollisionInfo {
  91. const cpShape *a, *b;
  92. cpCollisionID id;
  93. cpVect n;
  94. int count;
  95. // TODO Should this be a unique struct type?
  96. struct cpContact *arr;
  97. };
  98. struct cpArbiter {
  99. cpFloat e;
  100. cpFloat u;
  101. cpVect surface_vr;
  102. cpDataPointer data;
  103. const cpShape *a, *b;
  104. cpBody *body_a, *body_b;
  105. struct cpArbiterThread thread_a, thread_b;
  106. int count;
  107. struct cpContact *contacts;
  108. cpVect n;
  109. // Regular, wildcard A and wildcard B collision handlers.
  110. cpCollisionHandler *handler, *handlerA, *handlerB;
  111. cpBool swapped;
  112. cpTimestamp stamp;
  113. enum cpArbiterState state;
  114. };
  115. struct cpShapeMassInfo {
  116. cpFloat m;
  117. cpFloat i;
  118. cpVect cog;
  119. cpFloat area;
  120. };
  121. typedef enum cpShapeType{
  122. CP_CIRCLE_SHAPE,
  123. CP_SEGMENT_SHAPE,
  124. CP_POLY_SHAPE,
  125. CP_NUM_SHAPES
  126. } cpShapeType;
  127. typedef cpBB (*cpShapeCacheDataImpl)(cpShape *shape, cpTransform transform);
  128. typedef void (*cpShapeDestroyImpl)(cpShape *shape);
  129. typedef void (*cpShapePointQueryImpl)(const cpShape *shape, cpVect p, cpPointQueryInfo *info);
  130. typedef void (*cpShapeSegmentQueryImpl)(const cpShape *shape, cpVect a, cpVect b, cpFloat radius, cpSegmentQueryInfo *info);
  131. typedef struct cpShapeClass cpShapeClass;
  132. struct cpShapeClass {
  133. cpShapeType type;
  134. cpShapeCacheDataImpl cacheData;
  135. cpShapeDestroyImpl destroy;
  136. cpShapePointQueryImpl pointQuery;
  137. cpShapeSegmentQueryImpl segmentQuery;
  138. };
  139. struct cpShape {
  140. const cpShapeClass *klass;
  141. cpSpace *space;
  142. cpBody *body;
  143. struct cpShapeMassInfo massInfo;
  144. cpBB bb;
  145. cpBool sensor;
  146. cpFloat e;
  147. cpFloat u;
  148. cpVect surfaceV;
  149. cpDataPointer userData;
  150. cpCollisionType type;
  151. cpShapeFilter filter;
  152. cpShape *next;
  153. cpShape *prev;
  154. cpHashValue hashid;
  155. };
  156. struct cpCircleShape {
  157. cpShape shape;
  158. cpVect c, tc;
  159. cpFloat r;
  160. };
  161. struct cpSegmentShape {
  162. cpShape shape;
  163. cpVect a, b, n;
  164. cpVect ta, tb, tn;
  165. cpFloat r;
  166. cpVect a_tangent, b_tangent;
  167. };
  168. struct cpSplittingPlane {
  169. cpVect v0, n;
  170. };
  171. #define CP_POLY_SHAPE_INLINE_ALLOC 6
  172. struct cpPolyShape {
  173. cpShape shape;
  174. cpFloat r;
  175. int count;
  176. // The untransformed planes are appended at the end of the transformed planes.
  177. struct cpSplittingPlane *planes;
  178. // Allocate a small number of splitting planes internally for simple poly.
  179. struct cpSplittingPlane _planes[2*CP_POLY_SHAPE_INLINE_ALLOC];
  180. };
  181. typedef void (*cpConstraintPreStepImpl)(cpConstraint *constraint, cpFloat dt);
  182. typedef void (*cpConstraintApplyCachedImpulseImpl)(cpConstraint *constraint, cpFloat dt_coef);
  183. typedef void (*cpConstraintApplyImpulseImpl)(cpConstraint *constraint, cpFloat dt);
  184. typedef cpFloat (*cpConstraintGetImpulseImpl)(cpConstraint *constraint);
  185. typedef struct cpConstraintClass {
  186. cpConstraintPreStepImpl preStep;
  187. cpConstraintApplyCachedImpulseImpl applyCachedImpulse;
  188. cpConstraintApplyImpulseImpl applyImpulse;
  189. cpConstraintGetImpulseImpl getImpulse;
  190. } cpConstraintClass;
  191. struct cpConstraint {
  192. const cpConstraintClass *klass;
  193. cpSpace *space;
  194. cpBody *a, *b;
  195. cpConstraint *next_a, *next_b;
  196. cpFloat maxForce;
  197. cpFloat errorBias;
  198. cpFloat maxBias;
  199. cpBool collideBodies;
  200. cpConstraintPreSolveFunc preSolve;
  201. cpConstraintPostSolveFunc postSolve;
  202. cpDataPointer userData;
  203. };
  204. struct cpPinJoint {
  205. cpConstraint constraint;
  206. cpVect anchorA, anchorB;
  207. cpFloat dist;
  208. cpVect r1, r2;
  209. cpVect n;
  210. cpFloat nMass;
  211. cpFloat jnAcc;
  212. cpFloat bias;
  213. };
  214. struct cpSlideJoint {
  215. cpConstraint constraint;
  216. cpVect anchorA, anchorB;
  217. cpFloat min, max;
  218. cpVect r1, r2;
  219. cpVect n;
  220. cpFloat nMass;
  221. cpFloat jnAcc;
  222. cpFloat bias;
  223. };
  224. struct cpPivotJoint {
  225. cpConstraint constraint;
  226. cpVect anchorA, anchorB;
  227. cpVect r1, r2;
  228. cpMat2x2 k;
  229. cpVect jAcc;
  230. cpVect bias;
  231. };
  232. struct cpGrooveJoint {
  233. cpConstraint constraint;
  234. cpVect grv_n, grv_a, grv_b;
  235. cpVect anchorB;
  236. cpVect grv_tn;
  237. cpFloat clamp;
  238. cpVect r1, r2;
  239. cpMat2x2 k;
  240. cpVect jAcc;
  241. cpVect bias;
  242. };
  243. struct cpDampedSpring {
  244. cpConstraint constraint;
  245. cpVect anchorA, anchorB;
  246. cpFloat restLength;
  247. cpFloat stiffness;
  248. cpFloat damping;
  249. cpDampedSpringForceFunc springForceFunc;
  250. cpFloat target_vrn;
  251. cpFloat v_coef;
  252. cpVect r1, r2;
  253. cpFloat nMass;
  254. cpVect n;
  255. cpFloat jAcc;
  256. };
  257. struct cpDampedRotarySpring {
  258. cpConstraint constraint;
  259. cpFloat restAngle;
  260. cpFloat stiffness;
  261. cpFloat damping;
  262. cpDampedRotarySpringTorqueFunc springTorqueFunc;
  263. cpFloat target_wrn;
  264. cpFloat w_coef;
  265. cpFloat iSum;
  266. cpFloat jAcc;
  267. };
  268. struct cpRotaryLimitJoint {
  269. cpConstraint constraint;
  270. cpFloat min, max;
  271. cpFloat iSum;
  272. cpFloat bias;
  273. cpFloat jAcc;
  274. };
  275. struct cpRatchetJoint {
  276. cpConstraint constraint;
  277. cpFloat angle, phase, ratchet;
  278. cpFloat iSum;
  279. cpFloat bias;
  280. cpFloat jAcc;
  281. };
  282. struct cpGearJoint {
  283. cpConstraint constraint;
  284. cpFloat phase, ratio;
  285. cpFloat ratio_inv;
  286. cpFloat iSum;
  287. cpFloat bias;
  288. cpFloat jAcc;
  289. };
  290. struct cpSimpleMotor {
  291. cpConstraint constraint;
  292. cpFloat rate;
  293. cpFloat iSum;
  294. cpFloat jAcc;
  295. };
  296. typedef struct cpContactBufferHeader cpContactBufferHeader;
  297. typedef void (*cpSpaceArbiterApplyImpulseFunc)(cpArbiter *arb);
  298. struct cpSpace {
  299. int iterations;
  300. cpVect gravity;
  301. cpFloat damping;
  302. cpFloat idleSpeedThreshold;
  303. cpFloat sleepTimeThreshold;
  304. cpFloat collisionSlop;
  305. cpFloat collisionBias;
  306. cpTimestamp collisionPersistence;
  307. cpDataPointer userData;
  308. cpTimestamp stamp;
  309. cpFloat curr_dt;
  310. cpArray *dynamicBodies;
  311. cpArray *staticBodies;
  312. cpArray *rousedBodies;
  313. cpArray *sleepingComponents;
  314. cpHashValue shapeIDCounter;
  315. cpSpatialIndex *staticShapes;
  316. cpSpatialIndex *dynamicShapes;
  317. cpArray *constraints;
  318. cpArray *arbiters;
  319. cpContactBufferHeader *contactBuffersHead;
  320. cpHashSet *cachedArbiters;
  321. cpArray *pooledArbiters;
  322. cpArray *allocatedBuffers;
  323. unsigned int locked;
  324. cpBool usesWildcards;
  325. cpHashSet *collisionHandlers;
  326. cpCollisionHandler defaultHandler;
  327. cpBool skipPostStep;
  328. cpArray *postStepCallbacks;
  329. cpBody *staticBody;
  330. cpBody _staticBody;
  331. };
  332. typedef struct cpPostStepCallback {
  333. cpPostStepFunc func;
  334. void *key;
  335. void *data;
  336. } cpPostStepCallback;
  337. #endif