Browse Source

implement some methods and make some fixes in Actor

mikymod 12 years ago
parent
commit
46a5280a24
2 changed files with 100 additions and 4 deletions
  1. 91 4
      engine/physics/Actor.cpp
  2. 9 0
      engine/physics/Actor.h

+ 91 - 4
engine/physics/Actor.cpp

@@ -52,12 +52,14 @@ using physx::PxShape;
 using physx::PxShapeFlag;
 using physx::PxU32;
 using physx::PxFilterData;
+using physx::PxForceMode;
 
 using physx::PxD6Joint;
 using physx::PxD6JointCreate;
 using physx::PxD6Axis;
 using physx::PxD6Motion;
 
+
 namespace crown
 {
 
@@ -91,8 +93,6 @@ Actor::Actor(const PhysicsResource* res, uint32_t i, PxPhysics* physics, PxScene
 			{
 				static_cast<PxRigidDynamic*>(m_actor)->setRigidDynamicFlag(PxRigidDynamicFlag::eKINEMATIC, true);
 			}
-			//break;
-
 			//PxRigidBodyExt::setMassAndUpdateInertia(*static_cast<PxRigidDynamic*>(m_actor), 500.0f);
 
 			PxD6Joint* joint = PxD6JointCreate(*physics, m_actor, PxTransform(pose), NULL, PxTransform(pose));
@@ -101,6 +101,7 @@ Actor::Actor(const PhysicsResource* res, uint32_t i, PxPhysics* physics, PxScene
 			//joint->setMotion(PxD6Axis::eZ, PxD6Motion::eFREE);
 			//joint->setMotion(PxD6Axis::eSWING1, PxD6Motion::eFREE);
 			joint->setMotion(PxD6Axis::eSWING2, PxD6Motion::eFREE);
+
 			break;
 		}
 		default:
@@ -147,6 +148,13 @@ Actor::Actor(const PhysicsResource* res, uint32_t i, PxPhysics* physics, PxScene
 		index++;
 	}
 
+	m_group = a.group;
+	m_mask = a.mask;
+
+	// FIXME collisions works only if enable_collision() is called here first
+	// collision enabled by default
+	enable_collision();
+
 	m_actor->setActorFlag(PxActorFlag::eSEND_SLEEP_NOTIFIES, true);
 	m_scene->addActor(*m_actor);
 }
@@ -164,13 +172,13 @@ Actor::~Actor()
 //-----------------------------------------------------------------------------
 void Actor::create_sphere(const Vector3& position, float radius)
 {
-	/*PxShape* shape = */m_actor->createShape(PxSphereGeometry(radius), *m_mat);
+	m_actor->createShape(PxSphereGeometry(radius), *m_mat);
 }
 
 //-----------------------------------------------------------------------------
 void Actor::create_box(const Vector3& position, float half_x, float half_y, float half_z)
 {
-	/*PxShape* shape = */m_actor->createShape(PxBoxGeometry(half_x, half_y, half_z), *m_mat);
+	m_actor->createShape(PxBoxGeometry(half_x, half_y, half_z), *m_mat);
 }
 
 //-----------------------------------------------------------------------------
@@ -191,6 +199,48 @@ void Actor::disable_gravity()
 	m_actor->setActorFlag(PxActorFlag::eDISABLE_GRAVITY, true);
 }
 
+//-----------------------------------------------------------------------------
+void Actor::enable_collision()
+{
+	PxFilterData filter_data;
+	filter_data.word0 = (PxU32) m_group;
+	filter_data.word1 = (PxU32) m_mask;
+
+	const PxU32 num_shapes = m_actor->getNbShapes();
+
+	PxShape** shapes = (PxShape**) default_allocator().allocate((sizeof(PxShape*) * num_shapes));
+	m_actor->getShapes(shapes, num_shapes);
+
+	for(PxU32 i = 0; i < num_shapes; i++)
+	{
+		PxShape* shape = shapes[i];
+		shape->setSimulationFilterData(filter_data);
+	}
+
+	default_allocator().deallocate(shapes);
+}
+
+//-----------------------------------------------------------------------------
+void Actor::disable_collision()
+{
+	PxFilterData filter_data;
+	filter_data.word0 = (PxU32) CollisionGroup::GROUP_0;
+	filter_data.word1 = (PxU32) CollisionGroup::GROUP_0;
+
+	const PxU32 num_shapes = m_actor->getNbShapes();
+
+	PxShape** shapes = (PxShape**) default_allocator().allocate((sizeof(PxShape*) * num_shapes));
+	m_actor->getShapes(shapes, num_shapes);
+
+	for(PxU32 i = 0; i < num_shapes; i++)
+	{
+		PxShape* shape = shapes[i];
+		shape->setSimulationFilterData(filter_data);
+	}
+
+	default_allocator().deallocate(shapes);	
+}
+
 //-----------------------------------------------------------------------------
 bool Actor::is_static() const
 {
@@ -277,6 +327,43 @@ void Actor::set_angular_velocity(const Vector3& vel)
 	((PxRigidBody*)m_actor)->setAngularVelocity(velocity);
 }
 
+//-----------------------------------------------------------------------------
+void Actor::add_impulse(const Vector3& impulse)
+{
+	Vector3 p = m_scene_graph.world_pose(m_node).translation();
+
+	PxRigidBodyExt::addForceAtPos(*static_cast<PxRigidDynamic*>(m_actor),
+								  PxVec3(impulse.x, impulse.y, impulse.z),
+								  PxVec3(p.x, p.y, p.z),
+								  PxForceMode::eIMPULSE,
+								  true);
+}
+
+//-----------------------------------------------------------------------------
+void Actor::add_impulse_at(const Vector3& impulse, const Vector3& pos)
+{
+	PxRigidBodyExt::addForceAtLocalPos(*static_cast<PxRigidDynamic*>(m_actor),
+									   PxVec3(impulse.x, impulse.y, impulse.z),
+									   PxVec3(pos.x, pos.y, pos.z),
+									   PxForceMode::eIMPULSE,
+									   true);
+}
+
+//-----------------------------------------------------------------------------
+void Actor::push(const Vector3& vel, const float mass)
+{
+	// FIXME FIXME FIXME
+	Vector3 p = m_scene_graph.world_pose(m_node).translation();
+
+	Vector3 mq(vel.x * mass, vel.y * mass, vel.z * mass);
+	Vector3 f(mq.x / 0.017, mq.y / 0.017, mq.z / 0.017);
+
+	PxRigidBodyExt::addForceAtPos(*static_cast<PxRigidDynamic*>(m_actor),
+								  PxVec3(f.x, f.y, f.z),
+								  PxVec3(p.x, p.y, p.z));
+}
+
+
 //-----------------------------------------------------------------------------
 bool Actor::is_sleeping()
 {

+ 9 - 0
engine/physics/Actor.h

@@ -59,6 +59,9 @@ struct Actor
 	void				enable_gravity();
 	void				disable_gravity();
 
+	void				enable_collision();
+	void				disable_collision();
+
 	bool				is_static() const;
 	bool				is_dynamic() const;
 
@@ -77,6 +80,10 @@ struct Actor
 	Vector3				angular_velocity() const;
 	void				set_angular_velocity(const Vector3& vel);
 
+	void				add_impulse(const Vector3& impulse);
+	void				add_impulse_at(const Vector3& impulse, const Vector3& pos);
+	void				push(const Vector3& vel, const float mass);
+
 	bool				is_sleeping();
 	void				wake_up();
 
@@ -99,6 +106,8 @@ public:
 	int32_t					m_node;
 	PxRigidActor* 			m_actor;
 	PxMaterial* 			m_mat;
+	uint32_t				m_group;
+	uint32_t				m_mask;
 };
 
 } // namespace crown