PolyPhysicsScreen.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 PhysicsScreen : public Screen, b2ContactListener {
  46. public:
  47. PhysicsScreen();
  48. PhysicsScreen(float worldScale, float freq);
  49. ~PhysicsScreen();
  50. void Update();
  51. PhysicsScreenEntity *addPhysicsChild(ScreenEntity *newEntity, int entType, float friction, float density, float restitution = 0, bool isSensor = false, bool fixedRotation = false);
  52. void removePhysicsChild(PhysicsScreenEntity *entityToRemove);
  53. PhysicsScreenEntity *addCollisionChild(ScreenEntity *newEntity, int entType);
  54. void createDistanceJoint(ScreenEntity *ent1, ScreenEntity *ent2, bool collideConnected);
  55. void createPrismaticJoint(ScreenEntity *ent1, ScreenEntity *ent2, bool collideConnected);
  56. b2RevoluteJoint *createRevoluteJoint(ScreenEntity *ent1, ScreenEntity *ent2, float ax, float ay, bool enableLimit, float lowerLimit, float upperLimit, bool motorEnabled, float motorSpeed, float maxTorque);
  57. // b2MouseJoint *createMouseJoint(ScreenEntity *ent1, Vector2 *mp);
  58. void applyForce(ScreenEntity *ent, float fx, float fy);
  59. void applyImpulse(ScreenEntity *ent, float fx, float fy);
  60. void setGravity(Vector2 newGravity);
  61. PhysicsScreenEntity *getPhysicsEntityByShape(b2Shape *shape);
  62. void setVelocity(ScreenEntity *ent, float fx, float fy);
  63. void setVelocityX(ScreenEntity *ent, float fx);
  64. void setVelocityY(ScreenEntity *ent, float fy);
  65. /*
  66. void Add(const b2ContactPoint* point);
  67. void Persist(const b2ContactPoint* point);
  68. void Remove(const b2ContactPoint* point);
  69. */
  70. void BeginContact (b2Contact *contact);
  71. void EndContact (b2Contact *contact);
  72. void wakeUp(ScreenEntity *ent);
  73. void handleEvent(Event *event);
  74. Vector2 getEntityCollisionNormal(ScreenEntity *ent1, ScreenEntity *ent2);
  75. bool areEntitiesColliding(ScreenEntity *ent1, ScreenEntity *ent2);
  76. ScreenEntity *getEntityAtPosition(float x, float y);
  77. bool testEntityAtPosition(ScreenEntity *ent, float x, float y);
  78. void Shutdown();
  79. PhysicsScreenEntity *getPhysicsByScreenEntity(ScreenEntity *ent);
  80. void destroyMouseJoint(b2MouseJoint *mJoint);
  81. protected:
  82. float worldScale;
  83. void init(float worldScale, float physicsTimeStep, int physicsIterations, Vector2 physicsGravity);
  84. Timer *updateTimer;
  85. vector <PhysicsScreenEntity*> physicsChildren;
  86. ContactPoint m_points[MAX_B2DCONTACTPOINTS];
  87. int32 numContactPoints;
  88. b2World *world;
  89. float32 timeStep;
  90. int32 iterations;
  91. };
  92. }