float NxPhysicsCapsuleShape::GetRadius() { NxCapsuleShape * CapsuleShape = static_cast<NxCapsuleShape *>( mShape ); return CapsuleShape->getRadius(); }
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 } } }
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 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(); }