Actor.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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 "Device.h"
  29. #include "Physics.h"
  30. #include "Log.h"
  31. #include "PhysicsResource.h"
  32. #include "SceneGraph.h"
  33. #include "PxPhysicsAPI.h"
  34. using physx::PxRigidDynamicFlag;
  35. using physx::PxMat44;
  36. using physx::PxTransform;
  37. using physx::PxActorFlag;
  38. using physx::PxVec3;
  39. using physx::PxReal;
  40. using physx::PxRigidBody;
  41. using physx::PxRigidDynamic;
  42. using physx::PxRigidStatic;
  43. using physx::PxPlaneGeometry;
  44. using physx::PxSphereGeometry;
  45. using physx::PxBoxGeometry;
  46. using physx::PxRigidBodyExt;
  47. using physx::PxD6Joint;
  48. using physx::PxD6JointCreate;
  49. using physx::PxD6Axis;
  50. using physx::PxD6Motion;
  51. namespace crown
  52. {
  53. //-----------------------------------------------------------------------------
  54. Actor::Actor(const PhysicsResource* res, uint32_t i, PxScene* scene, SceneGraph& sg, int32_t node, const Vector3& pos, const Quaternion& rot)
  55. : m_resource(res)
  56. , m_index(i)
  57. , m_scene(scene)
  58. , m_scene_graph(sg)
  59. , m_node(node)
  60. {
  61. const PhysicsActor& a = m_resource->actor(m_index);
  62. // Creates actor
  63. Matrix4x4 m = sg.world_pose(node);
  64. PxMat44 pose((PxReal*)(m.to_float_ptr()));
  65. switch (a.type)
  66. {
  67. case ActorType::STATIC:
  68. {
  69. m_actor = device()->physx()->createRigidStatic(PxTransform(pose));
  70. break;
  71. }
  72. case ActorType::DYNAMIC_PHYSICAL:
  73. case ActorType::DYNAMIC_KINEMATIC:
  74. {
  75. m_actor = device()->physx()->createRigidDynamic(PxTransform(pose));
  76. if (a.type == ActorType::DYNAMIC_KINEMATIC)
  77. {
  78. static_cast<PxRigidDynamic*>(m_actor)->setRigidDynamicFlag(PxRigidDynamicFlag::eKINEMATIC, true);
  79. }
  80. break;
  81. }
  82. default:
  83. {
  84. CE_FATAL("Oops, unknown actor type");
  85. break;
  86. }
  87. }
  88. m_actor->userData = this;
  89. m_mat = device()->physx()->createMaterial(0.5f, 0.5f, 1.0f);
  90. // Creates shapes
  91. uint32_t index = m_resource->shape_index(m_index);
  92. for (uint32_t i = 0; i < a.num_shapes; i++)
  93. {
  94. PhysicsShape shape = m_resource->shape(index);
  95. Vector3 pos = sg.world_position(node);
  96. switch(shape.type)
  97. {
  98. case PhysicsShapeType::SPHERE:
  99. {
  100. create_sphere(pos, shape.x);
  101. break;
  102. }
  103. case PhysicsShapeType::BOX:
  104. {
  105. create_box(pos, shape.x, shape.y, shape.z);
  106. break;
  107. }
  108. case PhysicsShapeType::PLANE:
  109. {
  110. create_plane(pos, Vector3(shape.x, shape.y, shape.z));
  111. break;
  112. }
  113. default:
  114. {
  115. CE_FATAL("Oops, unknown shape type");
  116. }
  117. }
  118. index++;
  119. }
  120. if (a.type == ActorType::DYNAMIC_PHYSICAL || a.type == ActorType::DYNAMIC_KINEMATIC)
  121. {
  122. PxRigidBodyExt::setMassAndUpdateInertia(*static_cast<PxRigidDynamic*>(m_actor), 500.0f);
  123. PxD6Joint* joint = PxD6JointCreate(*device()->physx(), m_actor, PxTransform(pose), NULL, PxTransform(pose));
  124. joint->setMotion(PxD6Axis::eX, PxD6Motion::eFREE);
  125. joint->setMotion(PxD6Axis::eY, PxD6Motion::eFREE);
  126. //joint->setMotion(PxD6Axis::eZ, PxD6Motion::eFREE);
  127. //joint->setMotion(PxD6Axis::eSWING1, PxD6Motion::eFREE);
  128. joint->setMotion(PxD6Axis::eSWING2, PxD6Motion::eFREE);
  129. }
  130. m_scene->addActor(*m_actor);
  131. }
  132. //-----------------------------------------------------------------------------
  133. Actor::~Actor()
  134. {
  135. if (m_actor)
  136. {
  137. m_scene->removeActor(*m_actor);
  138. m_actor->release();
  139. }
  140. }
  141. //-----------------------------------------------------------------------------
  142. void Actor::create_sphere(const Vector3& position, float radius)
  143. {
  144. m_actor->createShape(PxSphereGeometry(radius), *m_mat);
  145. }
  146. //-----------------------------------------------------------------------------
  147. void Actor::create_box(const Vector3& position, float a, float b, float c)
  148. {
  149. m_actor->createShape(PxBoxGeometry(a, b, c), *m_mat);
  150. }
  151. //-----------------------------------------------------------------------------
  152. void Actor::create_plane(const Vector3& position, const Vector3& /*normal*/)
  153. {
  154. Log::i("CREATE PLANE");
  155. m_actor->createShape(PxPlaneGeometry(), *m_mat);
  156. }
  157. //-----------------------------------------------------------------------------
  158. void Actor::enable_gravity()
  159. {
  160. m_actor->setActorFlag(PxActorFlag::eDISABLE_GRAVITY, false);
  161. }
  162. //-----------------------------------------------------------------------------
  163. void Actor::disable_gravity()
  164. {
  165. m_actor->setActorFlag(PxActorFlag::eDISABLE_GRAVITY, true);
  166. }
  167. //-----------------------------------------------------------------------------
  168. bool Actor::is_static() const
  169. {
  170. const PhysicsActor& a = m_resource->actor(m_index);
  171. return a.type == ActorType::STATIC;
  172. }
  173. //-----------------------------------------------------------------------------
  174. bool Actor::is_dynamic() const
  175. {
  176. const PhysicsActor& a = m_resource->actor(m_index);
  177. return a.type == ActorType::DYNAMIC_PHYSICAL || a.type == ActorType::DYNAMIC_KINEMATIC;
  178. }
  179. //-----------------------------------------------------------------------------
  180. bool Actor::is_kinematic() const
  181. {
  182. const PhysicsActor& a = m_resource->actor(m_index);
  183. return a.type == ActorType::DYNAMIC_KINEMATIC;
  184. }
  185. //-----------------------------------------------------------------------------
  186. bool Actor::is_physical() const
  187. {
  188. const PhysicsActor& a = m_resource->actor(m_index);
  189. return a.type == ActorType::DYNAMIC_PHYSICAL;
  190. }
  191. //-----------------------------------------------------------------------------
  192. float Actor::linear_damping() const
  193. {
  194. return ((PxRigidDynamic*)m_actor)->getLinearDamping();
  195. }
  196. //-----------------------------------------------------------------------------
  197. void Actor::set_linear_damping(float rate)
  198. {
  199. ((PxRigidDynamic*)m_actor)->setLinearDamping(rate);
  200. }
  201. //-----------------------------------------------------------------------------
  202. float Actor::angular_damping() const
  203. {
  204. return ((PxRigidDynamic*)m_actor)->getAngularDamping();
  205. }
  206. //-----------------------------------------------------------------------------
  207. void Actor::set_angular_damping(float rate)
  208. {
  209. ((PxRigidDynamic*)m_actor)->setAngularDamping(rate);
  210. }
  211. //-----------------------------------------------------------------------------
  212. Vector3 Actor::linear_velocity() const
  213. {
  214. PxVec3 vel = ((PxRigidBody*)m_actor)->getLinearVelocity();
  215. Vector3 velocity(vel.x, vel.y, vel.z);
  216. return velocity;
  217. }
  218. //-----------------------------------------------------------------------------
  219. void Actor::set_linear_velocity(const Vector3& vel)
  220. {
  221. PxVec3 velocity(vel.x, vel.y, vel.z);
  222. ((PxRigidBody*)m_actor)->setLinearVelocity(velocity);
  223. }
  224. //-----------------------------------------------------------------------------
  225. Vector3 Actor::angular_velocity() const
  226. {
  227. PxVec3 vel = ((PxRigidBody*)m_actor)->getAngularVelocity();
  228. Vector3 velocity(vel.x, vel.y, vel.z);
  229. return velocity;
  230. }
  231. //-----------------------------------------------------------------------------
  232. void Actor::set_angular_velocity(const Vector3& vel)
  233. {
  234. PxVec3 velocity(vel.x, vel.y, vel.z);
  235. ((PxRigidBody*)m_actor)->setAngularVelocity(velocity);
  236. }
  237. //-----------------------------------------------------------------------------
  238. bool Actor::is_sleeping()
  239. {
  240. return ((PxRigidDynamic*)m_actor)->isSleeping();
  241. }
  242. //-----------------------------------------------------------------------------
  243. void Actor::wake_up()
  244. {
  245. ((PxRigidDynamic*)m_actor)->wakeUp();
  246. }
  247. //-----------------------------------------------------------------------------
  248. void Actor::update_pose()
  249. {
  250. // Read world pose
  251. Matrix4x4 wp = m_scene_graph.world_pose(m_node);
  252. const PxMat44 pose((PxReal*) (wp.to_float_ptr()));
  253. const PxTransform world_transform(pose);
  254. const PhysicsActor& a = m_resource->actor(m_index);
  255. switch (a.type)
  256. {
  257. case ActorType::STATIC:
  258. {
  259. // m_actor->setGlobalPose(world_transform);
  260. }
  261. case ActorType::DYNAMIC_PHYSICAL:
  262. {
  263. break;
  264. }
  265. case ActorType::DYNAMIC_KINEMATIC:
  266. {
  267. static_cast<PxRigidDynamic*>(m_actor)->setKinematicTarget(world_transform);
  268. break;
  269. }
  270. default: break;
  271. }
  272. }
  273. //-----------------------------------------------------------------------------
  274. void Actor::update(const Matrix4x4& pose)
  275. {
  276. const PhysicsActor& a = m_resource->actor(m_index);
  277. if (a.type == ActorType::DYNAMIC_PHYSICAL)
  278. {
  279. m_scene_graph.set_world_pose(m_node, pose);
  280. }
  281. }
  282. } // namespace crown