Ejemplo n.º 1
0
	// Physics dll internal
	void RefreshColShape(IPhysicsInterface* colProvider){
		if (!colProvider)
		{
			Logger::Log(FB_ERROR_LOG_ARG, "No colProvider");			
			return;
		}
		mWorld->removeRigidBody(mSelf);
		mAddedToWorld = false;
		auto prevColShape = mSelf->getCollisionShape();
		if (prevColShape){
			Physics::GetInstance().Release(prevColShape);
		}

		btCollisionShape* colShape = 0;
		float mass = 1.0f;
		auto& physics = Physics::GetInstance();
		if (mGroupedRigidBody){
			if (colProvider->GetNumColShapes(mGroupIdx) == 0)
				return;

			colShape = physics.CreateColShapeForGroup(colProvider, mGroupIdx);
			mass = colProvider->GetMassForGroup(mGroupIdx);
		}
		else{
			if (colProvider->GetNumColShapes() == 0)
				return;

			colShape = physics.CreateColShape(colProvider);
			mass = colProvider->GetMass();
		}
		assert(colShape);
		mSelf->setCollisionShape(colShape);
		if (colShape)
			physics.AddRef(colShape);

		if (mass > 0 && colShape)
		{
			btVector3 inertia;
			colShape->calculateLocalInertia(mass, inertia);			
			mSelf->setMassProps(mass, inertia);
		}
		else{
			SetMass(0.f);
		}
		assert(!mAddedToWorld);
		mWorld->addRigidBody(mSelf, colProvider->GetCollisionGroup(), colProvider->GetCollisionMask());
		mAddedToWorld = true;
	}
	~Impl(){
		UnregisterFromWorld();
		mSelf->setUserPointer(0);
		mGamePtr = 0;
		FB_DELETE(mRotationInfo);
		RemoveConstraints();

		auto colShape = mSelf->getCollisionShape();
		if (colShape){
			Physics::GetInstance().Release(colShape);
		}

		if (mWorld){
			mWorld->removeRigidBody(mSelf);
			mAddedToWorld = false;
		}
		FB_DELETE_ALIGNED(mSelf->getMotionState());
	}
	//---------------------------------------------------------------------------
	Impl(RigidBodyImpl* self, btDiscreteDynamicsWorld* world, IPhysicsInterface* colProvider)
		: mSelf(self)
		, mWorld(world)
		, mPhysicsInterface(0)
		, mGamePtr(0)
		, mColProvider(colProvider)
		, mGroupedRigidBody(false)
		, mGroupIdx(Vec3I::MAX)
		, mGameFlag(0)
		, mAddedToWorld(false)
		, mDebug(false)
	{
		auto colShape = mSelf->getCollisionShape();
		if (colShape)
		{
			Physics::GetInstance().AddRef(colShape);
		}
		mRotationInfo = FB_NEW(RotationInfo);
		mSelf->setUserPointer(self);
	}
	void* GetColShapeUserPtr(int idx = 0){
		auto colShape = mSelf->getCollisionShape();
		assert(colShape);
		if (colShape->isCompound())
		{
			btCompoundShape* compound = (btCompoundShape*)colShape;
			if (idx < compound->getNumChildShapes() && idx >= 0)
			{
				auto child = compound->getChildShape(idx);
				if (child)
				{
					return child->getUserPointer();
				}
			}
		}
		else
		{
			return colShape->getUserPointer();
		}
		return 0;
	}
	void SetMass(float mass){
		btVector3 inertia;
		mSelf->getCollisionShape()->calculateLocalInertia(mass, inertia);
		mSelf->setMassProps(mass, inertia);
	}