Actor.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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 "PxCooking.h"
  36. #include "PxDefaultStreams.h"
  37. using physx::PxActorFlag;
  38. using physx::PxActorType;
  39. using physx::PxBoxGeometry;
  40. using physx::PxCapsuleGeometry;
  41. using physx::PxConvexFlag;
  42. using physx::PxConvexMesh;
  43. using physx::PxConvexMeshDesc;
  44. using physx::PxD6Axis;
  45. using physx::PxD6Joint;
  46. using physx::PxD6JointCreate;
  47. using physx::PxD6Motion;
  48. using physx::PxFilterData;
  49. using physx::PxForceMode;
  50. using physx::PxMat44;
  51. using physx::PxPlaneGeometry;
  52. using physx::PxReal;
  53. using physx::PxRigidActor;
  54. using physx::PxRigidBody;
  55. using physx::PxRigidBodyExt;
  56. using physx::PxRigidDynamic;
  57. using physx::PxRigidDynamicFlag;
  58. using physx::PxRigidStatic;
  59. using physx::PxShape;
  60. using physx::PxShapeFlag;
  61. using physx::PxSphereGeometry;
  62. using physx::PxTransform;
  63. using physx::PxU32;
  64. using physx::PxU16;
  65. using physx::PxVec3;
  66. using physx::PxDefaultMemoryOutputStream;
  67. using physx::PxDefaultMemoryInputData;
  68. using physx::PxConvexMeshGeometry;
  69. namespace crown
  70. {
  71. //-----------------------------------------------------------------------------
  72. Actor::Actor(const PhysicsResource* res, const PhysicsConfigResource* config, uint32_t index, PxPhysics* physics, PxCooking* cooking,
  73. PxScene* scene, SceneGraph& sg, int32_t node, const Vector3& pos, const Quaternion& rot)
  74. : m_resource(res)
  75. , m_config(config)
  76. , m_index(index)
  77. , m_scene(scene)
  78. , m_scene_graph(sg)
  79. , m_node(node)
  80. {
  81. const PhysicsActor& actor = m_resource->actor(m_index);
  82. const PhysicsActor2& actor_class = config->actor(actor.actor_class);
  83. const PxMat44 pose((PxReal*) (sg.world_pose(node).to_float_ptr()));
  84. if (actor_class.flags & PhysicsActor2::DYNAMIC)
  85. {
  86. m_actor = physics->createRigidDynamic(PxTransform(pose));
  87. if (actor_class.flags & PhysicsActor2::KINEMATIC)
  88. {
  89. static_cast<PxRigidDynamic*>(m_actor)->setRigidDynamicFlag(PxRigidDynamicFlag::eKINEMATIC, true);
  90. }
  91. PxD6Joint* joint = PxD6JointCreate(*physics, m_actor, PxTransform(pose), NULL, PxTransform(pose));
  92. joint->setMotion(PxD6Axis::eX, PxD6Motion::eFREE);
  93. joint->setMotion(PxD6Axis::eY, PxD6Motion::eFREE);
  94. joint->setMotion(PxD6Axis::eSWING2, PxD6Motion::eFREE);
  95. }
  96. else
  97. {
  98. m_actor = physics->createRigidStatic(PxTransform(pose));
  99. }
  100. m_actor->userData = this;
  101. create_shapes(res, config, physics, cooking);
  102. // FIXME collisions works only if enable_collision() is called here first
  103. // collision enabled by default
  104. enable_collision();
  105. m_actor->setActorFlag(PxActorFlag::eSEND_SLEEP_NOTIFIES, true);
  106. m_scene->addActor(*m_actor);
  107. }
  108. //-----------------------------------------------------------------------------
  109. Actor::~Actor()
  110. {
  111. if (m_actor)
  112. {
  113. m_scene->removeActor(*m_actor);
  114. m_actor->release();
  115. }
  116. }
  117. //-----------------------------------------------------------------------------
  118. void Actor::create_shapes(const PhysicsResource* res, const PhysicsConfigResource* config, PxPhysics* physics, PxCooking* cooking)
  119. {
  120. const PhysicsActor& actor = m_resource->actor(m_index);
  121. uint32_t shape_index = m_resource->shape_index(m_index);
  122. for (uint32_t i = 0; i < actor.num_shapes; i++)
  123. {
  124. const PhysicsShape& shape = m_resource->shape(shape_index);
  125. const PhysicsShape2& shape_class = config->shape(shape.shape_class);
  126. const PhysicsMaterial& material = config->material(shape.material);
  127. PxMaterial* mat = physics->createMaterial(material.static_friction, material.dynamic_friction, material.restitution);
  128. PxShape* px_shape = NULL;
  129. switch(shape.type)
  130. {
  131. case PhysicsShapeType::SPHERE:
  132. {
  133. px_shape = m_actor->createShape(PxSphereGeometry(shape.data_0), *mat);
  134. break;
  135. }
  136. case PhysicsShapeType::CAPSULE:
  137. {
  138. px_shape = m_actor->createShape(PxCapsuleGeometry(shape.data_0, shape.data_1), *mat);
  139. break;
  140. }
  141. case PhysicsShapeType::BOX:
  142. {
  143. px_shape = m_actor->createShape(PxBoxGeometry(shape.data_0, shape.data_1, shape.data_2), *mat);
  144. break;
  145. }
  146. case PhysicsShapeType::PLANE:
  147. {
  148. px_shape = m_actor->createShape(PxPlaneGeometry(), *mat);
  149. break;
  150. }
  151. case PhysicsShapeType::CONVEX_MESH:
  152. {
  153. MeshResource* resource = (MeshResource*) device()->resource_manager()->data(shape.resource);
  154. PxConvexMeshDesc convex_mesh_desc;
  155. convex_mesh_desc.points.count = resource->num_vertices();
  156. convex_mesh_desc.points.stride = sizeof(PxVec3);
  157. convex_mesh_desc.points.data = (PxVec3*) resource->vertices();
  158. convex_mesh_desc.triangles.count = resource->num_indices();
  159. convex_mesh_desc.triangles.stride = 3 * sizeof(PxU16);
  160. convex_mesh_desc.triangles.data = (PxU16*) resource->indices();
  161. convex_mesh_desc.flags = PxConvexFlag::eCOMPUTE_CONVEX;
  162. convex_mesh_desc.vertexLimit = MAX_PHYSX_VERTICES;
  163. PxDefaultMemoryOutputStream buf;
  164. if(!cooking->cookConvexMesh(convex_mesh_desc, buf))
  165. CE_FATAL();
  166. PxDefaultMemoryInputData input(buf.getData(), buf.getSize());
  167. PxConvexMesh* convex_mesh = physics->createConvexMesh(input);
  168. px_shape = m_actor->createShape(PxConvexMeshGeometry(convex_mesh), *mat);
  169. break;
  170. }
  171. default:
  172. {
  173. CE_FATAL("Oops, unknown shape type");
  174. }
  175. }
  176. if (shape_class.trigger)
  177. {
  178. px_shape->setFlag(PxShapeFlag::eSIMULATION_SHAPE, false);
  179. px_shape->setFlag(PxShapeFlag::eTRIGGER_SHAPE, true);
  180. }
  181. shape_index++;
  182. }
  183. // PxFilterData filter;
  184. // filter.word0 = (PxU32) m_group;
  185. // filter.word1 = (PxU32) m_mask;
  186. // shape->SetSimulationFilterData()
  187. }
  188. //-----------------------------------------------------------------------------
  189. void Actor::enable_gravity()
  190. {
  191. m_actor->setActorFlag(PxActorFlag::eDISABLE_GRAVITY, false);
  192. }
  193. //-----------------------------------------------------------------------------
  194. void Actor::disable_gravity()
  195. {
  196. m_actor->setActorFlag(PxActorFlag::eDISABLE_GRAVITY, true);
  197. }
  198. //-----------------------------------------------------------------------------
  199. void Actor::enable_collision()
  200. {
  201. // PxFilterData filter_data;
  202. // filter_data.word0 = (PxU32) m_group;
  203. // filter_data.word1 = (PxU32) m_mask;
  204. // const PxU32 num_shapes = m_actor->getNbShapes();
  205. // PxShape** shapes = (PxShape**) default_allocator().allocate((sizeof(PxShape*) * num_shapes));
  206. // m_actor->getShapes(shapes, num_shapes);
  207. // for(PxU32 i = 0; i < num_shapes; i++)
  208. // {
  209. // PxShape* shape = shapes[i];
  210. // shape->setSimulationFilterData(filter_data);
  211. // }
  212. // default_allocator().deallocate(shapes);
  213. }
  214. //-----------------------------------------------------------------------------
  215. void Actor::disable_collision()
  216. {
  217. // PxFilterData filter_data;
  218. // filter_data.word0 = (PxU32) CollisionGroup::GROUP_0;
  219. // filter_data.word1 = (PxU32) CollisionGroup::GROUP_0;
  220. // const PxU32 num_shapes = m_actor->getNbShapes();
  221. // PxShape** shapes = (PxShape**) default_allocator().allocate((sizeof(PxShape*) * num_shapes));
  222. // m_actor->getShapes(shapes, num_shapes);
  223. // for(PxU32 i = 0; i < num_shapes; i++)
  224. // {
  225. // PxShape* shape = shapes[i];
  226. // shape->setSimulationFilterData(filter_data);
  227. // }
  228. // default_allocator().deallocate(shapes);
  229. }
  230. //-----------------------------------------------------------------------------
  231. void Actor::set_kinematic()
  232. {
  233. static_cast<PxRigidDynamic*>(m_actor)->setRigidDynamicFlag(PxRigidDynamicFlag::eKINEMATIC, true);
  234. }
  235. //-----------------------------------------------------------------------------
  236. void Actor::clear_kinematic()
  237. {
  238. static_cast<PxRigidDynamic*>(m_actor)->setRigidDynamicFlag(PxRigidDynamicFlag::eKINEMATIC, false);
  239. m_scene_graph.set_world_pose(m_node, get_kinematic_pose());
  240. }
  241. //-----------------------------------------------------------------------------
  242. void Actor::move(const Vector3& pos)
  243. {
  244. CE_ASSERT(is_kinematic(), "Cannot call 'move' method for non-kinematic actor");
  245. PxVec3 position(pos.x, pos.y, pos.z);
  246. static_cast<PxRigidDynamic*>(m_actor)->setKinematicTarget(PxTransform(position));
  247. }
  248. //-----------------------------------------------------------------------------
  249. Matrix4x4 Actor::get_kinematic_pose() const
  250. {
  251. PxTransform transform;
  252. static_cast<PxRigidDynamic*>(m_actor)->getKinematicTarget(transform);
  253. Quaternion rot(transform.q.x, transform.q.y, transform.q.z, transform.q.w);
  254. Vector3 pos(transform.p.x, transform.p.y, transform.p.z);
  255. return Matrix4x4(rot, pos);
  256. }
  257. //-----------------------------------------------------------------------------
  258. bool Actor::is_static() const
  259. {
  260. return m_actor->getType() & PxActorType::eRIGID_STATIC;
  261. }
  262. //-----------------------------------------------------------------------------
  263. bool Actor::is_dynamic() const
  264. {
  265. return m_actor->getType() & PxActorType::eRIGID_DYNAMIC;
  266. }
  267. //-----------------------------------------------------------------------------
  268. bool Actor::is_kinematic() const
  269. {
  270. if (!is_dynamic()) return false;
  271. return static_cast<PxRigidDynamic*>(m_actor)->getRigidDynamicFlags() & PxRigidDynamicFlag::eKINEMATIC;
  272. }
  273. //-----------------------------------------------------------------------------
  274. float Actor::linear_damping() const
  275. {
  276. return ((PxRigidDynamic*)m_actor)->getLinearDamping();
  277. }
  278. //-----------------------------------------------------------------------------
  279. void Actor::set_linear_damping(float rate)
  280. {
  281. ((PxRigidDynamic*)m_actor)->setLinearDamping(rate);
  282. }
  283. //-----------------------------------------------------------------------------
  284. float Actor::angular_damping() const
  285. {
  286. return ((PxRigidDynamic*)m_actor)->getAngularDamping();
  287. }
  288. //-----------------------------------------------------------------------------
  289. void Actor::set_angular_damping(float rate)
  290. {
  291. ((PxRigidDynamic*)m_actor)->setAngularDamping(rate);
  292. }
  293. //-----------------------------------------------------------------------------
  294. Vector3 Actor::linear_velocity() const
  295. {
  296. PxVec3 vel = ((PxRigidBody*)m_actor)->getLinearVelocity();
  297. Vector3 velocity(vel.x, vel.y, vel.z);
  298. return velocity;
  299. }
  300. //-----------------------------------------------------------------------------
  301. void Actor::set_linear_velocity(const Vector3& vel)
  302. {
  303. PxVec3 velocity(vel.x, vel.y, vel.z);
  304. ((PxRigidBody*)m_actor)->setLinearVelocity(velocity);
  305. }
  306. //-----------------------------------------------------------------------------
  307. Vector3 Actor::angular_velocity() const
  308. {
  309. PxVec3 vel = ((PxRigidBody*)m_actor)->getAngularVelocity();
  310. Vector3 velocity(vel.x, vel.y, vel.z);
  311. return velocity;
  312. }
  313. //-----------------------------------------------------------------------------
  314. void Actor::set_angular_velocity(const Vector3& vel)
  315. {
  316. PxVec3 velocity(vel.x, vel.y, vel.z);
  317. ((PxRigidBody*)m_actor)->setAngularVelocity(velocity);
  318. }
  319. //-----------------------------------------------------------------------------
  320. void Actor::add_impulse(const Vector3& impulse)
  321. {
  322. Vector3 p = m_scene_graph.world_pose(m_node).translation();
  323. PxRigidBodyExt::addForceAtPos(*static_cast<PxRigidDynamic*>(m_actor),
  324. PxVec3(impulse.x, impulse.y, impulse.z),
  325. PxVec3(p.x, p.y, p.z),
  326. PxForceMode::eIMPULSE,
  327. true);
  328. }
  329. //-----------------------------------------------------------------------------
  330. void Actor::add_impulse_at(const Vector3& impulse, const Vector3& pos)
  331. {
  332. PxRigidBodyExt::addForceAtLocalPos(*static_cast<PxRigidDynamic*>(m_actor),
  333. PxVec3(impulse.x, impulse.y, impulse.z),
  334. PxVec3(pos.x, pos.y, pos.z),
  335. PxForceMode::eIMPULSE,
  336. true);
  337. }
  338. //-----------------------------------------------------------------------------
  339. void Actor::push(const Vector3& vel, const float mass)
  340. {
  341. // FIXME FIXME FIXME
  342. Vector3 p = m_scene_graph.world_pose(m_node).translation();
  343. Vector3 mq(vel.x * mass, vel.y * mass, vel.z * mass);
  344. Vector3 f(mq.x / 0.017, mq.y / 0.017, mq.z / 0.017);
  345. PxRigidBodyExt::addForceAtPos(*static_cast<PxRigidDynamic*>(m_actor),
  346. PxVec3(f.x, f.y, f.z),
  347. PxVec3(p.x, p.y, p.z));
  348. }
  349. //-----------------------------------------------------------------------------
  350. bool Actor::is_sleeping()
  351. {
  352. return ((PxRigidDynamic*)m_actor)->isSleeping();
  353. }
  354. //-----------------------------------------------------------------------------
  355. void Actor::wake_up()
  356. {
  357. ((PxRigidDynamic*)m_actor)->wakeUp();
  358. }
  359. //-----------------------------------------------------------------------------
  360. StringId32 Actor::name()
  361. {
  362. const PhysicsActor& a = m_resource->actor(m_index);
  363. return a.name;
  364. }
  365. //-----------------------------------------------------------------------------
  366. void Actor::update(const Matrix4x4& pose)
  367. {
  368. m_scene_graph.set_world_pose(m_node, pose);
  369. }
  370. } // namespace crown