Actor.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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::PxForceMode;
  51. using physx::PxD6Joint;
  52. using physx::PxD6JointCreate;
  53. using physx::PxD6Axis;
  54. using physx::PxD6Motion;
  55. namespace crown
  56. {
  57. //-----------------------------------------------------------------------------
  58. Actor::Actor(const PhysicsResource* res, uint32_t i, PxPhysics* physics, PxScene* scene, SceneGraph& sg, int32_t node, const Vector3& pos, const Quaternion& rot)
  59. : m_resource(res)
  60. , m_index(i)
  61. , m_scene(scene)
  62. , m_scene_graph(sg)
  63. , m_node(node)
  64. {
  65. const PhysicsActor& a = m_resource->actor(m_index);
  66. // Creates actor
  67. Matrix4x4 m = sg.world_pose(node);
  68. PxMat44 pose((PxReal*)(m.to_float_ptr()));
  69. switch (a.type)
  70. {
  71. case ActorType::STATIC:
  72. {
  73. m_actor = physics->createRigidStatic(PxTransform(pose));
  74. break;
  75. }
  76. case ActorType::DYNAMIC_PHYSICAL:
  77. case ActorType::DYNAMIC_KINEMATIC:
  78. {
  79. m_actor = physics->createRigidDynamic(PxTransform(pose));
  80. if (a.type == ActorType::DYNAMIC_KINEMATIC)
  81. {
  82. static_cast<PxRigidDynamic*>(m_actor)->setRigidDynamicFlag(PxRigidDynamicFlag::eKINEMATIC, true);
  83. }
  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_group = a.group;
  133. m_mask = a.mask;
  134. // FIXME collisions works only if enable_collision() is called here first
  135. // collision enabled by default
  136. enable_collision();
  137. m_actor->setActorFlag(PxActorFlag::eSEND_SLEEP_NOTIFIES, true);
  138. m_scene->addActor(*m_actor);
  139. }
  140. //-----------------------------------------------------------------------------
  141. Actor::~Actor()
  142. {
  143. if (m_actor)
  144. {
  145. m_scene->removeActor(*m_actor);
  146. m_actor->release();
  147. }
  148. }
  149. //-----------------------------------------------------------------------------
  150. void Actor::create_sphere(const Vector3& position, float radius)
  151. {
  152. m_actor->createShape(PxSphereGeometry(radius), *m_mat);
  153. }
  154. //-----------------------------------------------------------------------------
  155. void Actor::create_box(const Vector3& position, float half_x, float half_y, float half_z)
  156. {
  157. m_actor->createShape(PxBoxGeometry(half_x, half_y, half_z), *m_mat);
  158. }
  159. //-----------------------------------------------------------------------------
  160. void Actor::create_plane(const Vector3& position, const Vector3& /*normal*/)
  161. {
  162. m_actor->createShape(PxPlaneGeometry(), *m_mat);
  163. }
  164. //-----------------------------------------------------------------------------
  165. void Actor::enable_gravity()
  166. {
  167. m_actor->setActorFlag(PxActorFlag::eDISABLE_GRAVITY, false);
  168. }
  169. //-----------------------------------------------------------------------------
  170. void Actor::disable_gravity()
  171. {
  172. m_actor->setActorFlag(PxActorFlag::eDISABLE_GRAVITY, true);
  173. }
  174. //-----------------------------------------------------------------------------
  175. void Actor::enable_collision()
  176. {
  177. PxFilterData filter_data;
  178. filter_data.word0 = (PxU32) m_group;
  179. filter_data.word1 = (PxU32) m_mask;
  180. const PxU32 num_shapes = m_actor->getNbShapes();
  181. PxShape** shapes = (PxShape**) default_allocator().allocate((sizeof(PxShape*) * num_shapes));
  182. m_actor->getShapes(shapes, num_shapes);
  183. for(PxU32 i = 0; i < num_shapes; i++)
  184. {
  185. PxShape* shape = shapes[i];
  186. shape->setSimulationFilterData(filter_data);
  187. }
  188. default_allocator().deallocate(shapes);
  189. }
  190. //-----------------------------------------------------------------------------
  191. void Actor::disable_collision()
  192. {
  193. PxFilterData filter_data;
  194. filter_data.word0 = (PxU32) CollisionGroup::GROUP_0;
  195. filter_data.word1 = (PxU32) CollisionGroup::GROUP_0;
  196. const PxU32 num_shapes = m_actor->getNbShapes();
  197. PxShape** shapes = (PxShape**) default_allocator().allocate((sizeof(PxShape*) * num_shapes));
  198. m_actor->getShapes(shapes, num_shapes);
  199. for(PxU32 i = 0; i < num_shapes; i++)
  200. {
  201. PxShape* shape = shapes[i];
  202. shape->setSimulationFilterData(filter_data);
  203. }
  204. default_allocator().deallocate(shapes);
  205. }
  206. //-----------------------------------------------------------------------------
  207. void Actor::set_kinematic()
  208. {
  209. static_cast<PxRigidDynamic*>(m_actor)->setRigidDynamicFlag(PxRigidDynamicFlag::eKINEMATIC, true);
  210. }
  211. //-----------------------------------------------------------------------------
  212. void Actor::clear_kinematic()
  213. {
  214. static_cast<PxRigidDynamic*>(m_actor)->setRigidDynamicFlag(PxRigidDynamicFlag::eKINEMATIC, false);
  215. }
  216. //-----------------------------------------------------------------------------
  217. bool Actor::is_static() const
  218. {
  219. const PhysicsActor& a = m_resource->actor(m_index);
  220. return a.type == ActorType::STATIC;
  221. }
  222. //-----------------------------------------------------------------------------
  223. bool Actor::is_dynamic() const
  224. {
  225. const PhysicsActor& a = m_resource->actor(m_index);
  226. return a.type == ActorType::DYNAMIC_PHYSICAL || a.type == ActorType::DYNAMIC_KINEMATIC;
  227. }
  228. //-----------------------------------------------------------------------------
  229. bool Actor::is_kinematic() const
  230. {
  231. const PhysicsActor& a = m_resource->actor(m_index);
  232. return a.type == ActorType::DYNAMIC_KINEMATIC;
  233. }
  234. //-----------------------------------------------------------------------------
  235. bool Actor::is_physical() const
  236. {
  237. const PhysicsActor& a = m_resource->actor(m_index);
  238. return a.type == ActorType::DYNAMIC_PHYSICAL;
  239. }
  240. //-----------------------------------------------------------------------------
  241. float Actor::linear_damping() const
  242. {
  243. return ((PxRigidDynamic*)m_actor)->getLinearDamping();
  244. }
  245. //-----------------------------------------------------------------------------
  246. void Actor::set_linear_damping(float rate)
  247. {
  248. ((PxRigidDynamic*)m_actor)->setLinearDamping(rate);
  249. }
  250. //-----------------------------------------------------------------------------
  251. float Actor::angular_damping() const
  252. {
  253. return ((PxRigidDynamic*)m_actor)->getAngularDamping();
  254. }
  255. //-----------------------------------------------------------------------------
  256. void Actor::set_angular_damping(float rate)
  257. {
  258. ((PxRigidDynamic*)m_actor)->setAngularDamping(rate);
  259. }
  260. //-----------------------------------------------------------------------------
  261. Vector3 Actor::linear_velocity() const
  262. {
  263. PxVec3 vel = ((PxRigidBody*)m_actor)->getLinearVelocity();
  264. Vector3 velocity(vel.x, vel.y, vel.z);
  265. return velocity;
  266. }
  267. //-----------------------------------------------------------------------------
  268. void Actor::set_linear_velocity(const Vector3& vel)
  269. {
  270. PxVec3 velocity(vel.x, vel.y, vel.z);
  271. ((PxRigidBody*)m_actor)->setLinearVelocity(velocity);
  272. }
  273. //-----------------------------------------------------------------------------
  274. Vector3 Actor::angular_velocity() const
  275. {
  276. PxVec3 vel = ((PxRigidBody*)m_actor)->getAngularVelocity();
  277. Vector3 velocity(vel.x, vel.y, vel.z);
  278. return velocity;
  279. }
  280. //-----------------------------------------------------------------------------
  281. void Actor::set_angular_velocity(const Vector3& vel)
  282. {
  283. PxVec3 velocity(vel.x, vel.y, vel.z);
  284. ((PxRigidBody*)m_actor)->setAngularVelocity(velocity);
  285. }
  286. //-----------------------------------------------------------------------------
  287. void Actor::add_impulse(const Vector3& impulse)
  288. {
  289. Vector3 p = m_scene_graph.world_pose(m_node).translation();
  290. PxRigidBodyExt::addForceAtPos(*static_cast<PxRigidDynamic*>(m_actor),
  291. PxVec3(impulse.x, impulse.y, impulse.z),
  292. PxVec3(p.x, p.y, p.z),
  293. PxForceMode::eIMPULSE,
  294. true);
  295. }
  296. //-----------------------------------------------------------------------------
  297. void Actor::add_impulse_at(const Vector3& impulse, const Vector3& pos)
  298. {
  299. PxRigidBodyExt::addForceAtLocalPos(*static_cast<PxRigidDynamic*>(m_actor),
  300. PxVec3(impulse.x, impulse.y, impulse.z),
  301. PxVec3(pos.x, pos.y, pos.z),
  302. PxForceMode::eIMPULSE,
  303. true);
  304. }
  305. //-----------------------------------------------------------------------------
  306. void Actor::push(const Vector3& vel, const float mass)
  307. {
  308. // FIXME FIXME FIXME
  309. Vector3 p = m_scene_graph.world_pose(m_node).translation();
  310. Vector3 mq(vel.x * mass, vel.y * mass, vel.z * mass);
  311. Vector3 f(mq.x / 0.017, mq.y / 0.017, mq.z / 0.017);
  312. PxRigidBodyExt::addForceAtPos(*static_cast<PxRigidDynamic*>(m_actor),
  313. PxVec3(f.x, f.y, f.z),
  314. PxVec3(p.x, p.y, p.z));
  315. }
  316. //-----------------------------------------------------------------------------
  317. bool Actor::is_sleeping()
  318. {
  319. return ((PxRigidDynamic*)m_actor)->isSleeping();
  320. }
  321. //-----------------------------------------------------------------------------
  322. void Actor::wake_up()
  323. {
  324. ((PxRigidDynamic*)m_actor)->wakeUp();
  325. }
  326. //-----------------------------------------------------------------------------
  327. StringId32 Actor::name()
  328. {
  329. const PhysicsActor& a = m_resource->actor(m_index);
  330. return a.name;
  331. }
  332. //-----------------------------------------------------------------------------
  333. void Actor::update_pose()
  334. {
  335. // Read world pose
  336. Matrix4x4 wp = m_scene_graph.world_pose(m_node);
  337. const PxMat44 pose((PxReal*) (wp.to_float_ptr()));
  338. const PxTransform world_transform(pose);
  339. const PhysicsActor& a = m_resource->actor(m_index);
  340. switch (a.type)
  341. {
  342. case ActorType::STATIC:
  343. {
  344. // m_actor->setGlobalPose(world_transform);
  345. }
  346. case ActorType::DYNAMIC_PHYSICAL:
  347. {
  348. break;
  349. }
  350. case ActorType::DYNAMIC_KINEMATIC:
  351. {
  352. static_cast<PxRigidDynamic*>(m_actor)->setKinematicTarget(world_transform);
  353. break;
  354. }
  355. default: break;
  356. }
  357. }
  358. //-----------------------------------------------------------------------------
  359. void Actor::update(const Matrix4x4& pose)
  360. {
  361. const PhysicsActor& a = m_resource->actor(m_index);
  362. if (a.type == ActorType::DYNAMIC_PHYSICAL)
  363. {
  364. m_scene_graph.set_world_pose(m_node, pose);
  365. }
  366. }
  367. } // namespace crown