actor.cpp 12 KB

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