Actor.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 "MathTypes.h"
  26. #include "PhysicsTypes.h"
  27. #include "IdArray.h"
  28. #include "WorldTypes.h"
  29. #include "PxPhysics.h"
  30. #include "PxScene.h"
  31. #include "PxRigidActor.h"
  32. #include "PxCooking.h"
  33. using physx::PxRigidActor;
  34. namespace crown
  35. {
  36. #define MAX_PHYSX_VERTICES 256
  37. struct PhysicsResource;
  38. struct PhysicsConfigResource;
  39. struct Unit;
  40. struct SceneGraph;
  41. /// Represents a rigid body.
  42. ///
  43. /// @ingroup Physics
  44. struct Actor
  45. {
  46. Actor(PhysicsWorld& pw, const PhysicsResource* res, uint32_t actor_idx, SceneGraph& sg, int32_t node, UnitId unit_id);
  47. ~Actor();
  48. /// Sets the world position of the actor.
  49. Vector3 world_position() const;
  50. /// Sets the world rotation of the actor.
  51. Quaternion world_rotation() const;
  52. /// Sets the world pose of the actor.
  53. Matrix4x4 world_pose() const;
  54. /// Teleports the actor to the given world position.
  55. void teleport_world_position(const Vector3& p);
  56. /// Teleports the actor to the given world rotation.
  57. void teleport_world_rotation(const Quaternion& r);
  58. /// Teleports the actor to the given world pose.
  59. void teleport_world_pose(const Matrix4x4& m);
  60. /// Returns the center of mass of the actor.
  61. Vector3 center_of_mass() const;
  62. /// Makes the actor subject to gravity
  63. void enable_gravity();
  64. /// Makes the actor unsubject to gravity
  65. void disable_gravity();
  66. void enable_collision();
  67. void disable_collision();
  68. /// Sets the collision filter of the actor.
  69. void set_collision_filter(const char* filter);
  70. void set_collision_filter(StringId32 filter);
  71. /// Sets whether the actor is kinematic or not.
  72. /// @note This call has no effect on static actors.
  73. void set_kinematic(bool kinematic);
  74. /// Moves the actor to @a pos
  75. /// @note This call only affects nonkinematic actors.
  76. void move(const Vector3& pos);
  77. /// Returns whether the actor is static.
  78. bool is_static() const;
  79. /// Returns whether the actor is dynamic.
  80. bool is_dynamic() const;
  81. /// Returns whether the actor is kinematic (keyframed).
  82. bool is_kinematic() const;
  83. /// Returns whether the actor is nonkinematic (i.e. dynamic and not kinematic).
  84. bool is_nonkinematic() const;
  85. /// Returns the linear damping of the actor.
  86. float linear_damping() const;
  87. /// Sets the linear damping of the actor.
  88. void set_linear_damping(float rate);
  89. /// Returns the angular damping of the actor.
  90. float angular_damping() const;
  91. /// Sets the angular damping of the actor.
  92. void set_angular_damping(float rate);
  93. /// Returns the linear velocity of the actor.
  94. Vector3 linear_velocity() const;
  95. /// Sets the linear velocity of the actor.
  96. /// @note This call only affects nonkinematic actors.
  97. void set_linear_velocity(const Vector3& vel);
  98. /// Returns the angular velocity of the actor.
  99. Vector3 angular_velocity() const;
  100. /// Sets the angular velocity of the actor.
  101. /// @note This call only affects nonkinematic actors.
  102. void set_angular_velocity(const Vector3& vel);
  103. /// Adds a linear impulse (acting along the center of mass) to the actor.
  104. /// @note This call only affects nonkinematic actors.
  105. void add_impulse(const Vector3& impulse);
  106. /// Adds a linear impulse (acting along the world position @a pos) to the actor.
  107. /// @note This call only affects nonkinematic actors.
  108. void add_impulse_at(const Vector3& impulse, const Vector3& pos);
  109. /// Adds a torque impulse to the actor.
  110. void add_torque_impulse(const Vector3& i);
  111. /// Pushes the actor as if it was hit by a point object with the given @a mass
  112. /// travelling at the given @a velocity.
  113. /// @note This call only affects nonkinematic actors.
  114. void push(const Vector3& vel, float mass);
  115. /// Like push() but applies the force at the world position @a pos.
  116. /// @note This call only affects nonkinematic actors.
  117. void push_at(const Vector3& vel, float mass, const Vector3& pos);
  118. /// Returns whether the actor is sleeping.
  119. bool is_sleeping();
  120. /// Wakes the actor up.
  121. void wake_up();
  122. /// Returns the id of the unit that owns the actor.
  123. UnitId unit_id() const;
  124. /// Returns the unit that owns the actor.
  125. Unit* unit();
  126. const PhysicsResource* resource() const { return m_resource; }
  127. private:
  128. void create_objects();
  129. void destroy_objects();
  130. void update(const Matrix4x4& pose);
  131. public:
  132. PhysicsWorld& m_world;
  133. const PhysicsResource* m_resource;
  134. uint32_t m_index;
  135. PxRigidActor* m_actor;
  136. SceneGraph& m_scene_graph;
  137. int32_t m_node;
  138. UnitId m_unit;
  139. private:
  140. friend class PhysicsWorld;
  141. };
  142. } // namespace crown