void btHfFluidBuoyantConvexShape::generateShape (btScalar radius, btScalar gap)
{
	btTransform T;
	T.setIdentity ();
	btVector3 aabbMin, aabbMax;
	getAabb (T, aabbMin, aabbMax);

	m_radius = radius;
	m_numVoxels = 0;

	btVoronoiSimplexSolver simplexSolver;
	btSphereShape sphereShape(radius);
	btVector3* voxelPositions = (btVector3*)btAlignedAlloc (sizeof(btVector3)*MAX_VOXEL_DIMENSION*MAX_VOXEL_DIMENSION*MAX_VOXEL_DIMENSION,16);
	for (int i = 0; i < MAX_VOXEL_DIMENSION; i++)
	{
		for (int j = 0; j < MAX_VOXEL_DIMENSION; j++)
		{
			for (int k = 0; k < MAX_VOXEL_DIMENSION; k++)
			{
				btVector3 point;
				btTransform sT;
				sT.setIdentity ();
				
				point.setX(aabbMin.getX() + (i * btScalar(2.0f) * radius) + (i * gap));
				point.setY(aabbMin.getY() + (j * btScalar(2.0f) * radius) + (j * gap));
				point.setZ(aabbMin.getZ() + (k * btScalar(2.0f) * radius) + (k * gap));
				
				if (TestPointAgainstAabb2(aabbMin, aabbMax, point))
				{
					btTransform sT;
					sT.setIdentity ();
					sT.setOrigin (point);

					if (intersect (&simplexSolver, T, sT, m_convexShape, &sphereShape))
					{
						voxelPositions[m_numVoxels] = point;
						m_numVoxels++;
					}
				}
			}
		}
	}
	m_voxelPositions = (btVector3*)btAlignedAlloc (sizeof(btVector3)*m_numVoxels, 16);
	for (int i = 0; i < m_numVoxels;i++)
	{
		m_voxelPositions[i] = voxelPositions[i];
	}
	btAlignedFree (voxelPositions);
	m_volumePerVoxel = btScalar(4.0f)/btScalar(3.0f)*SIMD_PI*radius*radius*radius;
	m_totalVolume = m_numVoxels * m_volumePerVoxel;
	m_radius = radius;
}
예제 #2
0
const bool NewPhysicsTrace(NewPhysTraceResults* const out, const Ogre::Vector3& start, const Ogre::Vector3& end, 
	const Ogre::Vector3& BBHalfExtents, const Ogre::Vector3& rotation, bool isInterior, OEngine::Physic::PhysicEngine* enginePass)
{
	//if (!traceobj->incellptr)
	//	return false;
	//if(enginePass->dynamicsWorld->getCollisionObjectArray().at(60)->getCollisionShape()->isConvex())
	//	std::cout << "It's convex\n";
	


	const btVector3 btstart(start.x, start.y, start.z);
	const btVector3 btend(end.x, end.y, end.z);
	const btQuaternion btrot(rotation.y, rotation.x, rotation.z);   //y, x, z

	const btBoxShape newshape(btVector3(BBHalfExtents.x, BBHalfExtents.y, BBHalfExtents.z));
	const btTransform from(btrot, btstart);
	const btTransform to(btrot, btend);

    // warning: unused variable ...
    /*
	float x = from.getOrigin().getX();
	float y = from.getOrigin().getY();
	float z = from.getOrigin().getZ();
	float x2 = to.getOrigin().getX();
	float y2 = to.getOrigin().getY();
	float z2 = to.getOrigin().getZ();
    */
	
	//std::cout << "BtFrom: " << x << "," << y << "," << z << "\n";
	//std::cout << "BtTo: " << x2 << "," << y2 << "," << z2 << "\n";
	//std::cout << "BtTo: " << to.getOrigin().getX() << "," << to.getOrigin().getY() << "," << to.getOrigin().getZ() << "\n";


	btCollisionWorld::ClosestConvexResultCallback
		newTraceCallback(btstart, btend);

	newTraceCallback.m_collisionFilterMask = (traceType == collisionWorldTrace) ? Only_Collision : Only_Pickup;
	
	
	enginePass->dynamicsWorld->convexSweepTest(&newshape, from, to, newTraceCallback);
	//newTraceCallback.
	
	
	//std::cout << "NUM: " << enginePass->dynamicsWorld->getNumCollisionObjects() << "\n";

	// Copy the hit data over to our trace results struct:
	out->fraction = newTraceCallback.m_closestHitFraction;

	Ogre::Vector3& outhitnormal = out->hitNormal;
	const btVector3& tracehitnormal = newTraceCallback.m_hitNormalWorld;

	outhitnormal.x = tracehitnormal.x();
	outhitnormal.y = tracehitnormal.y();
	outhitnormal.z = tracehitnormal.z();

	Ogre::Vector3& outhitpos = out->endPos;
	const btVector3& tracehitpos = newTraceCallback.m_hitPointWorld;

	outhitpos.x = tracehitpos.x();
	outhitpos.y = tracehitpos.y();
	outhitpos.z= tracehitpos.z();

	// StartSolid test:
	{
		out->startSolid = false;
		//btCollisionObject collision;
		//collision.setCollisionShape(const_cast<btBoxShape* const>(&newshape) );

		//CustomContactCallback crb;

		//world.world->contactTest(&collision, crb);
		//out->startSolid = crb.hit;

		// If outside and underground, we're solid
		if (!isInterior)   //Check if we are interior
		{
		}

		// If inside and out of the tree, we're solid
		else
		{
			btVector3 aabbMin, aabbMax;
			enginePass->broadphase->getBroadphaseAabb(aabbMin, aabbMax);
			//std::cout << "AABBMIN" << aabbMin.getX() <<"," <<aabbMin.getY() << "," << aabbMin.getZ()  << "\n";
			//std::cout << "AABBMAX" << aabbMax.getX() <<"," <<aabbMax.getY() << "," << aabbMax.getZ()  << "\n";
			//std::cout << "AABBMAX" << aabbMax << "\n";
			if (!TestPointAgainstAabb2(aabbMin, aabbMax, *(const btVector3* const)&(start) ) )
			{
				//We're solid
				out->startSolid = true;
			}
		}
	}

	const bool hasHit = newTraceCallback.hasHit();

	

	
	return hasHit;
}