Actor.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #pragma once
  24. #include "Types.h"
  25. #include "IdArray.h"
  26. #include "PhysicsTypes.h"
  27. #include "Vector3.h"
  28. #include "Matrix4x4.h"
  29. #include "Quaternion.h"
  30. #include "PxPhysics.h"
  31. #include "PxScene.h"
  32. #include "PxRigidActor.h"
  33. #include "PxCooking.h"
  34. using physx::PxRigidActor;
  35. using physx::PxMaterial;
  36. using physx::PxScene;
  37. using physx::PxPhysics;
  38. using physx::PxCooking;
  39. namespace crown
  40. {
  41. #define MAX_PHYSX_VERTICES 256
  42. struct PhysicsResource;
  43. struct PhysicsConfigResource;
  44. struct Quaternion;
  45. struct Matrix4x4;
  46. struct Unit;
  47. class SceneGraph;
  48. ///
  49. /// @ingroup Physics
  50. struct Actor
  51. {
  52. /// Constructor
  53. Actor(const PhysicsResource* res, const PhysicsConfigResource* config, uint32_t index, PxPhysics* physics, PxCooking* cooking,
  54. PxScene* scene, SceneGraph& sg, int32_t node, Unit* unit, const Vector3& pos, const Quaternion& rot);
  55. /// Destructor
  56. ~Actor();
  57. /// Makes the actor subject to gravity
  58. void enable_gravity();
  59. /// Makes the actor unsubject to gravity
  60. void disable_gravity();
  61. void enable_collision();
  62. void disable_collision();
  63. /// Makes the actor kinematic (keyframed)
  64. /// @note
  65. /// Works only for dynamic actors
  66. void set_kinematic();
  67. /// Makes the actor dynamic
  68. /// @note
  69. /// Works only for kinematic actors
  70. void clear_kinematic();
  71. /// Moves the actor to @a pos
  72. /// @note
  73. /// Works only for kinematic actors
  74. void move(const Vector3& pos);
  75. /// Returns whether the actor is static (i.e. immovable).
  76. bool is_static() const;
  77. /// Returns whether the actor is dynamic (i.e. driven dy physics).
  78. bool is_dynamic() const;
  79. /// Returns whether the actor is kinematic (i.e. driven by the user).
  80. bool is_kinematic() const;
  81. /// Returns the rate at which rigid bodies dissipate linear momentum
  82. float linear_damping() const;
  83. /// Sets the rate at which rigid bodies dissipate linear momentum
  84. void set_linear_damping(float rate);
  85. /// Returns the rate at which rigid bodies dissipate angular momentum
  86. float angular_damping() const;
  87. /// Sets the rate at which rigid bodies dissipate angular momentum
  88. void set_angular_damping(float rate);
  89. /// Returns linear velocity of the actor
  90. /// @note
  91. /// If actor is sleeping, linear velocity must be 0
  92. Vector3 linear_velocity() const;
  93. /// Sets linear velocity of the actor
  94. /// @note
  95. /// If actor is sleeping, this will wake it up
  96. void set_linear_velocity(const Vector3& vel);
  97. /// Returns angular velocity of the actor
  98. /// @note
  99. /// If actor is sleeping, angular velocity must be 0
  100. Vector3 angular_velocity() const;
  101. /// Sets angular velocity of the actor
  102. /// @note
  103. /// If actor is sleeping, this will wake it up
  104. void set_angular_velocity(const Vector3& vel);
  105. /// Applies a force (or impulse) defined in the global coordinate frame, acting at a particular point in global coordinates, to the actor.
  106. /// @note
  107. /// If the force does not act along the center of mass of the actor, this will also add the corresponding torque.
  108. /// Because forces are reset at the end of every timestep, you can maintain a total external force on an object by calling this once every frame.
  109. void add_impulse(const Vector3& impulse);
  110. /// Applies a force (or impulse) defined in the global coordinate frame, acting at a particular point in local coordinates, to the actor.
  111. /// @note
  112. /// If the force does not act along the center of mass of the actor, this will also add the corresponding torque.
  113. /// Because forces are reset at the end of every timestep, you can maintain a total external force on an object by calling this once every frame.
  114. void add_impulse_at(const Vector3& impulse, const Vector3& pos);
  115. /// Applies a force, evaluated by actor's @a mass and @a velocity that will be achieved, to the actor
  116. void push(const Vector3& vel, const float mass);
  117. /// Returns true if tha actor is sleeping, false otherwise
  118. bool is_sleeping();
  119. /// Forces the actor to wake up
  120. void wake_up();
  121. /// Returns actor's name
  122. StringId32 name();
  123. /// Returns the unit that owns this actor
  124. Unit* unit();
  125. private:
  126. void update(const Matrix4x4& pose);
  127. void create_shapes(const PhysicsResource* res, const PhysicsConfigResource* config, PxPhysics* physics, PxCooking* cooking);
  128. Matrix4x4 get_kinematic_pose() const;
  129. public:
  130. const PhysicsResource* m_resource;
  131. const PhysicsConfigResource* m_config;
  132. uint32_t m_index;
  133. Unit* m_unit;
  134. PxScene* m_scene;
  135. SceneGraph& m_scene_graph;
  136. int32_t m_node;
  137. PxRigidActor* m_actor;
  138. uint32_t m_group;
  139. uint32_t m_mask;
  140. private:
  141. friend class PhysicsWorld;
  142. };
  143. } // namespace crown