Actor.cpp 13 KB

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