actor.cpp 12 KB

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