PolyPhysicsScreen.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * PolyPhysicsScreen.h
  3. * Poly
  4. *
  5. * Created by Ivan Safrin on 5/8/08.
  6. * Copyright 2008 __MyCompanyName__. All rights reserved.
  7. *
  8. */
  9. // @package ScreenPhysics
  10. #pragma once
  11. #include "PolyGlobals.h"
  12. #include "PolyScreen.h"
  13. #include "Box2D/Box2D.h"
  14. //#include "PolyCoreServices.h"
  15. #include "PolyScreenLine.h"
  16. #include "PolyPhysicsScreenEntity.h"
  17. #include "PolyTimer.h"
  18. #include <vector>
  19. #define MAX_B2DCONTACTPOINTS 2048
  20. namespace Polycode {
  21. class _PolyExport PhysicsScreenEvent : public Event {
  22. public:
  23. PhysicsScreenEntity *entity1;
  24. PhysicsScreenEntity *entity2;
  25. static const int EVENT_NEW_SHAPE_COLLISION = 0;
  26. static const int EVENT_END_SHAPE_COLLISION = 1;
  27. static const int EVENT_PERSIST_SHAPE_COLLISION = 2;
  28. };
  29. enum ContactState
  30. {
  31. e_contactAdded = 0,
  32. e_contactPersisted = 1,
  33. e_contactRemoved = 2,
  34. };
  35. struct ContactPoint
  36. {
  37. b2Shape* shape1;
  38. b2Shape* shape2;
  39. b2Vec2 normal;
  40. b2Vec2 position;
  41. b2Vec2 velocity;
  42. b2ContactID id;
  43. ContactState state;
  44. };
  45. class _PolyExport PhysicsJoint {
  46. public:
  47. PhysicsJoint() {}
  48. ~PhysicsJoint() {}
  49. b2Joint *box2DJoint;
  50. };
  51. class _PolyExport PhysicsScreen : public Screen, b2ContactListener {
  52. public:
  53. PhysicsScreen();
  54. PhysicsScreen(float worldScale, float freq);
  55. ~PhysicsScreen();
  56. void Update();
  57. PhysicsScreenEntity *addPhysicsChild(ScreenEntity *newEntity, int entType, float friction=0.1, float density=1, float restitution = 0, bool isSensor = false, bool fixedRotation = false);
  58. void removePhysicsChild(PhysicsScreenEntity *entityToRemove);
  59. PhysicsScreenEntity *addCollisionChild(ScreenEntity *newEntity, int entType);
  60. void destroyJoint(PhysicsJoint *joint);
  61. void createDistanceJoint(ScreenEntity *ent1, ScreenEntity *ent2, bool collideConnected);
  62. void createPrismaticJoint(ScreenEntity *ent1, ScreenEntity *ent2, bool collideConnected);
  63. PhysicsJoint *createRevoluteJoint(ScreenEntity *ent1, ScreenEntity *ent2, float ax, float ay, bool enableLimit, float lowerLimit, float upperLimit, bool motorEnabled, float motorSpeed, float maxTorque);
  64. // b2MouseJoint *createMouseJoint(ScreenEntity *ent1, Vector2 *mp);
  65. void applyForce(ScreenEntity *ent, float fx, float fy);
  66. void applyImpulse(ScreenEntity *ent, float fx, float fy);
  67. void setGravity(Vector2 newGravity);
  68. void setTransform(ScreenEntity *ent, Vector2 pos, float angle);
  69. PhysicsScreenEntity *getPhysicsEntityByShape(b2Shape *shape);
  70. PhysicsScreenEntity *getPhysicsEntityByFixture(b2Fixture *fixture);
  71. void setVelocity(ScreenEntity *ent, float fx, float fy);
  72. void setVelocityX(ScreenEntity *ent, float fx);
  73. void setVelocityY(ScreenEntity *ent, float fy);
  74. void setSpin(ScreenEntity *ent, float spin);
  75. Vector2 getVelocity(ScreenEntity *ent);
  76. void BeginContact (b2Contact *contact);
  77. void EndContact (b2Contact *contact);
  78. void wakeUp(ScreenEntity *ent);
  79. void handleEvent(Event *event);
  80. Vector2 getEntityCollisionNormal(ScreenEntity *ent1, ScreenEntity *ent2);
  81. bool areEntitiesColliding(ScreenEntity *ent1, ScreenEntity *ent2);
  82. ScreenEntity *getEntityAtPosition(float x, float y);
  83. bool testEntityAtPosition(ScreenEntity *ent, float x, float y);
  84. void Shutdown();
  85. PhysicsScreenEntity *getPhysicsByScreenEntity(ScreenEntity *ent);
  86. void destroyMouseJoint(b2MouseJoint *mJoint);
  87. protected:
  88. float worldScale;
  89. void init(float worldScale, float physicsTimeStep, int physicsIterations, Vector2 physicsGravity);
  90. Timer *updateTimer;
  91. vector <PhysicsScreenEntity*> physicsChildren;
  92. ContactPoint m_points[MAX_B2DCONTACTPOINTS];
  93. int32 numContactPoints;
  94. b2World *world;
  95. float32 timeStep;
  96. int32 iterations;
  97. };
  98. }