コード例 #1
0
ファイル: Boid.cpp プロジェクト: Gerjo/NintendoDS-Flocking
void Boid::seperation(deque<Boid*>& neighbours) {
	Vector2D steeringForce(0, 0);
	Vector2D vDisplacement(0, 0);
	
	deque<Boid*>::iterator itBoid;
	
	for(itBoid = neighbours.begin(); itBoid != neighbours.end(); ++itBoid) {
		if(*itBoid == this) continue;
		
		vDisplacement = position;
		vDisplacement.subtract((*itBoid)->getPosition());
 
		steeringForce = vDisplacement;
		steeringForce.devide(vDisplacement.getLength());
	}
	
	steeringForce.normalize();
	
	steeringForce.scale(0.1f);
	
	velocity.add(steeringForce);
	
}
コード例 #2
0
ファイル: Mole.cpp プロジェクト: bullfrognz/MoleMadness
void
CMole::ComputeRepulsorForce(CFVec2& _vrRepelForce)
{
	// Get wall repulsors
	CWall* pWalls = 0;
	int iNumWalls = 0;	


	// Get mole repulsors
	CMole* pMoles = 0;
	int iNumMoles = 0;


	CMMMContext& rMMMContext = CMMMContext::GetInstance();
	rMMMContext.GetWalls(pWalls, iNumWalls);
	rMMMContext.GetMoles(pMoles, iNumMoles);


	// Get repulsors
	std::vector<CRepulsor*> m_aRepulsors;


	for (int i = 0; i < iNumWalls; ++ i)
	{
		m_aRepulsors.push_back( &pWalls[i].GetRepulsor(0) );
		m_aRepulsors.push_back( &pWalls[i].GetRepulsor(1) );
	}


	for (int i = 0; i < iNumMoles; ++ i)
	{
		if (&pMoles[i] != this)
		m_aRepulsors.push_back( pMoles[i].GetRepulsor() );
	}



	for (unsigned int j = 0; j < m_aRepulsors.size(); ++ j)
	{
		CFVec2 vRepulseForce(0.0f, 0.0f);


		CFVec2 vRepulsorPosition = m_aRepulsors[j]->GetPosition();
		float fRepulsorRadius    = m_aRepulsors[j]->GetRadius();
		float fRepulsionRadius   = m_aRepulsors[j]->GetRepulsionRadius();

			
		// Displacement to repulsor
		CFVec2 vDisplacement(m_vPosition);
		vDisplacement -= vRepulsorPosition;


		// Direction to repulsor
		CFVec2 vDirection(vDisplacement);
		vDirection.Normalise();


		// Distance to repulsor
		float fDistance(vDisplacement.Magnitude());
		fDistance -= fRepulsorRadius;
		fDistance -= m_fRadius;


		// Within repulsion radius
		if (fDistance < fRepulsionRadius)
		{
			// How far into repulsion radius
			float fRepulsionRatio = 1.0f - (fDistance / fRepulsionRadius);


			// Opposite direction
			CFVec2 vOppositeDirection(vDirection);
			vOppositeDirection.Normalise();

				
			// Apply force opposite direction to repulsor
			vRepulseForce = vOppositeDirection;
			vRepulseForce *= m_aRepulsors[j]->GetMultiplier();
			vRepulseForce *= fRepulsionRatio;


			_vrRepelForce += vRepulseForce;
		}
	}
}
コード例 #3
0
void CIFXMotionDecoder::ProcessMotionCompressedX( IFXDataBlockX &rDataBlockX )
{
	if(NULL == m_pCoreServices) {
		IFXCHECKX(IFX_E_NOT_INITIALIZED);
	}

	// Get the scene graph
	IFXDECLARELOCAL(IFXSceneGraph,pSceneGraph);
	IFXCHECKX(m_pCoreServices->GetSceneGraph( IID_IFXSceneGraph,( void** )&pSceneGraph ));

	// Process the motion description data block
	// The motion description block has the following sections:
	// 1. MotionName
	// 2. TrackCount
	// 3. InverseQuantTime
	// 4. InverseQuantRotation
	//    5. TrackName
	//    6. TimeCount
	//    7. InverseQuantDisplacement
	//    8. InverseQuantScale
	//      9. Time
	//      10. Displacement
	//      11. Rotation
	//      12. Scale

	// set metadata
	IFXDECLARELOCAL(IFXMetaDataX, pBlockMD);
	IFXDECLARELOCAL(IFXMetaDataX, pObjectMD);
	rDataBlockX.QueryInterface(IID_IFXMetaDataX, (void**)&pBlockMD);
	m_pObject->QueryInterface(IID_IFXMetaDataX, (void**)&pObjectMD);
	pObjectMD->AppendX(pBlockMD);

	// Create a bitstream component and initialize it to decode the block
	IFXDECLARELOCAL(IFXBitStreamCompressedX,pBitStreamX);
	IFXCHECKX(IFXCreateComponent( CID_IFXBitStreamX, IID_IFXBitStreamCompressedX, ( void** )&pBitStreamX ));
	IFXDECLARELOCAL(IFXNameMap, pNameMap);
	m_pCoreServices->GetNameMap(IID_IFXNameMap, (void**)&pNameMap);
	U32 uProfile;
	pNameMap->GetProfile(m_uLoadId, uProfile);
	pBitStreamX->SetNoCompressionMode((uProfile & IFXPROFILE_NOCOMPRESSION) ? TRUE : FALSE);

	U32 uBlockType = 0;
	rDataBlockX.GetBlockTypeX(uBlockType);
	pBitStreamX->SetDataBlockX( rDataBlockX );

	// 1. MotionName
	IFXString stringMotionName;
	// read the motion name
	pBitStreamX->ReadIFXStringX( stringMotionName );
	IFXCHECKX(pNameMap->Map(m_uLoadId, IFXSceneGraph::MOTION, stringMotionName));

	// Access the appropriate interface of the shader that was handed in via SetObject().
	IFXDECLARELOCAL(IFXMotionResource,pMotionResource);
	IFXCHECKX(m_pObject->QueryInterface( IID_IFXMotionResource, (void**)&pMotionResource ));

	pMotionResource->GetMotionRef()->SetName(stringMotionName);

	// 2. TrackCount
	U32 uTrackCount = 0;
	pBitStreamX->ReadU32X( uTrackCount );
	// 3. InverseQuantTime
	F32 fInverseQuantTime = 1.0;
	pBitStreamX->ReadF32X( fInverseQuantTime );
	// 4. InverseQuantRotation
	F32 fInverseQuantRotation = 1.0;
	pBitStreamX->ReadF32X( fInverseQuantRotation );

	U32 i = 0;
	for( i=0; i < uTrackCount; i++ )
	{
		// 5. TrackName
		IFXString trackName;
		pBitStreamX->ReadIFXStringX( trackName );
		IFXDECLARELOCAL(IFXNameMap, pNameMap);
		m_pCoreServices->GetNameMap(IID_IFXNameMap, (void**)&pNameMap);
		IFXCHECKX(pNameMap->Map(m_uLoadId, IFXSceneGraph::MOTION, trackName));
		// Find the track id from the track name
		U32 uTrackID = 0;
		IFXRESULT iFindResult = pMotionResource->FindTrack( &trackName, &uTrackID );
		if( IFXFAILURE( iFindResult ) ) {
			IFXCHECKX(pMotionResource->AddTrack( &trackName, &uTrackID ));
		}

		// 6. TimeCount
		U32 uTimeCount = 0;
		pBitStreamX->ReadU32X( uTimeCount );
		IFXKeyFrame *pKeyFrameArray = NULL;
		pKeyFrameArray = new IFXKeyFrame[uTimeCount];
		if( NULL == pKeyFrameArray ) {
			IFXCHECKX(IFX_E_OUT_OF_MEMORY);
		}

		// 7. InverseQuantDisplacement
		F32 fInverseQuantDisplacement = 1.0;
		pBitStreamX->ReadF32X(fInverseQuantDisplacement);

		// 8. InverseQuantScale
		F32 fInverseQuantScale = 1.0;
		pBitStreamX->ReadF32X(fInverseQuantScale);

		IFXKeyFrame thePredictedKeyFrame;
		U32 j = 0;
		for( j=0;  j < uTimeCount; j++ )
		{
			//  9. Time
			F32 fDiffTime = 0.0f;
			if(0==j || (uTimeCount-1) == j)
			{
				pBitStreamX->ReadF32X(fDiffTime);
			}
			else
			{
				U32 uDiffTime = 0;
				U8 u8SignTime = 0;
				pBitStreamX->ReadCompressedU8X(uACContextMotionSignTime,u8SignTime);
				pBitStreamX->ReadCompressedU32X(uACContextMotionDiffTime, uDiffTime );
				fDiffTime = (fInverseQuantTime*uDiffTime);
				if(u8SignTime & 1) {
					fDiffTime *= -1.0f;
				}
			}

			F32 fPredictedTime = thePredictedKeyFrame.Time();
			F32 fTime = fDiffTime + fPredictedTime;
			pKeyFrameArray[j].SetTime( fTime );

			//  10. Displacement
			F32 fdX=0.0f, fdY=0.0f, fdZ=0.0f;
			if(0==j || ((uTimeCount-1) == j && BlockType_ResourceMotionU3D == uBlockType ))
			{
				pBitStreamX->ReadF32X(fdX);
				pBitStreamX->ReadF32X(fdY);
				pBitStreamX->ReadF32X(fdZ);
			}
			else
			{
				U8 u8SignDisplacement = 0;
				U32 udX = 0;
				U32 udY = 0;
				U32 udZ = 0;
				pBitStreamX->ReadCompressedU8X(uACContextMotionSignDisplacement, u8SignDisplacement);
				pBitStreamX->ReadCompressedU32X(uACContextMotionDiffDisplacement, udX );
				pBitStreamX->ReadCompressedU32X(uACContextMotionDiffDisplacement, udY );
				pBitStreamX->ReadCompressedU32X(uACContextMotionDiffDisplacement, udZ );
				fdX = fInverseQuantDisplacement*udX;
				fdY = fInverseQuantDisplacement*udY;
				fdZ = fInverseQuantDisplacement*udZ;
				if(u8SignDisplacement & 1) {
					fdX *= -1.0;
				}
				if(u8SignDisplacement & 2) {
					fdY *= -1.0;
				}
				if(u8SignDisplacement & 4) {
					fdZ *= -1.0;
				}
			}

			IFXVector3 vDisplacement(0.0f,0.0f,0.0f);
			IFXVector3 vPredictedDisplacement = thePredictedKeyFrame.Location();
			IFXVector3 vDiffDisplacement(fdX,fdY,fdZ);
			vDisplacement.Add(vDiffDisplacement,vPredictedDisplacement);
			pKeyFrameArray[j].Location() = vDisplacement;

			//  11. Rotation
			U8 u8SignRot = 0;
			U32 uB=0,uC=0,uD=0;
			if(0==j || ((uTimeCount-1) == j && BlockType_ResourceMotionU3D == uBlockType ))
			{
				F32 fao,fbo,fco,fdo;
				pBitStreamX->ReadF32X(fao);
				pBitStreamX->ReadF32X(fbo);
				pBitStreamX->ReadF32X(fco);
				pBitStreamX->ReadF32X(fdo);
				IFXQuaternion vRotation(1.0f,0.0f,0.0f,0.0f);
				vRotation.Set(fao,fbo,fco,fdo);
				pKeyFrameArray[j].Rotation() = vRotation;
			} else {
				pBitStreamX->ReadCompressedU8X(uACContextMotionSignRotation,u8SignRot);
				pBitStreamX->ReadCompressedU32X(uACContextMotionDiffRotation, uB );
				pBitStreamX->ReadCompressedU32X(uACContextMotionDiffRotation, uC );
				pBitStreamX->ReadCompressedU32X(uACContextMotionDiffRotation, uD );
				F32 fb = fInverseQuantRotation*uB;
				F32 fc = fInverseQuantRotation*uC;
				F32 fd = fInverseQuantRotation*uD;
				if(fb > 1.0f) fb = 1.0f;
				if(fc > 1.0f) fc = 1.0f;
				if(fd > 1.0f) fd = 1.0f;
				F32 fa = (F32) sqrt(fabs(1.0 - ((F64)fb)*((F64)fb) - ((F64)fc)*((F64)fc) - ((F64)fd)*((F64)fd)));
				if(u8SignRot & 1) {
					fa *= -1.0;
				}
				if(u8SignRot & 2) {
					fb *= -1.0;
				}
				if(u8SignRot & 4) {
					fc *= -1.0;
				}
				if(u8SignRot & 8) {
					fd *= -1.0;
				}

				// Set the key frame values
				IFXQuaternion vDiffRotation( fa,fb,fc,fd );
				IFXQuaternion vPredictedRotation = thePredictedKeyFrame.Rotation();
				IFXQuaternion vRotationQ(1.0f,0.0f,0.0f,0.0f);
				vRotationQ.Multiply(vPredictedRotation,vDiffRotation);
				pKeyFrameArray[j].Rotation() = vRotationQ;
			}

			// 12. Scale
			F32 fdsX=0.0f, fdsY=0.0f, fdsZ=0.0f;
			if(0==j || ((uTimeCount-1) == j && BlockType_ResourceMotionU3D == uBlockType ))
			{
				pBitStreamX->ReadF32X(fdsX);
				pBitStreamX->ReadF32X(fdsY);
				pBitStreamX->ReadF32X(fdsZ);
			} else {
				U8 u8SignScale = 0;
				U32 udsX = 0;
				U32 udsY = 0;
				U32 udsZ = 0;
				pBitStreamX->ReadCompressedU8X(uACContextMotionSignScale, u8SignScale);
				pBitStreamX->ReadCompressedU32X(uACContextMotionDiffScale, udsX );
				pBitStreamX->ReadCompressedU32X(uACContextMotionDiffScale, udsY );
				pBitStreamX->ReadCompressedU32X(uACContextMotionDiffScale, udsZ );
				fdsX = fInverseQuantScale*udsX;
				fdsY = fInverseQuantScale*udsY;
				fdsZ = fInverseQuantScale*udsZ;
				if(u8SignScale & 1) {
					fdsX *= -1.0;
				}
				if(u8SignScale & 2) {
					fdsY *= -1.0;
				}
				if(u8SignScale & 4) {
					fdsZ *= -1.0;
				}
			}

			IFXVector3 vScaleQ(0.0f,0.0f,0.0f);
			IFXVector3 vPredictedScaleQ = thePredictedKeyFrame.Scale();
			IFXVector3 vDiffScaleQ(fdsX,fdsY,fdsZ);
			vScaleQ.Add(vDiffScaleQ,vPredictedScaleQ);
			pKeyFrameArray[j].Scale() = vScaleQ;

			// Set the predicted frame for the next key frame
			thePredictedKeyFrame = pKeyFrameArray[j];
		}

		// Set the key frame array to the motion resource
		IFXCHECKX(pMotionResource->ClearTrack( uTrackID ));
		IFXCHECKX(pMotionResource->InsertKeyFrames( uTrackID,uTimeCount,pKeyFrameArray ));
		IFXDELETE_ARRAY( pKeyFrameArray );
	}

	// Now, add a fully-mapped mixer to the mixer palette for this motion
	{
		U32 uMixerID = 0;
		IFXDECLARELOCAL(IFXPalette,pMixerPalette);
		IFXCHECKX(pSceneGraph->GetPalette( IFXSceneGraph::MIXER, &pMixerPalette ));
		IFXRESULT iFindResult = pMixerPalette->Find( &stringMotionName,&uMixerID );
		// if not found, add it to the palette
		if ( IFXFAILURE( iFindResult ) ) {
			IFXCHECKX(pMixerPalette->Add( &stringMotionName,&uMixerID ));
		}

		// now, try to get the motion resource pointer
		IFXDECLARELOCAL(IFXMixerConstruct, pMixerConstruct );
		IFXRESULT iGetResourceResult = pMixerPalette->GetResourcePtr(uMixerID,IID_IFXMixerConstruct,(void**)&pMixerConstruct);
		// if there was no resource pointer, then create one
		if (IFXFAILURE(iGetResourceResult)) {
			IFXCHECKX(IFXCreateComponent( CID_IFXMixerConstruct, IID_IFXMixerConstruct,( void** )&pMixerConstruct ));
			IFXASSERT(pMotionResource);
			pMixerConstruct->SetMotionResource(pMotionResource);

			// Set the scenegraph
			IFXCHECKX(pMixerConstruct->SetSceneGraph( pSceneGraph ));
			pMixerConstruct->SetExternalFlag(m_bExternal);
			pMixerConstruct->SetPriority(rDataBlockX.GetPriorityX(), FALSE, FALSE);

			// set the resource pointer in the palette
			IFXDECLARELOCAL(IFXUnknown,pUnknown);
			IFXCHECKX(pMixerConstruct->QueryInterface(IID_IFXUnknown, (void**)&pUnknown));
			IFXCHECKX(pMixerPalette->SetResourcePtr( uMixerID,pUnknown ));
		}
	}
}