PhysicsWorld.pkg 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. $#include "PhysicsWorld.h"
  2. /// Physics raycast hit.
  3. struct PhysicsRaycastResult
  4. {
  5. PhysicsRaycastResult();
  6. /// Hit position.
  7. Vector3 position_ @ position;
  8. /// Hit normal.
  9. Vector3 normal_ @ normal;
  10. /// Hit distance from ray origin.
  11. float distance_ @ distance;
  12. /// Rigid body that was hit.
  13. RigidBody* body_ @ body;
  14. };
  15. /// Delayed world transform assignment for parented rigidbodies.
  16. struct DelayedWorldTransform
  17. {
  18. /// Rigid body.
  19. RigidBody* rigidBody_ @ rigidBody;
  20. /// Parent rigid body.
  21. RigidBody* parentRigidBody_ @ parentRigidBody;
  22. /// New world position.
  23. Vector3 worldPosition_ @ worldPosition;
  24. /// New world rotation.
  25. Quaternion worldRotation_ @ worldRotation;
  26. };
  27. /// Physics simulation world component. Should be added only to the root scene node.
  28. class PhysicsWorld : public Component
  29. {
  30. public:
  31. /// Step the simulation forward.
  32. void Update(float timeStep);
  33. /// Refresh collisions only without updating dynamics.
  34. void UpdateCollisions();
  35. /// Set simulation steps per second.
  36. void SetFps(int fps);
  37. /// Set gravity.
  38. void SetGravity(Vector3 gravity);
  39. /// Set number of constraint solver iterations.
  40. void SetNumIterations(int num);
  41. /// Set whether to interpolate between simulation steps.
  42. void SetInterpolation(bool enable);
  43. /// Set whether to use Bullet's internal edge utility for trimesh collisions. Disabled by default.
  44. void SetInternalEdge(bool enable);
  45. /// Set split impulse collision mode. This is more accurate, but slower. Disabled by default.
  46. void SetSplitImpulse(bool enable);
  47. /// Set maximum angular velocity for network replication.
  48. void SetMaxNetworkAngularVelocity(float velocity);
  49. /// Perform a physics world raycast and return all hits.
  50. /// Perform a physics world raycast and return the closest hit.
  51. void RaycastSingle(PhysicsRaycastResult& result, const Ray& ray, float maxDistance, unsigned collisionMask = M_MAX_UNSIGNED);
  52. /// Perform a physics world swept sphere test and return the closest hit.
  53. void SphereCast(PhysicsRaycastResult& result, const Ray& ray, float radius, float maxDistance, unsigned collisionMask = M_MAX_UNSIGNED);
  54. /// Return gravity.
  55. Vector3 GetGravity() const;
  56. /// Return number of constraint solver iterations.
  57. int GetNumIterations() const;
  58. /// Return whether interpolation between simulation steps is enabled.
  59. bool GetInterpolation() const { return interpolation_; }
  60. /// Return whether Bullet's internal edge utility for trimesh collisions is enabled.
  61. bool GetInternalEdge() const { return internalEdge_; }
  62. /// Return whether split impulse collision mode is enabled.
  63. bool GetSplitImpulse() const;
  64. /// Return simulation steps per second.
  65. int GetFps() const { return fps_; }
  66. /// Return maximum angular velocity for network replication.
  67. float GetMaxNetworkAngularVelocity() const { return maxNetworkAngularVelocity_; }
  68. /// Add a rigid body to keep track of. Called by RigidBody.
  69. void AddRigidBody(RigidBody* body);
  70. /// Remove a rigid body. Called by RigidBody.
  71. void RemoveRigidBody(RigidBody* body);
  72. /// Add a collision shape to keep track of. Called by CollisionShape.
  73. void AddCollisionShape(CollisionShape* shape);
  74. /// Remove a collision shape. Called by CollisionShape.
  75. void RemoveCollisionShape(CollisionShape* shape);
  76. /// Add a constraint to keep track of. Called by Constraint.
  77. void AddConstraint(Constraint* joint);
  78. /// Remove a constraint. Called by Constraint.
  79. void RemoveConstraint(Constraint* joint);
  80. /// Add a delayed world transform assignment. Called by RigidBody.
  81. void AddDelayedWorldTransform(const DelayedWorldTransform& transform);
  82. /// Add debug geometry to the debug renderer.
  83. void DrawDebugGeometry(bool depthTest);
  84. /// Set debug renderer to use. Called both by PhysicsWorld itself and physics components.
  85. void SetDebugRenderer(DebugRenderer* debug);
  86. /// Set debug geometry depth test mode. Called both by PhysicsWorld itself and physics components.
  87. void SetDebugDepthTest(bool enable);
  88. /// Clean up the geometry cache.
  89. void CleanupGeometryCache();
  90. /// Set node dirtying to be disregarded.
  91. void SetApplyingTransforms(bool enable);
  92. /// Return whether node dirtying should be disregarded.
  93. bool IsApplyingTransforms() const;
  94. // Properties:
  95. tolua_property__get_set Vector3 gravity;
  96. tolua_property__get_set int numIterations;
  97. tolua_property__get_set int fps;
  98. tolua_property__get_set bool interpolation;
  99. tolua_property__get_set bool internalEdge;
  100. tolua_property__get_set bool splitImpulse;
  101. };
  102. ${
  103. // Patch for PhysicsWorld.gravity property.
  104. #define TOLUA_DISABLE_tolua_get_PhysicsWorld_gravity
  105. #define tolua_get_PhysicsWorld_gravity tolua_PhysicsLuaAPI_PhysicsWorld_GetGravity00
  106. $}