PolyPhysicsScreen.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 "PolyScreen.h"
  22. #include "Box2D/Box2D.h"
  23. //#include "PolyCoreServices.h"
  24. #include "PolyScreenLine.h"
  25. #include "PolyPhysicsScreenEntity.h"
  26. #include "PolyTimer.h"
  27. #include <vector>
  28. #define MAX_B2DCONTACTPOINTS 2048
  29. namespace Polycode {
  30. class _PolyExport PhysicsScreenEvent : public Event {
  31. public:
  32. PhysicsScreenEntity *entity1;
  33. PhysicsScreenEntity *entity2;
  34. static const int EVENT_NEW_SHAPE_COLLISION = 0;
  35. static const int EVENT_END_SHAPE_COLLISION = 1;
  36. static const int EVENT_PERSIST_SHAPE_COLLISION = 2;
  37. };
  38. enum ContactState
  39. {
  40. e_contactAdded = 0,
  41. e_contactPersisted = 1,
  42. e_contactRemoved = 2,
  43. };
  44. struct ContactPoint
  45. {
  46. b2Shape* shape1;
  47. b2Shape* shape2;
  48. b2Vec2 normal;
  49. b2Vec2 position;
  50. b2Vec2 velocity;
  51. b2ContactID id;
  52. ContactState state;
  53. };
  54. class _PolyExport PhysicsJoint {
  55. public:
  56. PhysicsJoint() {}
  57. ~PhysicsJoint() {}
  58. b2Joint *box2DJoint;
  59. };
  60. /**
  61. * A 2D Physics enabled screen.
  62. */
  63. class _PolyExport PhysicsScreen : public Screen, b2ContactListener {
  64. public:
  65. /**
  66. *
  67. */
  68. PhysicsScreen(Number worldScale, Number freq);
  69. /**
  70. * Default constructor.
  71. */
  72. PhysicsScreen();
  73. ~PhysicsScreen();
  74. void Update();
  75. /**
  76. * Adds a ScreenEntity as a physics enabled child.
  77. * @param newEntity Screen entity to add.
  78. * @param entType Physics entity type to add as. Possible values are PhysicsScreenEntity::ENTITY_RECT, PhysicsScreenEntity::ENTITY_CIRCLE and PhysicsScreenEntity::ENTITY_STATICRECT
  79. * @return The physics entity wrapper.
  80. */
  81. PhysicsScreenEntity *addPhysicsChild(ScreenEntity *newEntity, int entType, Number friction=0.1, Number density=1, Number restitution = 0, bool isSensor = false, bool fixedRotation = false);
  82. void removePhysicsChild(PhysicsScreenEntity *entityToRemove);
  83. PhysicsScreenEntity *addCollisionChild(ScreenEntity *newEntity, int entType);
  84. void destroyJoint(PhysicsJoint *joint);
  85. void createDistanceJoint(ScreenEntity *ent1, ScreenEntity *ent2, bool collideConnected);
  86. void createPrismaticJoint(ScreenEntity *ent1, ScreenEntity *ent2, bool collideConnected);
  87. PhysicsJoint *createRevoluteJoint(ScreenEntity *ent1, ScreenEntity *ent2, Number ax, Number ay, bool enableLimit, Number lowerLimit, Number upperLimit, bool motorEnabled, Number motorSpeed, Number maxTorque);
  88. // b2MouseJoint *createMouseJoint(ScreenEntity *ent1, Vector2 *mp);
  89. void applyForce(ScreenEntity *ent, Number fx, Number fy);
  90. void applyImpulse(ScreenEntity *ent, Number fx, Number fy);
  91. void setGravity(Vector2 newGravity);
  92. void setTransform(ScreenEntity *ent, Vector2 pos, Number angle);
  93. PhysicsScreenEntity *getPhysicsEntityByShape(b2Shape *shape);
  94. PhysicsScreenEntity *getPhysicsEntityByFixture(b2Fixture *fixture);
  95. void setVelocity(ScreenEntity *ent, Number fx, Number fy);
  96. void setVelocityX(ScreenEntity *ent, Number fx);
  97. void setVelocityY(ScreenEntity *ent, Number fy);
  98. void setSpin(ScreenEntity *ent, Number spin);
  99. Vector2 getVelocity(ScreenEntity *ent);
  100. void BeginContact (b2Contact *contact);
  101. void EndContact (b2Contact *contact);
  102. void wakeUp(ScreenEntity *ent);
  103. void handleEvent(Event *event);
  104. Vector2 getEntityCollisionNormal(ScreenEntity *ent1, ScreenEntity *ent2);
  105. bool areEntitiesColliding(ScreenEntity *ent1, ScreenEntity *ent2);
  106. ScreenEntity *getEntityAtPosition(Number x, Number y);
  107. bool testEntityAtPosition(ScreenEntity *ent, Number x, Number y);
  108. void Shutdown();
  109. PhysicsScreenEntity *getPhysicsByScreenEntity(ScreenEntity *ent);
  110. void destroyMouseJoint(b2MouseJoint *mJoint);
  111. protected:
  112. Number worldScale;
  113. void init(Number worldScale, Number physicsTimeStep, int physicsIterations, Vector2 physicsGravity);
  114. Timer *updateTimer;
  115. vector <PhysicsScreenEntity*> physicsChildren;
  116. ContactPoint m_points[MAX_B2DCONTACTPOINTS];
  117. int32 numContactPoints;
  118. b2World *world;
  119. Number timeStep;
  120. int32 iterations;
  121. };
  122. }