Actor.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. struct Actor
  49. {
  50. /// Constructor
  51. Actor(const PhysicsResource* res, const PhysicsConfigResource* config, uint32_t index, PxPhysics* physics, PxCooking* cooking,
  52. PxScene* scene, SceneGraph& sg, int32_t node, Unit* unit, const Vector3& pos, const Quaternion& rot);
  53. /// Destructor
  54. ~Actor();
  55. /// Makes the actor subject to gravity
  56. void enable_gravity();
  57. /// Makes the actor unsubject to gravity
  58. void disable_gravity();
  59. void enable_collision();
  60. void disable_collision();
  61. /// Makes the actor kinematic (keyframed)
  62. /// @note
  63. /// Works only for dynamic actors
  64. void set_kinematic();
  65. /// Makes the actor dynamic
  66. /// @note
  67. /// Works only for kinematic actors
  68. void clear_kinematic();
  69. /// Moves the actor to @a pos
  70. /// @note
  71. /// Works only for kinematic actors
  72. void move(const Vector3& pos);
  73. /// Returns whether the actor is static (i.e. immovable).
  74. bool is_static() const;
  75. /// Returns whether the actor is dynamic (i.e. driven dy physics).
  76. bool is_dynamic() const;
  77. /// Returns whether the actor is kinematic (i.e. driven by the user).
  78. bool is_kinematic() const;
  79. /// Returns the rate at which rigid bodies dissipate linear momentum
  80. float linear_damping() const;
  81. /// Sets the rate at which rigid bodies dissipate linear momentum
  82. void set_linear_damping(float rate);
  83. /// Returns the rate at which rigid bodies dissipate angular momentum
  84. float angular_damping() const;
  85. /// Sets the rate at which rigid bodies dissipate angular momentum
  86. void set_angular_damping(float rate);
  87. /// Returns linear velocity of the actor
  88. /// @note
  89. /// If actor is sleeping, linear velocity must be 0
  90. Vector3 linear_velocity() const;
  91. /// Sets linear velocity of the actor
  92. /// @note
  93. /// If actor is sleeping, this will wake it up
  94. void set_linear_velocity(const Vector3& vel);
  95. /// Returns angular velocity of the actor
  96. /// @note
  97. /// If actor is sleeping, angular velocity must be 0
  98. Vector3 angular_velocity() const;
  99. /// Sets angular velocity of the actor
  100. /// @note
  101. /// If actor is sleeping, this will wake it up
  102. void set_angular_velocity(const Vector3& vel);
  103. /// Applies a force (or impulse) defined in the global coordinate frame, acting at a particular point in global coordinates, to the actor.
  104. /// @note
  105. /// If the force does not act along the center of mass of the actor, this will also add the corresponding torque.
  106. /// 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.
  107. void add_impulse(const Vector3& impulse);
  108. /// Applies a force (or impulse) defined in the global coordinate frame, acting at a particular point in local coordinates, to the actor.
  109. /// @note
  110. /// If the force does not act along the center of mass of the actor, this will also add the corresponding torque.
  111. /// 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.
  112. void add_impulse_at(const Vector3& impulse, const Vector3& pos);
  113. /// Applies a force, evaluated by actor's @a mass and @a velocity that will be achieved, to the actor
  114. void push(const Vector3& vel, const float mass);
  115. /// Returns true if tha actor is sleeping, false otherwise
  116. bool is_sleeping();
  117. /// Forces the actor to wake up
  118. void wake_up();
  119. /// Returns actor's name
  120. StringId32 name();
  121. /// Returns the unit that owns this actor
  122. Unit* unit();
  123. private:
  124. void update(const Matrix4x4& pose);
  125. void create_shapes(const PhysicsResource* res, const PhysicsConfigResource* config, PxPhysics* physics, PxCooking* cooking);
  126. Matrix4x4 get_kinematic_pose() const;
  127. public:
  128. const PhysicsResource* m_resource;
  129. const PhysicsConfigResource* m_config;
  130. uint32_t m_index;
  131. Unit* m_unit;
  132. PxScene* m_scene;
  133. SceneGraph& m_scene_graph;
  134. int32_t m_node;
  135. PxRigidActor* m_actor;
  136. uint32_t m_group;
  137. uint32_t m_mask;
  138. private:
  139. friend class PhysicsWorld;
  140. };
  141. } // namespace crown