actor.cpp 12 KB

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