2
0

PolyPhysicsScreen.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. /*
  2. Copyright (C) 2011 by Ivan Safrin
  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. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #pragma once
  20. #include "PolyGlobals.h"
  21. #include "PolyEvent.h"
  22. #include "PolyScene.h"
  23. #include "PolyVector2.h"
  24. #include "Box2D/Box2D.h"
  25. #include <vector>
  26. #define MAX_B2DCONTACTPOINTS 2048
  27. namespace Polycode {
  28. class Entity;
  29. class PhysicsScene2DEntity;
  30. class Timer;
  31. /**
  32. * Event sent out by the PhysicsScreen class when collisions begin and end.
  33. */
  34. class _PolyExport PhysicsScene2DEvent : public Event {
  35. public:
  36. PhysicsScene2DEvent();
  37. ~PhysicsScene2DEvent();
  38. /**
  39. * First colliding entity.
  40. */
  41. Entity *entity1;
  42. /**
  43. * Second colliding entity.
  44. */
  45. Entity *entity2;
  46. /**
  47. * First colliding entity.
  48. */
  49. Entity *getFirstEntity();
  50. /**
  51. * Second colliding entity.
  52. */
  53. Entity *getSecondEntity();
  54. /**
  55. * Local collision normal.
  56. */
  57. Vector2 localCollisionNormal;
  58. /**
  59. * Collision normal in world space
  60. */
  61. Vector2 worldCollisionNormal;
  62. /**
  63. * Collision point in local space
  64. */
  65. Vector2 localCollisionPoint;
  66. /**
  67. * Collision point in world space
  68. */
  69. Vector2 worldCollisionPoint;
  70. /**
  71. * Raw Box2d Contact
  72. */
  73. b2Contact POLYIGNORE *contact;
  74. /**
  75. * Strength of the collision impact.
  76. */
  77. Number impactStrength;
  78. /**
  79. * Friction strength of the impact
  80. */
  81. Number frictionStrength;
  82. static const int EVENTBASE_PHYSICSSCREENEVENT = 0x800;
  83. /**
  84. * Event sent out when a collision begins
  85. */
  86. static const int EVENT_NEW_SHAPE_COLLISION = EVENTBASE_PHYSICSSCREENEVENT+0;
  87. /**
  88. * Event sent out when a collision ends
  89. */
  90. static const int EVENT_END_SHAPE_COLLISION = EVENTBASE_PHYSICSSCREENEVENT+1;
  91. /**
  92. * Event sent out when a collision begins
  93. */
  94. static const int EVENT_SOLVE_SHAPE_COLLISION = EVENTBASE_PHYSICSSCREENEVENT+3;
  95. };
  96. enum ContactState
  97. {
  98. e_contactAdded = 0,
  99. e_contactPersisted = 1,
  100. e_contactRemoved = 2,
  101. };
  102. struct ContactPoint
  103. {
  104. b2Shape* shape1;
  105. b2Shape* shape2;
  106. b2Vec2 normal;
  107. b2Vec2 position;
  108. b2Vec2 velocity;
  109. b2ContactID id;
  110. ContactState state;
  111. };
  112. class _PolyExport PhysicsJoint {
  113. public:
  114. PhysicsJoint() {}
  115. ~PhysicsJoint() {}
  116. b2Joint *box2DJoint;
  117. };
  118. /**
  119. * A 2D Physics enabled screen. A PhysicsScreen acts like a normal screen, except that entities added to it with addPhysicsChild have physics automatically simulated. You can also use it to check collisions using addCollisionChild.
  120. */
  121. class _PolyExport PhysicsScene2D : public Scene, b2ContactListener {
  122. public:
  123. /**
  124. * Creates a new physics screen.
  125. */
  126. PhysicsScene2D(Number worldScale, int velIterations=10, int posIterations=10);
  127. /**
  128. * Default constructor.
  129. */
  130. PhysicsScene2D();
  131. ~PhysicsScene2D();
  132. void fixedUpdate();
  133. /**
  134. * Adds a Entity as a physics enabled child. Once an entity is added as a physics child, its transforms are set by the physics engine and you are not able to position it manually. Use addCollisionChild/trackCollisionChild to track collisions of entities that you can position manually.
  135. * @param newEntity Screen entity to add.
  136. * @param entType Physics entity type to add as. Possible values are PhysicsEntity::ENTITY_RECT, PhysicsEntity::ENTITY_CIRCLE and PhysicsEntity::ENTITY_MESH. If the type is ENTITY_MESH, the Entity passed must be a ScreenMesh!
  137. * @param isStatic If this parameter is true, the body is static (doesn't move on its own).
  138. * @param friction Friction of the physics entity. Friction controls how entities drag along each other.
  139. * @param density Density of the physics entity. Density controls how heavy the entity is.
  140. * @param restitution Restitution of the physics entity. Restitution controls how bouncy the entity is.
  141. * @param isSensor If this is set to true, the entity won't collide with other entities, but its collision will register.
  142. * @param fixedRotation If this is set to true, the entity will always have a locked rotation.
  143. * @param groupIndex is the physiscs shape's collision group. A negative number means objects of that group won't collide with eachother
  144. * @return The physics entity wrapper.
  145. */
  146. PhysicsScene2DEntity *addPhysicsChild(Entity *newEntity, int entType, bool isStatic, Number friction=0.1, Number density=1, Number restitution = 0, bool isSensor = false, bool fixedRotation = false, int groupIndex = 0);
  147. /**
  148. * Tracks a Entity as a physics enabled child. Once an entity is added as a physics child, its transforms are set by the physics engine and you are not able to position it manually. Use addCollisionChild/trackCollisionChild to track collisions of entities that you can position manually.
  149. * @param newEntity Screen entity to add.
  150. * @param entType Physics entity type to add as. Possible values are PhysicsEntity::ENTITY_RECT, PhysicsEntity::ENTITY_CIRCLE and PhysicsEntity::ENTITY_MESH. If the type is ENTITY_MESH, the Entity passed must be a ScreenMesh!
  151. * @param isStatic If this parameter is true, the body is static (doesn't move on its own).
  152. * @param friction Friction of the physics entity. Friction controls how entities drag along each other.
  153. * @param density Density of the physics entity. Density controls how heavy the entity is.
  154. * @param restitution Restitution of the physics entity. Restitution controls how bouncy the entity is.
  155. * @param isSensor If this is set to true, the entity won't collide with other entities, but its collision will register.
  156. * @param fixedRotation If this is set to true, the entity will always have a locked rotation.
  157. * @param groupIndex is the physiscs shape's collision group. A negative number means objects of that group won't collide with eachother
  158. * @return The physics entity wrapper.
  159. */
  160. PhysicsScene2DEntity *trackPhysicsChild(Entity *newEntity, int entType, bool isStatic, Number friction=0.1, Number density=1, Number restitution = 0, bool isSensor = false, bool fixedRotation = false, int groupIndex = 0);
  161. /**
  162. * Stops physics tracking for this entity but does not remove from screen.
  163. * @param entity Entity to stop tracking for.
  164. */
  165. void stopTrackingChild(Entity *entity);
  166. /**
  167. * Removes a physics child from the screen.
  168. * @param entityToRemove Entity to remove from the screen.
  169. */
  170. void removePhysicsChild(Entity *entityToRemove);
  171. void removeChild(Entity *entityToRemove);
  172. /**
  173. * Begins tracking collisions for a Entity and adds it to the scene.
  174. * @param newEntity Entity to track collisions for.
  175. * @param entType Physics shape of the entity. Possible values are PhysicsEntity::ENTITY_RECT or PhysicsEntity::ENTITY_CIRCLE.
  176. * @param entityToRemove Entity to remove from the screen.
  177. */
  178. PhysicsScene2DEntity *addCollisionChild(Entity *newEntity, int entType, int groupIndex = 0, bool sensorOnly = true);
  179. /**
  180. * Begins tracking collisions for a Entity.
  181. * @param newEntity Entity to track collisions for.
  182. * @param entType Physics shape of the entity. Possible values are PhysicsEntity::ENTITY_RECT or PhysicsEntity::ENTITY_CIRCLE.
  183. * @param entityToRemove Entity to remove from the screen.
  184. */
  185. PhysicsScene2DEntity *trackCollisionChild(Entity *newEntity, int entType, int groupIndex = 0);
  186. /**
  187. * Removes an existing joint.
  188. * @param joint Joint to remove.
  189. */
  190. void destroyJoint(PhysicsJoint *joint);
  191. /**
  192. * Creates a new distance joint. Distance joints keep the two entities at a fixed distance.
  193. * @param ent1 First entity to join.
  194. * @param ent2 Second entity to join.
  195. * @param collideConnected If set to true, both entities will collide with each other, if false, they will not.
  196. * @return Created physics joint.
  197. */
  198. PhysicsJoint *createDistanceJoint(Entity *ent1, Entity *ent2, bool collideConnected);
  199. /**
  200. * Creates a new prismatic joint. Prismatic joints provide one degree of freedom between two entities.
  201. * @param ent1 First entity to join.
  202. * @param ent2 Second entity to join.
  203. * @param collideConnected If set to true, both entities will collide with each other, if false, they will not.
  204. * @param ax Anchor point x (relative to first entity)
  205. * @param ay Anchor point y (relative to first entity)
  206. * @param enableLimit If true, the rotation will be limited to the specified values
  207. * @param lowerTranslation If enableLimit is true, specifies the lower translation limit.
  208. * @param upperTranslation If enableLimit is true, specifies the upper translation limit.
  209. * @param motorEnabled If enabled, applies a constant motor to the rotation joint.
  210. * @param motorSpeed If motorEnabled is true, controls the speed at which the motor rotates.
  211. * @param maxTorque If motorEnabled is true, specifies the maximum force applied.
  212. * @param worldAxis Specifies the relative world axis for the prismatic joint.
  213. * @return Created physics joint.
  214. */
  215. PhysicsJoint *createPrismaticJoint(Entity *ent1, Entity *ent2, Vector2 worldAxis, Number ax, Number ay, bool collideConnected=false, Number lowerTranslation=0, Number upperTranslation=0, bool enableLimit=false, Number motorSpeed=0, Number motorForce=0, bool motorEnabled=false);
  216. /**
  217. * Creates a new revolute joint. Revolute joints enable one entity to rotate around a point on another entity.
  218. * @param ent1 Entity to anchor to.
  219. * @param ent2 Entity to anchor.
  220. * @param ax Anchor point x (relative to first entity)
  221. * @param ay Anchor point y (relative to first entity)
  222. * @param enableLimit If true, the rotation will be limited to the specified values
  223. * @param lowerLimit If enableLimit is true, specifies the lower limit of the rotation in degrees.
  224. * @param upperLimit If enableLimit is true, specifies the upper limit of the rotation in degrees.
  225. * @param motorEnabled If enabled, applies a constant torque to the rotation joint.
  226. * @param motorSpeed If motorEnabled is true, controls the speed at which the motor rotates.
  227. * @param maxTorque If motorEnabled is true, specifies the maximum torque applied.
  228. * @return Created physics joint.
  229. */
  230. PhysicsJoint *createRevoluteJoint(Entity *ent1, Entity *ent2, Number ax, Number ay, bool collideConnected=false, bool enableLimit=false, Number lowerLimit=0, Number upperLimit=0, bool motorEnabled=false, Number motorSpeed=0, Number maxTorque=0);
  231. // b2MouseJoint *createMouseJoint(Entity *ent1, Vector2 *mp);
  232. /**
  233. * Applies linear force to an entity.
  234. * @param ent Entity to apply force to.
  235. * @param fx X value of the force direction vector.
  236. * @param fy Y value of the force direction vector.
  237. */
  238. void applyForce(Entity *ent, Number fx, Number fy);
  239. /**
  240. * Applies an impulse to an entity
  241. * @param ent Entity to apply force to.
  242. * @param fx X value of the impulse direction vector.
  243. * @param fy Y value of the impulse direction vector.
  244. */
  245. void applyImpulse(Entity *ent, Number fx, Number fy);
  246. /**
  247. * Sets the gravity for this screen.
  248. * @param newGravity The new gravity vector.
  249. */
  250. void setGravity(Vector2 newGravity);
  251. /**
  252. * Warps an entity to the specified location and angle.
  253. * @param ent Entity to transform.
  254. * @param pos New position to set.
  255. * @param angle New angle to set.
  256. */
  257. void setTransform(Entity *ent, Vector2 pos, Number angle);
  258. PhysicsScene2DEntity *getPhysicsEntityByShape(b2Shape *shape);
  259. PhysicsScene2DEntity *getPhysicsEntityByFixture(b2Fixture *fixture);
  260. /**
  261. * Sets the linear velocity of an entity.
  262. * @param ent Entity to set velocity to.
  263. * @param fx X direction of velocity vector.
  264. * @param fy Y direction of velocity vector.
  265. */
  266. void setVelocity(Entity *ent, Number fx, Number fy);
  267. /**
  268. * Sets the linear velocity of an entity on the X axis.
  269. * @param ent Entity to set velocity to.
  270. * @param fx X direction of velocity vector.
  271. */
  272. void setVelocityX(Entity *ent, Number fx);
  273. /**
  274. * Sets the linear velocity of an entity on the Y axis.
  275. * @param ent Entity to set velocity to.
  276. * @param fy Y direction of velocity vector.
  277. */
  278. void setVelocityY(Entity *ent, Number fy);
  279. /**
  280. * Sets the angular velocity of an entity
  281. * @param ent Entity to apply spin to.
  282. * @param spin Spin value.
  283. */
  284. void setAngularVelocity(Entity *ent, Number spin);
  285. /**
  286. * Returns the velocity of an entity.
  287. * @param ent Entity to return velocity for
  288. * @return Velocity of the specified entity.
  289. */
  290. Vector2 getVelocity(Entity *ent);
  291. void BeginContact (b2Contact *contact);
  292. void EndContact (b2Contact *contact);
  293. void PreSolve(b2Contact* contact, const b2Manifold* oldManifold);
  294. void PostSolve(b2Contact* contact, const b2ContactImpulse* impulse);
  295. /**
  296. * Wake up a sleeping entity. The physics engine puts non-moving entities to sleep automatically. Use this to wake them up.
  297. * @param ent Entity to wake up.
  298. */
  299. void wakeUp(Entity *ent);
  300. void handleEvent(Event *event);
  301. /**
  302. * Returns the entity at the specified position.
  303. * @param x X position.
  304. * @param y Y position.
  305. * @return If there is a collision-tracked entity at the specified position, it will be returned, NULL if there isn't.
  306. */
  307. Entity *getEntityAtPosition(Number x, Number y);
  308. /**
  309. * Returns true if the specified entity is at the specified position.
  310. * @param ent Entity to test.
  311. * @param x X position.
  312. * @param y Y position.
  313. * @return If there specified entity overlaps the specified position, this returns true.
  314. */
  315. bool testEntityAtPosition(Entity *ent, Number x, Number y);
  316. /**
  317. * Tests collision between two entities
  318. */
  319. bool testEntityCollision(Entity *ent1, Entity *ent2);
  320. bool isEntityColliding(Entity *ent1);
  321. void Shutdown();
  322. /**
  323. * Returns the physics entity for the specified screen entity. When you add ScreenEntities to the physics screen, these physics entities are created to track the physics status of the screen entities. You don't need to deal with these ever, but if you want, you can get them anyway.
  324. * @param ent Entity instance to return the physics entity for.
  325. * @return PhysicsEntity associated with the Entity.
  326. */
  327. PhysicsScene2DEntity *getPhysicsByEntity(Entity *ent);
  328. void destroyMouseJoint(b2MouseJoint *mJoint);
  329. protected:
  330. Number worldScale;
  331. std::vector <PhysicsScene2DEntity*> physicsChildren;
  332. std::vector<PhysicsScene2DEvent*> eventsToDispatch;
  333. void init(Number worldScale, int velIterations, int posIterations, Vector2 physicsGravity);
  334. std::vector<b2Contact*> contacts;
  335. b2World *world;
  336. int32 velocityIterations, positionIterations;
  337. };
  338. typedef PhysicsScene2D PhysicsScreen;
  339. typedef PhysicsScene2DEvent PhysicsScreenEvent;
  340. }