Actor.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  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 "Device.h"
  25. #include "Log.h"
  26. #include "Matrix4x4.h"
  27. #include "MeshResource.h"
  28. #include "PhysicsResource.h"
  29. #include "PxPhysicsAPI.h"
  30. #include "Quaternion.h"
  31. #include "ResourceManager.h"
  32. #include "SceneGraph.h"
  33. #include "Unit.h"
  34. #include "Vector3.h"
  35. #include "World.h"
  36. #include "PhysicsWorld.h"
  37. #include "Quaternion.h"
  38. #include "StringUtils.h"
  39. #include "PxCooking.h"
  40. #include "PxDefaultStreams.h"
  41. using physx::PxActorFlag;
  42. using physx::PxActorType;
  43. using physx::PxBoxGeometry;
  44. using physx::PxCapsuleGeometry;
  45. using physx::PxConvexFlag;
  46. using physx::PxConvexMesh;
  47. using physx::PxConvexMeshDesc;
  48. using physx::PxConvexMeshGeometry;
  49. using physx::PxD6Axis;
  50. using physx::PxD6Joint;
  51. using physx::PxD6JointCreate;
  52. using physx::PxD6Motion;
  53. using physx::PxDefaultMemoryInputData;
  54. using physx::PxDefaultMemoryOutputStream;
  55. using physx::PxFilterData;
  56. using physx::PxForceMode;
  57. using physx::PxMat44;
  58. using physx::PxPlaneGeometry;
  59. using physx::PxQuat;
  60. using physx::PxReal;
  61. using physx::PxRigidActor;
  62. using physx::PxRigidBody;
  63. using physx::PxRigidBodyExt;
  64. using physx::PxRigidBodyFlag;
  65. using physx::PxRigidDynamic;
  66. using physx::PxRigidDynamicFlag;
  67. using physx::PxRigidStatic;
  68. using physx::PxShape;
  69. using physx::PxShapeFlag;
  70. using physx::PxSphereGeometry;
  71. using physx::PxTransform;
  72. using physx::PxU16;
  73. using physx::PxU32;
  74. using physx::PxVec3;
  75. namespace crown
  76. {
  77. //-----------------------------------------------------------------------------
  78. Actor::Actor(PhysicsWorld& pw, const PhysicsResource* res, uint32_t actor_idx, SceneGraph& sg, int32_t node, UnitId unit_id)
  79. : m_world(pw)
  80. , m_resource(res)
  81. , m_index(actor_idx)
  82. , m_scene_graph(sg)
  83. , m_node(node)
  84. , m_unit(unit_id)
  85. {
  86. create_objects();
  87. }
  88. //-----------------------------------------------------------------------------
  89. Actor::~Actor()
  90. {
  91. destroy_objects();
  92. }
  93. //-----------------------------------------------------------------------------
  94. void Actor::create_objects()
  95. {
  96. const PhysicsActor& actor = m_resource->actor(m_index);
  97. PxScene* scene = m_world.physx_scene();
  98. PxPhysics* physics = m_world.physx_physics();
  99. const PhysicsConfigResource* config = m_world.resource();
  100. const PhysicsActor2& actor_class = config->actor(actor.actor_class);
  101. // Create rigid body
  102. const PxMat44 pose((PxReal*) matrix4x4::to_float_ptr(m_scene_graph.world_pose(m_node)));
  103. if (actor_class.flags & PhysicsActor2::DYNAMIC)
  104. {
  105. m_actor = physics->createRigidDynamic(PxTransform(pose));
  106. if (actor_class.flags & PhysicsActor2::KINEMATIC)
  107. {
  108. static_cast<PxRigidDynamic*>(m_actor)->setRigidDynamicFlag(PxRigidDynamicFlag::eKINEMATIC, true);
  109. }
  110. PxD6Joint* joint = PxD6JointCreate(*physics, m_actor, PxTransform(pose), NULL, PxTransform(pose));
  111. joint->setMotion(PxD6Axis::eX, PxD6Motion::eFREE);
  112. joint->setMotion(PxD6Axis::eY, PxD6Motion::eFREE);
  113. joint->setMotion(PxD6Axis::eSWING2, PxD6Motion::eFREE);
  114. }
  115. else
  116. {
  117. m_actor = physics->createRigidStatic(PxTransform(pose));
  118. }
  119. // Create shapes
  120. uint32_t shape_index = m_resource->shape_index(m_index);
  121. for (uint32_t i = 0; i < actor.num_shapes; i++)
  122. {
  123. const PhysicsShape& shape = m_resource->shape(shape_index);
  124. const PhysicsShape2& shape_class = config->shape(shape.shape_class);
  125. const PhysicsMaterial& material = config->material(shape.material);
  126. PxMaterial* mat = physics->createMaterial(material.static_friction, material.dynamic_friction, material.restitution);
  127. PxShape* px_shape = NULL;
  128. switch(shape.type)
  129. {
  130. case PhysicsShapeType::SPHERE:
  131. {
  132. px_shape = m_actor->createShape(PxSphereGeometry(shape.data_0), *mat);
  133. break;
  134. }
  135. case PhysicsShapeType::CAPSULE:
  136. {
  137. px_shape = m_actor->createShape(PxCapsuleGeometry(shape.data_0, shape.data_1), *mat);
  138. break;
  139. }
  140. case PhysicsShapeType::BOX:
  141. {
  142. px_shape = m_actor->createShape(PxBoxGeometry(shape.data_0, shape.data_1, shape.data_2), *mat);
  143. break;
  144. }
  145. case PhysicsShapeType::PLANE:
  146. {
  147. px_shape = m_actor->createShape(PxPlaneGeometry(), *mat);
  148. break;
  149. }
  150. case PhysicsShapeType::CONVEX_MESH:
  151. {
  152. MeshResource* resource = (MeshResource*) device()->resource_manager()->get(shape.resource);
  153. PxConvexMeshDesc convex_mesh_desc;
  154. convex_mesh_desc.points.count = resource->num_vertices();
  155. convex_mesh_desc.points.stride = sizeof(PxVec3);
  156. convex_mesh_desc.points.data = (PxVec3*) resource->vertices();
  157. convex_mesh_desc.triangles.count = resource->num_indices();
  158. convex_mesh_desc.triangles.stride = 3 * sizeof(PxU16);
  159. convex_mesh_desc.triangles.data = (PxU16*) resource->indices();
  160. convex_mesh_desc.flags = PxConvexFlag::eCOMPUTE_CONVEX;
  161. convex_mesh_desc.vertexLimit = MAX_PHYSX_VERTICES;
  162. PxDefaultMemoryOutputStream buf;
  163. if(!m_world.physx_cooking()->cookConvexMesh(convex_mesh_desc, buf))
  164. CE_FATAL("");
  165. PxDefaultMemoryInputData input(buf.getData(), buf.getSize());
  166. PxConvexMesh* convex_mesh = physics->createConvexMesh(input);
  167. px_shape = m_actor->createShape(PxConvexMeshGeometry(convex_mesh), *mat);
  168. break;
  169. }
  170. default:
  171. {
  172. CE_FATAL("Oops, unknown shape type");
  173. }
  174. }
  175. // Setup shape pose
  176. px_shape->setLocalPose(PxTransform(
  177. PxVec3(shape.position.x, shape.position.y, shape.position.z),
  178. PxQuat(shape.rotation.x, shape.rotation.y, shape.rotation.z, shape.rotation.w)));
  179. // Setup collision filters
  180. PxFilterData filter_data;
  181. filter_data.word0 = config->filter(shape_class.collision_filter).me;
  182. filter_data.word1 = config->filter(shape_class.collision_filter).mask;
  183. px_shape->setSimulationFilterData(filter_data);
  184. if (shape_class.trigger)
  185. {
  186. px_shape->setFlag(PxShapeFlag::eSIMULATION_SHAPE, false);
  187. px_shape->setFlag(PxShapeFlag::eTRIGGER_SHAPE, true);
  188. }
  189. shape_index++;
  190. }
  191. if (is_dynamic())
  192. {
  193. PxRigidBodyExt::updateMassAndInertia(*static_cast<PxRigidBody*>(m_actor), actor.mass);
  194. }
  195. m_actor->userData = this;
  196. scene->addActor(*m_actor);
  197. }
  198. //-----------------------------------------------------------------------------
  199. void Actor::destroy_objects()
  200. {
  201. if (m_actor)
  202. {
  203. m_world.physx_scene()->removeActor(*m_actor);
  204. m_actor->release();
  205. }
  206. }
  207. //-----------------------------------------------------------------------------
  208. Vector3 Actor::world_position() const
  209. {
  210. const PxTransform tr = m_actor->getGlobalPose();
  211. return Vector3(tr.p.x, tr.p.y, tr.p.z);
  212. }
  213. //-----------------------------------------------------------------------------
  214. Quaternion Actor::world_rotation() const
  215. {
  216. const PxTransform tr = m_actor->getGlobalPose();
  217. return Quaternion(tr.q.x, tr.q.y, tr.q.z, tr.q.w);
  218. }
  219. //-----------------------------------------------------------------------------
  220. Matrix4x4 Actor::world_pose() const
  221. {
  222. const PxTransform tr = m_actor->getGlobalPose();
  223. return Matrix4x4(Quaternion(tr.q.x, tr.q.y, tr.q.z, tr.q.w), Vector3(tr.p.x, tr.p.y, tr.p.z));
  224. }
  225. //-----------------------------------------------------------------------------
  226. void Actor::teleport_world_position(const Vector3& p)
  227. {
  228. PxTransform tr = m_actor->getGlobalPose();
  229. tr.p.x = p.x;
  230. tr.p.y = p.y;
  231. tr.p.z = p.z;
  232. m_actor->setGlobalPose(tr);
  233. }
  234. //-----------------------------------------------------------------------------
  235. void Actor::teleport_world_rotation(const Quaternion& r)
  236. {
  237. PxTransform tr = m_actor->getGlobalPose();
  238. tr.q.x = r.x;
  239. tr.q.y = r.y;
  240. tr.q.z = r.z;
  241. tr.q.w = r.w;
  242. m_actor->setGlobalPose(tr);
  243. }
  244. //-----------------------------------------------------------------------------
  245. void Actor::teleport_world_pose(const Matrix4x4& m)
  246. {
  247. using namespace matrix4x4;
  248. const PxVec3 x(m.x.x, m.x.y, m.x.z);
  249. const PxVec3 y(m.y.x, m.y.y, m.y.z);
  250. const PxVec3 z(m.z.x, m.z.y, m.z.z);
  251. const PxVec3 t(translation(m).x, translation(m).y, translation(m).z);
  252. m_actor->setGlobalPose(PxTransform(PxMat44(x, y, z, t)));
  253. }
  254. //-----------------------------------------------------------------------------
  255. Vector3 Actor::center_of_mass() const
  256. {
  257. if (is_static())
  258. return Vector3(0, 0, 0);
  259. const PxTransform tr = static_cast<PxRigidBody*>(m_actor)->getCMassLocalPose();
  260. return Vector3(tr.p.x, tr.p.y, tr.p.z);
  261. }
  262. //-----------------------------------------------------------------------------
  263. void Actor::enable_gravity()
  264. {
  265. m_actor->setActorFlag(PxActorFlag::eDISABLE_GRAVITY, false);
  266. }
  267. //-----------------------------------------------------------------------------
  268. void Actor::disable_gravity()
  269. {
  270. m_actor->setActorFlag(PxActorFlag::eDISABLE_GRAVITY, true);
  271. }
  272. //-----------------------------------------------------------------------------
  273. void Actor::enable_collision()
  274. {
  275. // const PxU32 num_shapes = m_actor->getNbShapes();
  276. // PxU32 idx = 0;
  277. // while (idx != num_shapes)
  278. // {
  279. // PxShape* shapes[8];
  280. // const PxU32 written = m_actor->getShapes(shapes, 8, idx);
  281. // for (PxU32 i = 0; i < written; i++)
  282. // {
  283. // PxFilterData fdata;
  284. // fdata.word0 = 0;
  285. // fdata.word1 = 0;
  286. // shapes[i]->setSimulationFilterData(fdata);
  287. // }
  288. // idx += written;
  289. // }
  290. }
  291. //-----------------------------------------------------------------------------
  292. void Actor::disable_collision()
  293. {
  294. }
  295. //-----------------------------------------------------------------------------
  296. void Actor::set_collision_filter(const char* name)
  297. {
  298. set_collision_filter(string::murmur2_32(name, string::strlen(name)));
  299. }
  300. //-----------------------------------------------------------------------------
  301. void Actor::set_collision_filter(StringId32 filter)
  302. {
  303. const PhysicsCollisionFilter& pcf = m_world.resource()->filter(filter);
  304. const PxU32 num_shapes = m_actor->getNbShapes();
  305. PxU32 idx = 0;
  306. while (idx != num_shapes)
  307. {
  308. PxShape* shapes[8];
  309. const PxU32 written = m_actor->getShapes(shapes, 8, idx);
  310. for (PxU32 i = 0; i < written; i++)
  311. {
  312. PxFilterData fdata;
  313. fdata.word0 = pcf.me;
  314. fdata.word1 = pcf.mask;
  315. shapes[i]->setSimulationFilterData(fdata);
  316. }
  317. idx += written;
  318. }
  319. }
  320. //-----------------------------------------------------------------------------
  321. void Actor::set_kinematic(bool kinematic)
  322. {
  323. if (is_static())
  324. return;
  325. static_cast<PxRigidBody*>(m_actor)->setRigidBodyFlag(PxRigidBodyFlag::eKINEMATIC, kinematic);
  326. }
  327. //-----------------------------------------------------------------------------
  328. void Actor::move(const Vector3& pos)
  329. {
  330. if (!is_kinematic())
  331. return;
  332. const PxVec3 position(pos.x, pos.y, pos.z);
  333. static_cast<PxRigidDynamic*>(m_actor)->setKinematicTarget(PxTransform(position));
  334. }
  335. //-----------------------------------------------------------------------------
  336. bool Actor::is_static() const
  337. {
  338. return m_actor->getType() & PxActorType::eRIGID_STATIC;
  339. }
  340. //-----------------------------------------------------------------------------
  341. bool Actor::is_dynamic() const
  342. {
  343. return m_actor->getType() & PxActorType::eRIGID_DYNAMIC;
  344. }
  345. //-----------------------------------------------------------------------------
  346. bool Actor::is_kinematic() const
  347. {
  348. if (!is_dynamic())
  349. return false;
  350. return static_cast<PxRigidDynamic*>(m_actor)->getRigidDynamicFlags() & PxRigidDynamicFlag::eKINEMATIC;
  351. }
  352. //-----------------------------------------------------------------------------
  353. bool Actor::is_nonkinematic() const
  354. {
  355. return is_dynamic() && !is_kinematic();
  356. }
  357. //-----------------------------------------------------------------------------
  358. float Actor::linear_damping() const
  359. {
  360. if (is_static())
  361. return 0;
  362. return static_cast<PxRigidDynamic*>(m_actor)->getLinearDamping();
  363. }
  364. //-----------------------------------------------------------------------------
  365. void Actor::set_linear_damping(float rate)
  366. {
  367. if (is_static())
  368. return;
  369. static_cast<PxRigidDynamic*>(m_actor)->setLinearDamping(rate);
  370. }
  371. //-----------------------------------------------------------------------------
  372. float Actor::angular_damping() const
  373. {
  374. if (is_static())
  375. return 0;
  376. return static_cast<PxRigidDynamic*>(m_actor)->getAngularDamping();
  377. }
  378. //-----------------------------------------------------------------------------
  379. void Actor::set_angular_damping(float rate)
  380. {
  381. if (is_static())
  382. return;
  383. static_cast<PxRigidDynamic*>(m_actor)->setAngularDamping(rate);
  384. }
  385. //-----------------------------------------------------------------------------
  386. Vector3 Actor::linear_velocity() const
  387. {
  388. if (is_static())
  389. return Vector3(0, 0, 0);
  390. const PxVec3 vel = static_cast<PxRigidBody*>(m_actor)->getLinearVelocity();
  391. return Vector3(vel.x, vel.y, vel.z);
  392. }
  393. //-----------------------------------------------------------------------------
  394. void Actor::set_linear_velocity(const Vector3& vel)
  395. {
  396. if (!is_nonkinematic())
  397. return;
  398. const PxVec3 velocity(vel.x, vel.y, vel.z);
  399. static_cast<PxRigidBody*>(m_actor)->setLinearVelocity(velocity);
  400. }
  401. //-----------------------------------------------------------------------------
  402. Vector3 Actor::angular_velocity() const
  403. {
  404. if (is_static())
  405. return Vector3(0, 0, 0);
  406. const PxVec3 vel = static_cast<PxRigidBody*>(m_actor)->getAngularVelocity();
  407. return Vector3(vel.x, vel.y, vel.z);
  408. }
  409. //-----------------------------------------------------------------------------
  410. void Actor::set_angular_velocity(const Vector3& vel)
  411. {
  412. if (!is_nonkinematic())
  413. return;
  414. const PxVec3 velocity(vel.x, vel.y, vel.z);
  415. static_cast<PxRigidBody*>(m_actor)->setAngularVelocity(velocity);
  416. }
  417. //-----------------------------------------------------------------------------
  418. void Actor::add_impulse(const Vector3& impulse)
  419. {
  420. if (!is_nonkinematic())
  421. return;
  422. static_cast<PxRigidDynamic*>(m_actor)->addForce(PxVec3(impulse.x, impulse.y, impulse.z), PxForceMode::eIMPULSE);
  423. }
  424. //-----------------------------------------------------------------------------
  425. void Actor::add_impulse_at(const Vector3& impulse, const Vector3& pos)
  426. {
  427. if (!is_nonkinematic())
  428. return;
  429. PxRigidBodyExt::addForceAtPos(*static_cast<PxRigidDynamic*>(m_actor),
  430. PxVec3(impulse.x, impulse.y, impulse.z),
  431. PxVec3(pos.x, pos.y, pos.z),
  432. PxForceMode::eIMPULSE);
  433. }
  434. //-----------------------------------------------------------------------------
  435. void Actor::add_torque_impulse(const Vector3& i)
  436. {
  437. if (!is_nonkinematic())
  438. return;
  439. static_cast<PxRigidBody*>(m_actor)->addTorque(PxVec3(i.x, i.y, i.z), PxForceMode::eIMPULSE);
  440. }
  441. //-----------------------------------------------------------------------------
  442. void Actor::push(const Vector3& vel, float mass)
  443. {
  444. add_impulse(vel * mass);
  445. }
  446. //-----------------------------------------------------------------------------
  447. void Actor::push_at(const Vector3& vel, float mass, const Vector3& pos)
  448. {
  449. add_impulse_at(vel * mass, pos);
  450. }
  451. //-----------------------------------------------------------------------------
  452. bool Actor::is_sleeping()
  453. {
  454. if (is_static())
  455. return true;
  456. return static_cast<PxRigidDynamic*>(m_actor)->isSleeping();
  457. }
  458. //-----------------------------------------------------------------------------
  459. void Actor::wake_up()
  460. {
  461. if (is_static())
  462. return;
  463. static_cast<PxRigidDynamic*>(m_actor)->wakeUp();
  464. }
  465. //-----------------------------------------------------------------------------
  466. UnitId Actor::unit_id() const
  467. {
  468. return m_unit;
  469. }
  470. //-----------------------------------------------------------------------------
  471. Unit* Actor::unit()
  472. {
  473. return (m_unit.id == INVALID_ID) ? NULL : m_world.world().get_unit(m_unit);
  474. }
  475. //-----------------------------------------------------------------------------
  476. void Actor::update(const Matrix4x4& pose)
  477. {
  478. m_scene_graph.set_world_pose(m_node, pose);
  479. }
  480. } // namespace crown