void CParticle::update( NxReal fDeltaTime )
{
	
	Alpha -= 4;

	// アクターを削除する
	if( Alpha < 0 )
	{
		Alpha = 0; 
		m_pParticleEmitter->removeParticle();
		return;
	}

	// Use our down-caster to get a sphere pointer (and make sure it is a sphere)
	NxSphereShape* pSphere = _pActor->getShapes()[0]->isSphere();
	const NxReal fMaxRadius = 0.01f;
	const NxReal fGrowthRate = 0.6f;
	if( pSphere )
	{
		// Grow the radius at growth rate fGrowthRate m/s, until it reaches fMaxRadius meters
		NxReal fCurrentRadius = pSphere->getRadius();
		if ( fCurrentRadius < fMaxRadius )
		{
			pSphere->setRadius( pSphere->getRadius() + fGrowthRate*fDeltaTime );
			if( pSphere->getRadius() > fMaxRadius )
				pSphere->setRadius( fMaxRadius );
		}
	}
}
示例#2
0
void Particle::update(NxReal fDeltaTime)
{
	// particles have a false, thermodynamic force applied which pushes them up every frame
	_pActor->addForce(_vThermoDynamicForce);

	// Use our down-caster to get a sphere pointer (and make sure it is a sphere)
	NxSphereShape* pSphere = _pActor->getShapes()[0]->isSphere();
	const NxReal fMaxRadius = 3.0f;
	const NxReal fGrowthRate = 0.6f;
	if (pSphere)
	{
		// Grow the radius at growth rate fGrowthRate m/s, until it reaches fMaxRadius meters
		NxReal fCurrentRadius = pSphere->getRadius();
		if (fCurrentRadius < fMaxRadius)
		{
			pSphere->setRadius(pSphere->getRadius() + fGrowthRate*fDeltaTime);
			if (pSphere->getRadius() > fMaxRadius)
				pSphere->setRadius(fMaxRadius);
		}
	}
}
示例#3
0
// Make a visible object that can be viewed by users for debugging purposes.
plDrawableSpans* plPXPhysical::CreateProxy(hsGMaterial* mat, hsTArray<uint32_t>& idx, plDrawableSpans* addTo)
{
    plDrawableSpans* myDraw = addTo;
    hsMatrix44 l2w, unused;
    GetTransform(l2w, unused);
    
    bool blended = ((mat->GetLayer(0)->GetBlendFlags() & hsGMatState::kBlendMask));

    NxShape* shape = fActor->getShapes()[0];

    NxTriangleMeshShape* trimeshShape = shape->isTriangleMesh();
    if (trimeshShape)
    {
        NxTriangleMeshDesc desc;
        trimeshShape->getTriangleMesh().saveToDesc(desc);

        hsTArray<hsPoint3>  pos;
        hsTArray<uint16_t>    tris;

        const int kMaxTris = 10000;
        const int kMaxVerts = 32000;
        if ((desc.numVertices < kMaxVerts) && (desc.numTriangles < kMaxTris))
        {
            pos.SetCount(desc.numVertices);
            tris.SetCount(desc.numTriangles * 3);

            for (int i = 0; i < desc.numVertices; i++ )
                pos[i] = GetTrimeshVert(desc, i);

            for (int i = 0; i < desc.numTriangles; i++)
                GetTrimeshTri(desc, i, &tris[i*3]);

            myDraw = plDrawableGenerator::GenerateDrawable(pos.GetCount(), 
                                            pos.AcquireArray(),
                                            nil,    // normals - def to avg (smooth) norm
                                            nil,    // uvws
                                            0,      // uvws per vertex
                                            nil,    // colors - def to white
                                            true,   // do a quick fake shade
                                            nil,    // optional color modulation
                                            tris.GetCount(),
                                            tris.AcquireArray(),
                                            mat,
                                            l2w,
                                            blended,
                                            &idx,
                                            myDraw);
        }
        else
        {
            int curTri = 0;
            int trisToDo = desc.numTriangles;
            while (trisToDo > 0)
            {
                int trisThisRound = trisToDo > kMaxTris ? kMaxTris : trisToDo;
                
                trisToDo -= trisThisRound;

                pos.SetCount(trisThisRound * 3);
                tris.SetCount(trisThisRound * 3);

                for (int i = 0; i < trisThisRound; i++)
                {
                    GetTrimeshTri(desc, curTri, &tris[i*3]);
                    pos[i*3 + 0] = GetTrimeshVert(desc, tris[i*3+0]);
                    pos[i*3 + 1] = GetTrimeshVert(desc, tris[i*3+1]);
                    pos[i*3 + 2] = GetTrimeshVert(desc, tris[i*3+2]);

                    curTri++;
                }
                myDraw = plDrawableGenerator::GenerateDrawable(pos.GetCount(), 
                                                pos.AcquireArray(),
                                                nil,    // normals - def to avg (smooth) norm
                                                nil,    // uvws
                                                0,      // uvws per vertex
                                                nil,    // colors - def to white
                                                true,   // do a quick fake shade
                                                nil,    // optional color modulation
                                                tris.GetCount(),
                                                tris.AcquireArray(),
                                                mat,
                                                l2w,
                                                blended,
                                                &idx,
                                                myDraw);
            }
        }
    }

    NxConvexShape* convexShape = shape->isConvexMesh();
    if (convexShape)
    {
        NxConvexMeshDesc desc;
        convexShape->getConvexMesh().saveToDesc(desc);

        hsTArray<hsPoint3>  pos;
        hsTArray<uint16_t>    tris;

        pos.SetCount(desc.numVertices);
        tris.SetCount(desc.numTriangles * 3);

        for (int i = 0; i < desc.numVertices; i++ )
            pos[i] = GetConvexVert(desc, i);

        for (int i = 0; i < desc.numTriangles; i++)
            GetConvexTri(desc, i, &tris[i*3]);

        myDraw = plDrawableGenerator::GenerateDrawable(pos.GetCount(), 
            pos.AcquireArray(),
            nil,    // normals - def to avg (smooth) norm
            nil,    // uvws
            0,      // uvws per vertex
            nil,    // colors - def to white
            true,   // do a quick fake shade
            nil,    // optional color modulation
            tris.GetCount(),
            tris.AcquireArray(),
            mat,
            l2w,
            blended,
            &idx,
            myDraw);
    }

    NxSphereShape* sphere = shape->isSphere();
    if (sphere)
    {
        float radius = sphere->getRadius();
        hsPoint3 offset = plPXConvert::Point(sphere->getLocalPosition());
        myDraw = plDrawableGenerator::GenerateSphericalDrawable(offset, radius,
            mat, l2w, blended,
            nil, &idx, myDraw);
    }

    NxBoxShape* box = shape->isBox();
    if (box)
    {
        hsPoint3 dim = plPXConvert::Point(box->getDimensions());
        myDraw = plDrawableGenerator::GenerateBoxDrawable(dim.fX*2.f, dim.fY*2.f, dim.fZ*2.f,
            mat,l2w,blended,
            nil,&idx,myDraw);
    }
    return myDraw;
}
示例#4
0
int pWorld::overlapSphereShapes(const VxSphere& worldSphere,CK3dEntity*shapeReference,pShapesType shapeType,CKGroup*shapes,int activeGroups/* =0xffffffff */, const pGroupsMask* groupsMask/* =NULL */, bool accurateCollision/* =false */)
{

	int result=0;

	NxSphere sphere;
	
	if (shapeReference)
	{
		
			NxShape *shape  = getShapeByEntityID(shapeReference->GetID());
			if (shape)
			{
				//shape->checkOverlapAABB()

				NxSphereShape *sphereShape  = static_cast<NxSphereShape*>(shape->isSphere());
				if (sphereShape)
				{
					sphere.radius = sphereShape->getRadius() + worldSphere.Radius();

					//ori : 
					VxVector ori  = worldSphere.Center();
					VxVector oriOut  = ori;
					if (shapeReference)
					{
						shapeReference->Transform(&oriOut,&ori);
					}

					sphere.center = getFrom(oriOut);

				}
			}

	}else{

		sphere.center = getFrom(worldSphere.Center());
		sphere.radius = worldSphere.Radius();

	}

	int total = 0;
	if (shapeType & ST_Dynamic )
	{
		total+=getScene()->getNbDynamicShapes();
	}
	
	if (shapeType & ST_Static)
	{
		total+=getScene()->getNbStaticShapes();
	}

	NxShape** _shapes = (NxShape**)NxAlloca(total*sizeof(NxShape*));
	for (NxU32 i = 0; i < total; i++)  _shapes[i] = NULL;



	NxGroupsMask mask;
	if (groupsMask)
	{
		mask.bits0 = groupsMask->bits0;
		mask.bits1 = groupsMask->bits1;
		mask.bits2 = groupsMask->bits2;
		mask.bits3 = groupsMask->bits3;
	}else{

		mask.bits0 = 0;
		mask.bits1 = 0;
		mask.bits2 = 0;
		mask.bits3 = 0;

	}
	



	result = getScene()->overlapSphereShapes(sphere,(NxShapesType)shapeType,total,_shapes,NULL,activeGroups,&mask,accurateCollision);

	if (_shapes && shapes )
	{
		for (int i  = 0 ; i < result ; i++)
		{
			NxShape *s = _shapes[i];
			if (s)
			{
				const char* name =s->getName();
				pSubMeshInfo *sInfo  = static_cast<pSubMeshInfo*>(s->userData);
				if (sInfo->entID)
				{
					CKObject *obj = (CKObject*)GetPMan()->m_Context->GetObject(sInfo->entID);
					if (obj)
					{
						
						shapes->AddObject((CKBeObject*)obj);
					}
				}
			}
		}
	}

	int op=2;




	return result;

}
void CPhysicsActor::SetScale( Vec3& scale )
{
	// do not scale if scale is currently 1:1 or if the scale
	// has not changed
	if( scale.x == 1 && scale.y == 1 && scale.z == 1 ||
		scale == m_CurrentScale )
	{
		return;
	}

	// make sure the scale is valid
	// No 0 scales or negative scales!
	if( scale.x <= 0 && scale.y <= 0 && scale.z <= 0 )
	{
		m_ToolBox->Log( LOGWARNING, _T("CPhysicsActor::SetScale() Invalid scale!\n" ) );
		return;
	}

	NxVec3 newScale( scale.x, scale.y, scale.z );

	// unscale the old scale
	// Loop through shapes in the actor
	unsigned int numShapes = m_Actor->getNbShapes();
	NxShape*const* shapes = m_Actor->getShapes();
   	NxShape* currentShape;

	NxVec3 shapeLocalPosition;

	// for each shape type scale its dimensions
	while( numShapes-- >= 1 )
	{
		currentShape = shapes[numShapes];

		// get the shape's type
		NxShapeType type = currentShape->getType();

		switch( type )
		{
			case NX_SHAPE_BOX:
				{
					// do something
					NxBoxShape* shape = (NxBoxShape*)currentShape;
					// rescale box dimensions
					NxVec3 dimensions = shape->getDimensions();
					
					Vec3 newDimensions(dimensions.x, dimensions.y, dimensions.z);
					RescaleVector( newDimensions, scale );

					// set the shape data with the newly rescaled dimensions
					shape->setDimensions( NxVec3(newDimensions.x, newDimensions.y, newDimensions.z) );
					break;
				}
			case NX_SHAPE_SPHERE:
				{
					// do something
					NxSphereShape* shape = (NxSphereShape*)currentShape;
					float radius = shape->getRadius();
					radius /= m_CurrentScale.x;
					radius *= newScale.x;
					// set the shape data with the newly rescaled dimensions
					shape->setRadius( radius );
					break;
				}
			case NX_SHAPE_CAPSULE:
				{
					// do something
					NxCapsuleShape* shape;
					shape = (NxCapsuleShape*)currentShape;
					// rescale radius
					float radius = shape->getRadius();
					radius /= m_CurrentScale.x;
					radius *= newScale.x;
					// rescale height
					float height = shape->getHeight();
					height /= m_CurrentScale.z;
					height *= newScale.z;
					// set the shape data with the newly rescaled dimensions
					shape->setRadius( radius );
					shape->setHeight( height );
					break;
				}
			default:
				m_ToolBox->Log( LOGWARNING, _T("CPhysicsObject::SetScale() Attempting to scale on unsupported shape!\n" ) );
				return;
		}

		// get the shape's local position and rescale it
		shapeLocalPosition = currentShape->getLocalPosition();

		Vec3 newShapeLocalPosition(shapeLocalPosition.x, shapeLocalPosition.y, shapeLocalPosition.z);
		RescaleVector( newShapeLocalPosition, scale );

		currentShape->setLocalPosition( NxVec3(newShapeLocalPosition.x, newShapeLocalPosition.y, newShapeLocalPosition.z) );
	}

	// Set the current scale to the new scale so we can unscale the scale
	m_CurrentScale = scale;
}
void CPhysicsActor::AddVisualization()
{
	// get the CPhysicsObject's name
	PHYSICSUSERDATA* userData = (PHYSICSUSERDATA*)m_Actor->userData;
	
	if( userData == NULL )
		return;

	CPhysicsObject* physObj = userData->physObj;
	IHashString* cpoName = physObj->GetParentName();

	// Loop through shapes in the actor
	unsigned int numShapes = m_Actor->getNbShapes();
	NxShape*const* shapes = m_Actor->getShapes();
   	NxShape* shape;

	// we need to unscale before feeding it to the shape objects since they
	// get the scale from the parent
	Vec3 invScale;
	invScale.x = 1.0f / m_CurrentScale.x;
	invScale.y = 1.0f / m_CurrentScale.y;
	invScale.z = 1.0f / m_CurrentScale.z;

	// Add visualizations for each shape
	while( numShapes-- )
	{
		shape = shapes[numShapes];
   		
		// Add shape to be rendered
		if( shape->isBox() )
		{
			NxBoxShape* boxShape = (NxBoxShape*)shape;

			Matrix4x4 localTransform;
			localTransform.SetIdentity();

			float tempRot[9];
			boxShape->getLocalOrientation().getColumnMajor( tempRot );
			localTransform.SetFrom3x3( tempRot );

			NxVec3 tempPos = boxShape->getLocalPosition();
			tempPos.x *= invScale.x;
			tempPos.y *= invScale.y;
			tempPos.z *= invScale.z;
			localTransform.SetTranslation( Vec3(tempPos.x, tempPos.y, tempPos.z) );

			NxVec3 boxDimensions = boxShape->getDimensions();
			float halfXDimension = boxDimensions.x * invScale.x;
			float halfYDimension = boxDimensions.y * invScale.y;
			float halfZDimension = boxDimensions.z * invScale.z;

			// Add a debug render object to visualize the object
			ADDOBJECTORIENTEDBOXPARAMS oobbParams;
			oobbParams.name = cpoName;
			oobbParams.min = Vec3( -halfXDimension, -halfYDimension, -halfZDimension );
			oobbParams.max = Vec3( halfXDimension, halfYDimension, halfZDimension );
			oobbParams.localTransform = localTransform;
			static DWORD msgHash_AddObjectOrientedBox = CHashString(_T("AddObjectOrientedBox")).GetUniqueID();
			m_ToolBox->SendMessage(msgHash_AddObjectOrientedBox, sizeof(ADDOBJECTORIENTEDBOXPARAMS), &oobbParams );
		}
		if( shape->isSphere() )
		{
			NxSphereShape* sphereShape = (NxSphereShape*)shape;

			float radius = sphereShape->getRadius();

			// Add a debug render object to visualize the object
			ADDSPHEREPARAMS sphereParams;
			sphereParams.name = cpoName;
			sphereParams.radius = radius * invScale.x;
			sphereParams.red = 0;
			sphereParams.green = 255; // making the sphere green to distinguish it from AABBs
			sphereParams.blue = 0;
			static DWORD msgHash_AddSphere = CHashString(_T("AddSphere")).GetUniqueID();
			m_ToolBox->SendMessage(msgHash_AddSphere, sizeof(ADDSPHEREPARAMS), &sphereParams );
		}
		if( shape->isCapsule() )
		{
			// Draw as a red box for now
			NxCapsuleShape* capsuleShape = (NxCapsuleShape*)shape;

			Matrix4x4 localTransform;
			localTransform.SetIdentity();

			float tempRot[9];
			capsuleShape->getLocalOrientation().getColumnMajor( tempRot );
			localTransform.SetFrom3x3( tempRot );

			NxVec3 tempPos = capsuleShape->getLocalPosition();
			tempPos.x *= invScale.x;
			tempPos.y *= invScale.y;
			tempPos.z *= invScale.z;
			localTransform.SetTranslation( Vec3(tempPos.x, tempPos.y, tempPos.z) );

			float halfXDimension = capsuleShape->getRadius();
			float halfYDimension = capsuleShape->getHeight() - capsuleShape->getRadius();
			float halfZDimension = capsuleShape->getRadius();

			// Add a debug render object to visualize the object
			ADDOBJECTORIENTEDBOXPARAMS oobbParams;
			oobbParams.name = cpoName;
			oobbParams.min = Vec3( -halfXDimension, -halfYDimension, -halfZDimension );
			oobbParams.max = Vec3( halfXDimension, halfYDimension, halfZDimension );
			oobbParams.localTransform = localTransform;
			oobbParams.red = 255;
			oobbParams.green = 0;
			oobbParams.blue = 0;
			static DWORD msgHash_AddObjectOrientedBox = CHashString(_T("AddObjectOrientedBox")).GetUniqueID();
			m_ToolBox->SendMessage(msgHash_AddObjectOrientedBox, sizeof(ADDOBJECTORIENTEDBOXPARAMS), &oobbParams );
		}
		if( shape->isConvexMesh() )
		{
			// not yet implemented
		}
	}
}
示例#7
0
void drawActor( NxActor *inActor )
{
   GFXDrawUtil *drawer = GFX->getDrawUtil();
   //drawer->setZRead( false );

   // Determine alpha we render shapes with.
   const U8 enabledAlpha = 255;
   const U8 disabledAlpha = 100;
   U8 renderAlpha = inActor->readActorFlag( NX_AF_DISABLE_COLLISION ) ? disabledAlpha : enabledAlpha;

   // Determine color we render actors and shapes with.
   ColorI actorColor( 0, 0, 255, 200 );   
   ColorI shapeColor = ( inActor->isSleeping() ? ColorI( 0, 0, 255, renderAlpha ) : ColorI( 255, 0, 255, renderAlpha ) );      

   MatrixF actorMat(true);
   inActor->getGlobalPose().getRowMajor44( actorMat );

   GFXStateBlockDesc desc;
   desc.setBlend( true );
   desc.setZReadWrite( true, false );
   desc.setCullMode( GFXCullNone );

   // Draw an xfm gizmo for the actor's globalPose...
   //drawer->drawTransform( desc, actorMat, Point3F::One, actorColor );
   
   // Loop through and render all the actor's shapes....

   NxShape *const*pShapeArray = inActor->getShapes();
   U32 numShapes = inActor->getNbShapes();

   for ( U32 i = 0; i < numShapes; i++ )
   {
      const NxShape *shape = pShapeArray[i];

      Point3F shapePos = pxCast<Point3F>( shape->getGlobalPosition() );
      MatrixF shapeMat(true);
      shape->getGlobalPose().getRowMajor44(shapeMat);
      shapeMat.setPosition( Point3F::Zero );

      switch ( shape->getType() )
      {
         case NX_SHAPE_SPHERE:
         {
            NxSphereShape *sphere = (NxSphereShape*)shape;     
            drawer->drawSphere( desc, sphere->getRadius(), shapePos, shapeColor );

            break;
         }
         case NX_SHAPE_BOX:
         {
            NxBoxShape *box = (NxBoxShape*)shape;
            Point3F size = pxCast<Point3F>( box->getDimensions() );            
            drawer->drawCube( desc, size*2, shapePos, shapeColor, &shapeMat );            
            break;
         }
         case NX_SHAPE_CAPSULE:
         {
            shapeMat.mul( MatrixF( EulerF( mDegToRad(90.0f), mDegToRad(90.0f), 0 ) ) );

            NxCapsuleShape *capsule = (NxCapsuleShape*)shape;
            drawer->drawCapsule( desc, shapePos, capsule->getRadius(), capsule->getHeight(), shapeColor, &shapeMat );

            break;
         }
         default:
         {
            break;
         }
      }
   }

   //drawer->clearZDefined();
}