Actor.cpp 9.1 KB

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