actor.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Copyright (c) 2012-2014 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/taylor001/crown/blob/master/LICENSE
  4. */
  5. #pragma once
  6. #include "types.h"
  7. #include "math_types.h"
  8. #include "physics_types.h"
  9. #include "id_array.h"
  10. #include "world_types.h"
  11. #include "resource_types.h"
  12. #include "PxPhysics.h"
  13. #include "PxScene.h"
  14. #include "PxRigidActor.h"
  15. #include "PxCooking.h"
  16. using physx::PxRigidActor;
  17. namespace crown
  18. {
  19. #define MAX_PHYSX_VERTICES 256
  20. struct Unit;
  21. struct SceneGraph;
  22. /// Represents a rigid body.
  23. ///
  24. /// @ingroup Physics
  25. struct Actor
  26. {
  27. Actor(PhysicsWorld& pw, const PhysicsResource* res, uint32_t actor_idx, SceneGraph& sg, int32_t node, UnitId unit_id);
  28. ~Actor();
  29. /// Returns the world position of the actor.
  30. Vector3 world_position() const;
  31. /// Returns the world rotation of the actor.
  32. Quaternion world_rotation() const;
  33. /// Returns the world pose of the actor.
  34. Matrix4x4 world_pose() const;
  35. /// Teleports the actor to the given world position.
  36. void teleport_world_position(const Vector3& p);
  37. /// Teleports the actor to the given world rotation.
  38. void teleport_world_rotation(const Quaternion& r);
  39. /// Teleports the actor to the given world pose.
  40. void teleport_world_pose(const Matrix4x4& m);
  41. /// Returns the center of mass of the actor.
  42. Vector3 center_of_mass() const;
  43. /// Enables gravity for the actor.
  44. void enable_gravity();
  45. /// Disables gravity for the actor.
  46. void disable_gravity();
  47. /// Enables collision detection for the actor.
  48. void enable_collision();
  49. /// Disables collision detection for the actor.
  50. void disable_collision();
  51. /// Sets the collision filter of the actor.
  52. void set_collision_filter(const char* filter);
  53. void set_collision_filter(StringId32 filter);
  54. /// Sets whether the actor is kinematic or not.
  55. /// @note This call has no effect on static actors.
  56. void set_kinematic(bool kinematic);
  57. /// Moves the actor to @a pos
  58. /// @note This call only affects nonkinematic actors.
  59. void move(const Vector3& pos);
  60. /// Returns whether the actor is static.
  61. bool is_static() const;
  62. /// Returns whether the actor is dynamic.
  63. bool is_dynamic() const;
  64. /// Returns whether the actor is kinematic (keyframed).
  65. bool is_kinematic() const;
  66. /// Returns whether the actor is nonkinematic (i.e. dynamic and not kinematic).
  67. bool is_nonkinematic() const;
  68. /// Returns the linear damping of the actor.
  69. float linear_damping() const;
  70. /// Sets the linear damping of the actor.
  71. void set_linear_damping(float rate);
  72. /// Returns the angular damping of the actor.
  73. float angular_damping() const;
  74. /// Sets the angular damping of the actor.
  75. void set_angular_damping(float rate);
  76. /// Returns the linear velocity of the actor.
  77. Vector3 linear_velocity() const;
  78. /// Sets the linear velocity of the actor.
  79. /// @note This call only affects nonkinematic actors.
  80. void set_linear_velocity(const Vector3& vel);
  81. /// Returns the angular velocity of the actor.
  82. Vector3 angular_velocity() const;
  83. /// Sets the angular velocity of the actor.
  84. /// @note This call only affects nonkinematic actors.
  85. void set_angular_velocity(const Vector3& vel);
  86. /// Adds a linear impulse (acting along the center of mass) to the actor.
  87. /// @note This call only affects nonkinematic actors.
  88. void add_impulse(const Vector3& impulse);
  89. /// Adds a linear impulse (acting along the world position @a pos) to the actor.
  90. /// @note This call only affects nonkinematic actors.
  91. void add_impulse_at(const Vector3& impulse, const Vector3& pos);
  92. /// Adds a torque impulse to the actor.
  93. void add_torque_impulse(const Vector3& i);
  94. /// Pushes the actor as if it was hit by a point object with the given @a mass
  95. /// travelling at the given @a velocity.
  96. /// @note This call only affects nonkinematic actors.
  97. void push(const Vector3& vel, float mass);
  98. /// Like push() but applies the force at the world position @a pos.
  99. /// @note This call only affects nonkinematic actors.
  100. void push_at(const Vector3& vel, float mass, const Vector3& pos);
  101. /// Returns whether the actor is sleeping.
  102. bool is_sleeping();
  103. /// Wakes the actor up.
  104. void wake_up();
  105. /// Returns the id of the unit that owns the actor.
  106. UnitId unit_id() const;
  107. /// Returns the unit that owns the actor.
  108. Unit* unit();
  109. const PhysicsResource* resource() const { return m_resource; }
  110. private:
  111. void create_objects();
  112. void destroy_objects();
  113. void update(const Matrix4x4& pose);
  114. public:
  115. PhysicsWorld& m_world;
  116. const PhysicsResource* m_resource;
  117. uint32_t m_index;
  118. PxRigidActor* m_actor;
  119. SceneGraph& m_scene_graph;
  120. int32_t m_node;
  121. UnitId m_unit;
  122. private:
  123. friend class PhysicsWorld;
  124. };
  125. } // namespace crown