Actor.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. #include "Actor.h"
  24. #include "Vector3.h"
  25. #include "Quaternion.h"
  26. #include "Matrix4x4.h"
  27. #include "Unit.h"
  28. #include "PhysicsGraph.h"
  29. #include "Device.h"
  30. #include "Physics.h"
  31. #include "Log.h"
  32. #include "SceneGraph.h"
  33. #include "PxPhysicsAPI.h"
  34. namespace crown
  35. {
  36. //-----------------------------------------------------------------------------
  37. Actor::Actor(PhysicsGraph& pg, int32_t sg_node, ActorType::Enum type, const Vector3& pos, const Quaternion& rot)
  38. : m_physics_graph(pg)
  39. , m_sg_node(sg_node)
  40. , m_type(type)
  41. {
  42. Matrix4x4 m(rot, pos);
  43. physx::PxMat44 pose((physx::PxReal*)(m.to_float_ptr()));
  44. switch (type)
  45. {
  46. case ActorType::STATIC:
  47. {
  48. m_actor = device()->physx()->createRigidStatic(physx::PxTransform(pose));
  49. break;
  50. }
  51. case ActorType::DYNAMIC:
  52. {
  53. m_actor = device()->physx()->createRigidDynamic(physx::PxTransform(pose));
  54. Log::d("Created dynamic");
  55. break;
  56. }
  57. default:
  58. {
  59. CE_FATAL("Unable to recognize actor type");
  60. }
  61. }
  62. m_mat = device()->physx()->createMaterial(0.5f, 0.5f, 0.5f);
  63. // FIXME
  64. create_sphere(Vector3(0, 0, 0), 2.0f);
  65. }
  66. //-----------------------------------------------------------------------------
  67. Actor::~Actor()
  68. {
  69. if (m_actor)
  70. {
  71. m_actor->release();
  72. }
  73. }
  74. //-----------------------------------------------------------------------------
  75. void Actor::create_sphere(const Vector3& position, float radius)
  76. {
  77. Shape shape(m_actor->createShape(physx::PxSphereGeometry(radius), *m_mat));
  78. m_physics_graph.create(m_sg_node, shape);
  79. }
  80. //-----------------------------------------------------------------------------
  81. void Actor::create_box(const Vector3& position, float a, float b, float c)
  82. {
  83. Shape shape(m_actor->createShape(physx::PxBoxGeometry(a, b, c), *m_mat));
  84. m_physics_graph.create(m_sg_node, shape);
  85. }
  86. //-----------------------------------------------------------------------------
  87. void Actor::create_plane(const Vector3& position, const Vector3& normal)
  88. {
  89. // TODO
  90. }
  91. //-----------------------------------------------------------------------------
  92. void Actor::enable_gravity()
  93. {
  94. m_actor->setActorFlag(physx::PxActorFlag::eDISABLE_GRAVITY, false);
  95. }
  96. //-----------------------------------------------------------------------------
  97. void Actor::disable_gravity()
  98. {
  99. m_actor->setActorFlag(physx::PxActorFlag::eDISABLE_GRAVITY, true);
  100. }
  101. //-----------------------------------------------------------------------------
  102. bool Actor::is_static() const
  103. {
  104. return m_type == ActorType::STATIC;
  105. }
  106. //-----------------------------------------------------------------------------
  107. bool Actor::is_dynamic() const
  108. {
  109. return m_type == ActorType::DYNAMIC;
  110. }
  111. //-----------------------------------------------------------------------------
  112. float Actor::linear_damping() const
  113. {
  114. return ((physx::PxRigidDynamic*)m_actor)->getLinearDamping();
  115. }
  116. //-----------------------------------------------------------------------------
  117. void Actor::set_linear_damping(float rate)
  118. {
  119. ((physx::PxRigidDynamic*)m_actor)->setLinearDamping(rate);
  120. }
  121. //-----------------------------------------------------------------------------
  122. float Actor::angular_damping() const
  123. {
  124. return ((physx::PxRigidDynamic*)m_actor)->getAngularDamping();
  125. }
  126. //-----------------------------------------------------------------------------
  127. void Actor::set_angular_damping(float rate)
  128. {
  129. ((physx::PxRigidDynamic*)m_actor)->setAngularDamping(rate);
  130. }
  131. //-----------------------------------------------------------------------------
  132. Vector3 Actor::linear_velocity() const
  133. {
  134. physx::PxVec3 vel = ((physx::PxRigidBody*)m_actor)->getLinearVelocity();
  135. Vector3 velocity(vel.x, vel.y, vel.z);
  136. return velocity;
  137. }
  138. //-----------------------------------------------------------------------------
  139. void Actor::set_linear_velocity(const Vector3& vel)
  140. {
  141. physx::PxVec3 velocity(vel.x, vel.y, vel.z);
  142. ((physx::PxRigidBody*)m_actor)->setLinearVelocity(velocity);
  143. }
  144. //-----------------------------------------------------------------------------
  145. Vector3 Actor::angular_velocity() const
  146. {
  147. physx::PxVec3 vel = ((physx::PxRigidBody*)m_actor)->getAngularVelocity();
  148. Vector3 velocity(vel.x, vel.y, vel.z);
  149. return velocity;
  150. }
  151. //-----------------------------------------------------------------------------
  152. void Actor::set_angular_velocity(const Vector3& vel)
  153. {
  154. physx::PxVec3 velocity(vel.x, vel.y, vel.z);
  155. ((physx::PxRigidBody*)m_actor)->setAngularVelocity(velocity);
  156. }
  157. //-----------------------------------------------------------------------------
  158. bool Actor::is_sleeping()
  159. {
  160. return ((physx::PxRigidDynamic*)m_actor)->isSleeping();
  161. }
  162. //-----------------------------------------------------------------------------
  163. void Actor::wake_up()
  164. {
  165. ((physx::PxRigidDynamic*)m_actor)->wakeUp();
  166. }
  167. } // namespace crown