PolyPhysicsScreen.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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.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 freq);
  49. PhysicsScreen(Vector2 physicsWorldLowerBound, Vector2 physicsWorldUpperBound);
  50. ~PhysicsScreen();
  51. void Update();
  52. PhysicsScreenEntity *addPhysicsChild(ScreenEntity *newEntity, int entType, float friction, float density, float restitution = 0, bool isSensor = false);
  53. void removePhysicsChild(PhysicsScreenEntity *entityToRemove);
  54. PhysicsScreenEntity *addCollisionChild(ScreenEntity *newEntity, int entType);
  55. void createDistanceJoint(ScreenEntity *ent1, ScreenEntity *ent2, bool collideConnected);
  56. void createPrismaticJoint(ScreenEntity *ent1, ScreenEntity *ent2, bool collideConnected);
  57. b2RevoluteJoint *createRevoluteJoint(ScreenEntity *ent1, ScreenEntity *ent2, float ax, float ay, bool enableLimit, float lowerLimit, float upperLimit, bool motorEnabled, float motorSpeed, float maxTorque);
  58. b2MouseJoint *createMouseJoint(ScreenEntity *ent1, Vector2 *mp);
  59. void applyForce(ScreenEntity *ent, float fx, float fy);
  60. void applyImpulse(ScreenEntity *ent, float fx, float fy);
  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. void Add(const b2ContactPoint* point);
  66. void Persist(const b2ContactPoint* point);
  67. void Remove(const b2ContactPoint* point);
  68. void wakeUp(ScreenEntity *ent);
  69. void handleEvent(Event *event);
  70. Vector2 getEntityCollisionNormal(ScreenEntity *ent1, ScreenEntity *ent2);
  71. bool areEntitiesColliding(ScreenEntity *ent1, ScreenEntity *ent2);
  72. ScreenEntity *getEntityAtPosition(float x, float y);
  73. bool testEntityAtPosition(ScreenEntity *ent, float x, float y);
  74. void Shutdown();
  75. PhysicsScreenEntity *getPhysicsByScreenEntity(ScreenEntity *ent);
  76. void destroyMouseJoint(b2MouseJoint *mJoint);
  77. protected:
  78. void init(Vector2 physicsWorldLowerBound, Vector2 physicsWorldUpperBound, float physicsTimeStep, int physicsIterations, Vector2 physicsGravity);
  79. Timer *updateTimer;
  80. vector <PhysicsScreenEntity*> physicsChildren;
  81. ContactPoint m_points[MAX_B2DCONTACTPOINTS];
  82. int32 numContactPoints;
  83. b2World *world;
  84. float32 timeStep;
  85. int32 iterations;
  86. };
  87. }