pRigidBody*pFactory::createBox(CK3dEntity *referenceObject,CK3dEntity *worldReferenceObject,pObjectDescr*descr,int creationFlags) { pWorld *world=getManager()->getWorld(worldReferenceObject,referenceObject); pRigidBody*result = world->getBody(referenceObject); if(result) { result->destroy(); delete result; result = NULL; } // we create our final body in the given world : result = createBody(referenceObject,worldReferenceObject); if (result) { result->setWorld(world); using namespace vtTools::AttributeTools; result->setFlags(descr->flags); result->setHullType(descr->hullType); result->setDataFlags(0x000); result->checkDataFlags(); if (result->getSkinWidth()==-1.0f) { result->setSkinWidth(0.01f); } VxMatrix v_matrix ; VxVector position,scale; VxQuaternion quat; v_matrix = referenceObject->GetWorldMatrix(); Vx3DDecomposeMatrix(v_matrix,quat,position,scale); VxVector box_s= BoxGetZero(referenceObject); NxVec3 pos = pMath::getFrom(position); NxQuat rot = pMath::getFrom(quat); float density = result->getDensity(); float radius = referenceObject->GetRadius(); if (referenceObject->GetRadius() < 0.001f ) { radius = 1.0f; } ////////////////////////////////////////////////////////////////////////// //create actors description NxActorDesc actorDesc;actorDesc.setToDefault(); NxBodyDesc bodyDesc;bodyDesc.setToDefault(); ////////////////////////////////////////////////////////////////////////// NxMaterialDesc *materialDescr = NULL; NxMaterial *material = NULL; if (isFlagOn(result->getDataFlags(),EDF_MATERIAL_PARAMETER)) { NxMaterialDesc entMatNull;entMatNull.setToDefault(); NxMaterialDesc *entMat = createMaterialFromEntity(referenceObject); material = world->getScene()->createMaterial(entMatNull); material->loadFromDesc(*entMat); result->setMaterial(material); }else{ if (world->getDefaultMaterial()) { result->setMaterial(world->getDefaultMaterial()); } } ////////////////////////////////////////////////////////////////////////// NxBoxShapeDesc boxShape; if (creationFlags & E_OFC_DIMENSION ) { boxShape.dimensions = pMath::getFrom(box_s)*0.5f; } boxShape.density = descr->density; boxShape.materialIndex = result->getMaterial()->getMaterialIndex(); if (result->getSkinWidth()!=-1.0f) boxShape.skinWidth = result->getSkinWidth(); actorDesc.shapes.pushBack(&boxShape); ////////////////////////////////////////////////////////////////////////// //dynamic object ? if (result->getFlags() & BF_Moving){ actorDesc.body = &bodyDesc; } else actorDesc.body = NULL; ////////////////////////////////////////////////////////////////////////// //set transformations actorDesc.density = descr->density; if (creationFlags & E_OFC_POSITION) { actorDesc.globalPose.t = pos; } if (creationFlags & E_OFC_POSITION) { actorDesc.globalPose.M = rot; } ////////////////////////////////////////////////////////////////////////// //create the actor int v = actorDesc.isValid(); NxActor *actor = world->getScene()->createActor(actorDesc); if (!actor) { xLogger::xLog(ELOGERROR,E_LI_AGEIA,"Couldn't create actor"); delete result; return NULL; } ////////////////////////////////////////////////////////////////////////// //set additional settings : result->setActor(actor); actor->setName(referenceObject->GetName()); actor->userData= result; if (result->getFlags() & BF_Moving) { VxVector massOffsetOut;referenceObject->Transform(&massOffsetOut,&result->getMassOffset()); actor->setCMassOffsetGlobalPosition(pMath::getFrom(massOffsetOut)); } if (result->getFlags() & BF_Kinematic) { actor->raiseBodyFlag(NX_BF_KINEMATIC); } result->enableCollision((result->getFlags() & BF_Collision)); if (result->getFlags() & BF_Moving) { if (!(result->getFlags() & BF_Gravity)) { actor->raiseBodyFlag(NX_BF_DISABLE_GRAVITY); } } NxShape *shape = result->getShapeByIndex(); if (shape) { pSubMeshInfo *sInfo = new pSubMeshInfo(); sInfo->entID = referenceObject->GetID(); sInfo->refObject = (CKBeObject*)referenceObject; shape->userData = (void*)sInfo; result->setMainShape(shape); shape->setName(referenceObject->GetName()); } } return result; }
pRigidBody*pFactory::createRigidBody(CK3dEntity *referenceObject,pObjectDescr& oDescr) { CK3dEntity *worldReferenceObject = (CK3dEntity*)GetPMan()->GetContext()->GetObject(oDescr.worlReference); pWorld *world=getManager()->getWorld(worldReferenceObject,referenceObject); if (!world) { xLogger::xLog(XL_START,ELOGWARNING,E_LI_MANAGER,"world object invalid, setting to default world"); world = GetPMan()->getDefaultWorld(); } pRigidBody*result = world->getBody(referenceObject); // create minimum object result = createBody(referenceObject,worldReferenceObject); if (!result) { xLogger::xLog(XL_START,ELOGERROR,E_LI_MANAGER,"failed"); return NULL; } result->setWorld(world); result->setFlags(oDescr.flags); result->setHullType(oDescr.hullType); if (oDescr.density <= 0.001f) oDescr.density = 1.0f; if (oDescr.skinWidth <= 0.001f) oDescr.skinWidth = 0.025f; result->setDensity(oDescr.density); bool hierarchy = (oDescr.flags & BF_Hierarchy); bool isDeformable = oDescr.flags & BF_Deformable; bool trigger = oDescr.flags & BF_TriggerShape; //################################################################ // // Deformable ? // pCloth *cloth = NULL; pClothDesc cDescr; if (isDeformable) { xLogger::xLog(XL_START,ELOGERROR,E_LI_MANAGER,"deformable feature disabled in this release"); return NULL; cDescr.setToDefault(); cDescr.worldReference = worldReferenceObject->GetID(); if ( result->getFlags() & BF_Gravity ) { cDescr.flags |= PCF_Gravity; } if ( result->getFlags() & BF_Collision ) { } if (!cloth) { cloth = pFactory::Instance()->createCloth(referenceObject,cDescr); if (!cloth) { xLogger::xLog(XL_START,ELOGERROR,E_LI_MANAGER,"Cannot create cloth : factory object failed !"); } } } float density = oDescr.density; VxVector box_s= BoxGetZero(referenceObject); VxMatrix v_matrix ; VxVector position,scale; VxQuaternion quat; v_matrix = referenceObject->GetWorldMatrix(); Vx3DDecomposeMatrix(v_matrix,quat,position,scale); NxVec3 pos = pMath::getFrom(position); NxQuat rot = pMath::getFrom(quat); //---------------------------------------------------------------- // // Fill NxActorDescr // NxActorDesc actorDesc;actorDesc.setToDefault(); NxBodyDesc bodyDesc;bodyDesc.setToDefault(); // // Fix parameters to default values actorDesc.density = oDescr.density; //skin width if (oDescr.skinWidth<=0.0001f) oDescr.skinWidth=0.025f; float radius = result->GetVT3DObject()->GetRadius(); //---------------------------------------------------------------- // // Create the physic mesh. Externalizing this procedure will corrupt the // actor description object. // switch(oDescr.hullType) { ////////////////////////////////////////////////////////////////////////// case HT_Box: { NxBoxShapeDesc shape; if (! (oDescr.flags & BF_Deformable) ) { shape.dimensions = pMath::getFrom(box_s)*0.5f; } shape.density = density; shape.skinWidth = oDescr.skinWidth; actorDesc.shapes.pushBack(&shape); break; } ////////////////////////////////////////////////////////////////////////// case HT_Sphere: { NxSphereShapeDesc shape; if (! (oDescr.flags & BF_Deformable) ) { shape.radius = radius; } shape.density = density; shape.skinWidth = oDescr.skinWidth; actorDesc.shapes.pushBack(&shape); break; } ////////////////////////////////////////////////////////////////////////// case HT_Mesh: { NxTriangleMeshDesc myMesh; myMesh.setToDefault(); pFactory::Instance()->createMesh(result->getWorld()->getScene(),result->GetVT3DObject()->GetCurrentMesh(),myMesh); NxTriangleMeshShapeDesc shape; bool status = InitCooking(); if (!status) { xLogger::xLog(ELOGERROR,E_LI_AGEIA,"Couldn't initiate cooking lib!"); return false; } MemoryWriteBuffer buf; status = CookTriangleMesh(myMesh, buf); if (!status) { xLogger::xLog(ELOGERROR,E_LI_AGEIA,"Couldn't cook mesh!"); return false; } shape.meshData = GetPMan()->getPhysicsSDK()->createTriangleMesh(MemoryReadBuffer(buf.data)); shape.density = density; shape.group = oDescr.collisionGroup; actorDesc.shapes.pushBack(&shape); shape.skinWidth = oDescr.skinWidth; CloseCooking(); if (myMesh.points) { delete [] myMesh.points; } if (myMesh.triangles) { delete []myMesh.triangles; } break; } ////////////////////////////////////////////////////////////////////////// case HT_ConvexMesh: { if (result->GetVT3DObject()->GetCurrentMesh()) { if (result->GetVT3DObject()->GetCurrentMesh()->GetVertexCount()>=256 ) { xLogger::xLog(ELOGERROR,E_LI_AGEIA,"Only 256 vertices for convex meshes allowed, by Ageia!"); return false; } }else { xLogger::xLog(ELOGERROR,E_LI_AGEIA,"Object has no mesh!"); return false; } NxConvexMeshDesc myMesh; myMesh.setToDefault(); pFactory::Instance()->createConvexMesh(result->getWorld()->getScene(),result->GetVT3DObject()->GetCurrentMesh(),myMesh); NxConvexShapeDesc shape; bool status = InitCooking(); if (!status) { xLogger::xLog(XL_START,ELOGERROR,E_LI_AGEIA,"Couldn't initiate cooking lib!"); return false; } MemoryWriteBuffer buf; status = CookConvexMesh(myMesh, buf); if (!status) { xLogger::xLog(XL_START,ELOGERROR,E_LI_AGEIA,"Couldn't cook convex mesh!"); return false; } shape.meshData = GetPMan()->getPhysicsSDK()->createConvexMesh(MemoryReadBuffer(buf.data)); shape.density = density; actorDesc.shapes.pushBack(&shape); if (oDescr.skinWidth!=-1.0f) shape.skinWidth = oDescr.skinWidth; int h = shape.isValid(); CloseCooking(); if (myMesh.points) { delete [] myMesh.points; } if (myMesh.triangles) { delete []myMesh.triangles; } break; } ////////////////////////////////////////////////////////////////////////// case HT_ConvexCylinder: { NxConvexShapeDesc shape; pConvexCylinderSettings &cSettings = oDescr.convexCylinder; iAssertW( (oDescr.mask & OD_ConvexCylinder),pFactory::Instance()->findSettings(cSettings,result->GetVT3DObject()), "Hull type has been set to convex cylinder but there is no descriptions passed or activated in the pObjectDescr::mask.Trying object attributes...."); if (cSettings.radius.reference) cSettings.radius.evaluate(cSettings.radius.reference); if (cSettings.height.reference) cSettings.height.evaluate(cSettings.height.reference); iAssertW( cSettings.isValid() , cSettings.setToDefault(),""); cSettings.radius.value = cSettings.radius.value > 0.0f ? (cSettings.radius.value*0.5) : (box_s.v[cSettings.radius.referenceAxis] * 0.5f); cSettings.height.value = cSettings.height.value > 0.0f ? cSettings.height.value : (box_s.v[cSettings.height.referenceAxis] * 0.5f); bool resultAssert = true; iAssertWR( pFactory::Instance()->_createConvexCylinderMesh(&shape,cSettings,result->GetVT3DObject()),"",resultAssert); shape.density = density; shape.skinWidth = oDescr.skinWidth; actorDesc.shapes.pushBack(&shape); break; } ////////////////////////////////////////////////////////////////////////// case HT_Capsule: { NxCapsuleShapeDesc shape; pCapsuleSettingsEx &cSettings = oDescr.capsule; if (!( oDescr.mask & OD_Capsule) ) { // try over attribute : pFactory::Instance()->findSettings(cSettings,result->GetVT3DObject()); } bool resultAssert = true; if (cSettings.radius.reference) cSettings.radius.evaluate(cSettings.radius.reference); if (cSettings.height.reference) cSettings.height.evaluate(cSettings.height.reference); iAssertWR(cSettings.isValid(),cSettings.setToDefault(),resultAssert); shape.radius = cSettings.radius.value > 0.0f ? (cSettings.radius.value*0.5) : (box_s.v[cSettings.radius.referenceAxis] * 0.5f); shape.height = cSettings.height.value > 0.0f ? (cSettings.height.value-( 2*shape.radius)) : (box_s.v[cSettings.height.referenceAxis] - ( 2*shape.radius)) ; shape.density = density; shape.skinWidth = oDescr.skinWidth; actorDesc.shapes.pushBack(&shape); break; } case HT_Wheel: { xLogger::xLog(XL_START,ELOGERROR,E_LI_AGEIA,"Wheel shape can be sub shape only!"); return false; } } if ( !isDeformable) { actorDesc.globalPose.t = pos; actorDesc.globalPose.M = rot; } //---------------------------------------------------------------- // // Create the final NxActor // if (oDescr.flags & BF_Moving) actorDesc.body = &bodyDesc; NxActor *actor = world->getScene()->createActor(actorDesc); if (!actor) { xLogger::xLog(ELOGERROR,E_LI_AGEIA,"Couldn't create actor"); delete result; return NULL; } //---------------------------------------------------------------- // // // result->setActor(actor); actor->setName(referenceObject->GetName()); result->SetVT3DObject(referenceObject); actor->userData= result; ////////////////////////////////////////////////////////////////////////// //Deformable : if (isDeformable && cloth) { pDeformableSettings dSettings; dSettings.ImpulsThresold = 50.0f; dSettings.PenetrationDepth= 0.1f ; dSettings.MaxDeform = 2.0f; CKParameterOut *poutDS = referenceObject->GetAttributeParameter(GetPMan()->att_deformable); if (poutDS) { pFactory::Instance()->copyTo(dSettings,poutDS); } cloth->attachToCore(referenceObject,dSettings.ImpulsThresold,dSettings.PenetrationDepth,dSettings.MaxDeform); result->setCloth(cloth); } ////////////////////////////////////////////////////////////////////////// // // Extra settings : // if (result->getFlags() & BF_Moving) { VxVector massOffsetOut;referenceObject->Transform(&massOffsetOut,&oDescr.massOffsetLinear); actor->setCMassOffsetGlobalPosition(pMath::getFrom(massOffsetOut)); } if (result->getFlags() & BF_Kinematic) { result->setKinematic(true); } if (result->getFlags() & BF_Moving ) { result->enableGravity(result->getFlags() & BF_Gravity); } if (result->getFlags() & BF_Sleep ) { result->setSleeping(true); } if (oDescr.worlReference == 0) { if (GetPMan()->getDefaultWorld()) { oDescr.worlReference = GetPMan()->getDefaultWorld()->getReference()->GetID(); } } //---------------------------------------------------------------- // // store mesh meta info in the first main mesh // NxShape *shape = result->getShapeByIndex(); if (shape) { pSubMeshInfo *sInfo = new pSubMeshInfo(); sInfo->entID = referenceObject->GetID(); sInfo->refObject = (CKBeObject*)referenceObject; shape->userData = (void*)sInfo; result->setMainShape(shape); shape->setName(referenceObject->GetName()); } result->enableCollision( (result->getFlags() & BF_Collision), referenceObject ); //---------------------------------------------------------------- // // Adjust pivot // if ( (oDescr.mask & OD_Pivot) ) { iAssertW1( oDescr.pivot.isValid(),oDescr.pivot.setToDefault()); result->updatePivotSettings(oDescr.pivot,referenceObject); }else if(pFactory::Instance()->findSettings(oDescr.collision,referenceObject)) result->updatePivotSettings(oDescr.pivot,referenceObject); //---------------------------------------------------------------- // // Optimization // if ((oDescr.mask & OD_Optimization )) { iAssertW1( oDescr.optimization.isValid(),oDescr.optimization.setToDefault()); result->updateOptimizationSettings(oDescr.optimization); } else{ if(pFactory::Instance()->findSettings(oDescr.optimization,referenceObject)) { iAssertW1( oDescr.optimization.isValid(),oDescr.optimization.setToDefault()); result->updateOptimizationSettings(oDescr.optimization); } } //---------------------------------------------------------------- // // Collision // if ((oDescr.mask & OD_Collision)) result->updateCollisionSettings(oDescr.collision,referenceObject); else if(pFactory::Instance()->findSettings(oDescr.collision,referenceObject)) result->updateCollisionSettings(oDescr.collision,referenceObject); //---------------------------------------------------------------- // // Material // NxMaterialDesc *materialDescr = NULL; NxMaterial *material = NULL; pMaterial &bMaterial = oDescr.material; if (oDescr.mask & OD_Material) { result->updateMaterialSettings(bMaterial,referenceObject); }else { bool hasMaterial = pFactory::Instance()->findSettings(bMaterial,referenceObject); if (!hasMaterial) { if (world->getDefaultMaterial()) { int z = (int)world->getDefaultMaterial()->userData; shape->setMaterial(world->getDefaultMaterial()->getMaterialIndex()); } }else{ iAssertW( bMaterial.isValid(),bMaterial.setToDefault(), "Material settings were still invalid : "); NxMaterialDesc nxMatDescr; pFactory::Instance()->copyTo(nxMatDescr,bMaterial); NxMaterial *nxMaterial = world->getScene()->createMaterial(nxMatDescr); if (nxMaterial) { shape->setMaterial(nxMaterial->getMaterialIndex()); } } } xLogger::xLog(ELOGINFO,E_LI_MANAGER,"Rigid body creation successful : %s",referenceObject->GetName()); result->setInitialDescription(&oDescr); //---------------------------------------------------------------- // // Hierarchy mode fix : // if ( (oDescr.flags & BF_Hierarchy) ) oDescr.hirarchy = true; if ( oDescr.hirarchy ) oDescr.flags << BF_Hierarchy; if ( (!oDescr.hirarchy) || !(oDescr.flags & BF_Hierarchy) ) return result; //---------------------------------------------------------------- // // Parse hirarchy // CK3dEntity* subEntity = NULL; while (subEntity= referenceObject->HierarchyParser(subEntity) ) { pObjectDescr *subDescr = NULL; int attTypePBSetup = GetPMan()->getAttributeTypeByGuid(VTS_PHYSIC_ACTOR); if (subEntity->HasAttribute(attTypePBSetup)) { subDescr = new pObjectDescr(); CKParameterOut *par = subEntity->GetAttributeParameter(attTypePBSetup); IParameter::Instance()->copyTo(subDescr,par); subDescr->version = pObjectDescr::E_OD_VERSION::OD_DECR_V1; } if (!subDescr) continue; if (subDescr->flags & BF_SubShape) { ////////////////////////////////////////////////////////////////////////// // // Regular Mesh : // if (subDescr->hullType != HT_Cloth) { //result->addSubShape(NULL,*subDescr,subEntity); VxQuaternion refQuad;subEntity->GetQuaternion(&refQuad,referenceObject); VxVector relPos;subEntity->GetPosition(&relPos,referenceObject); shape = pFactory::Instance()->createShape(referenceObject,*subDescr,subEntity,subEntity->GetCurrentMesh(),relPos,refQuad); //NxShape *shape = result->getSubShape(subEntity); if (shape) { //---------------------------------------------------------------- // // check for collision setup // //try to get get from child attributes first /* if(pFactory::Instance()->findSettings(subDescr->collision,subEntity)) { result->updateCollisionSettings(subDescr->collision,subEntity); continue; } if ( (subDescr->mask & OD_Optimization) ) { //if (pFactory::Instance()->findSettings(subDescr->collision,subEntity)) result->updateCollisionSettings(subDescr->collision,subEntity); } else if ( (oDescr.mask & OD_Optimization) ) { result->updateCollisionSettings(oDescr.collision,subEntity); }else if(pFactory::Instance()->findSettings(subDescr->collision,subEntity)) { result->updateCollisionSettings(subDescr->collision,subEntity); } */ } } ////////////////////////////////////////////////////////////////////////// // // Cloth Mesh : // if (subDescr->hullType == HT_Cloth) { //pClothDesc *cloth = pFactory::Instance()->createPObjectDescrFromParameter(subEntity->GetAttributeParameter(GetPMan()->GetPAttribute())); } } } //---------------------------------------------------------------- // // Adjust mass // //---------------------------------------------------------------- // // Collision // if ((oDescr.mask & OD_Mass)) result->updateMassSettings(oDescr.mass); else if(pFactory::Instance()->findSettings(oDescr.mass,referenceObject)) result->updateMassSettings(oDescr.mass); return result; nothing: return result; }
void NxRobot::cloneRobot(int indexNewScene, int indexNewRobot, NxVec3 newPosition, int indexNewTeam) { NxRobot* nxRobotSource = Simulation::gScenes[this->indexScene]->allRobots->getRobotByIdByTeam(this->id, this->idTeam); NxActor* robotActor = Simulation::cloneActor(nxRobotSource->getActor(),indexNewScene); //NxBounds3 bodyBounds; //robotShapes[0]->getWorldBounds(bodyBounds); NxVehicleDesc vehicleDesc; vehicleDesc.position = NxVec3(robotActor->getGlobalPosition()); vehicleDesc.mass = robotActor->getMass(); //vehicleDesc.motorForce = 70000; //vehicleDesc.maxVelocity = 300.f; //vehicleDesc.cameraDistance = 8.0f; vehicleDesc.centerOfMass.set(robotActor->getCMassLocalPosition()); //vehicleDesc.differentialRatio = 3.42f; //vehicleDesc.transmissionEfficiency vehicleDesc.actor = robotActor; //Motor descricao //NxVehicleMotorDesc motorsDesc[4]; //for(NxU32 i=0;i<4;i++) //{ //motorsDesc[i].setToCorvette(); //vehicleDesc.motorsDesc.push_back(&motorsDesc[i]); //} //Roda (Wheel) descricao int numberWheels = nxRobotSource->getNbWheels(); NxWheelDesc* wheelDesc = new NxWheelDesc[numberWheels]; for(NxU32 i=0; i<numberWheels; i++) { //NxActor* wheelModel = Simulation::getActorWheel(indexSourceScene,indexNewRobot,i); //NxActorDesc wheelActorDesc; //wheelModel->saveToDesc(wheelActorDesc); //Simulation::gScenes[0]->releaseActor(*wheelModel); //NxShape*const* wheelShapes = actorWheel->getShapes(); //NxBounds3 wheelBounds; //wheelShapes[0]->getWorldBounds(wheelBounds); const NxWheel* wheel = nxRobotSource->getWheel(i); NxWheelShape* wheelShape = ((NxWheel2*)wheel)->getWheelShape(); //wheelDesc[i].wheelApproximation = 10; wheelDesc[i].wheelOrientation = wheelShape->getGlobalOrientation(); wheelDesc[i].position.set(wheelShape->getGlobalPosition()-robotActor->getGlobalPosition()); //wheelDesc[i].position.z = 0; wheelDesc[i].id = i; wheelDesc[i].wheelFlags = ((NxWheel*)wheel)->getWheelFlags(); wheelDesc[i].wheelRadius = wheel->getRadius(); //wheelDesc[i].wheelWidth = 100; wheelDesc[i].wheelSuspension = wheelShape->getSuspensionTravel(); wheelDesc[i].springRestitution = 0; wheelDesc[i].springDamping = 0; wheelDesc[i].springBias = 0.0f; wheelDesc[i].maxBrakeForce = 100; wheelDesc[i].frictionToFront = 0.1f;//0.1f; wheelDesc[i].frictionToSide = 0;//0.02f;// wheelDesc[i].inverseWheelMass = wheelShape->getInverseWheelMass(); //TODO: CONFIGURAR PARÂMETRO //Angulo das Rodas (Omni) wheelDesc[i].angWheelRelRobot = ((NxWheel2*)wheel)->angWheelRelRobot; vehicleDesc.robotWheels.pushBack(&wheelDesc[i]); } //Criar robot, vehicle base NxRobot* robot = (NxRobot*)NxRobot::createVehicle(Simulation::gScenes[indexNewScene], &vehicleDesc); //NxRobot* robot = (NxRobot*)NxRobot::createVehicle(gScenes[indexSourceScene], &vehicleDesc); robot->setId(indexNewRobot); robot->setIdTeam(indexNewTeam); robot->indexScene = indexNewScene; robot->bodyRadius = nxRobotSource->bodyRadius; //Dribbler and Kicker NxShape*const* robotShapes = robotActor->getShapes(); for(int i=0; i<robotActor->getNbShapes(); i++) { const char* shapeName = robotShapes[i]->getName(); if(shapeName) { char* dribblerName = new char[10];//"Driblador\0" dribblerName[9] = 0; memcpy(dribblerName, shapeName, strlen(dribblerName)); if(strcmp(dribblerName, "Driblador") == 0) { robot->dribbler->dribblerShapes.push_back(robotShapes[i]); } delete dribblerName; } } robot->kicker->kickerShapeDesc = new NxBoxShapeDesc(); NxShapeDesc* shapeDesc = nxRobotSource->kicker->kickerShapeDesc; robot->kicker->kickerShapeDesc->localPose = shapeDesc->localPose; ((NxBoxShapeDesc*)(robot->kicker->kickerShapeDesc))->dimensions = ((NxBoxShapeDesc*)shapeDesc)->dimensions; //Initial Pose robot->setInitialPose(robotActor->getGlobalPose()); robotActor->putToSleep(); //Transladando o robo robot->getActor()->setGlobalPosition(newPosition); string label; string plabel = "Robo"; stringstream out; out << indexNewRobot; out << "-"; out << indexNewTeam; //out << "-"; //out << indexNewScene; label.append(plabel); label.append(out.str()); char* arrayLabel = new char[label.size()+1]; arrayLabel[label.size()]=0; memcpy(arrayLabel, label.c_str(), label.size()); robotActor->setName(arrayLabel); //delete arrayLabel; }
pRigidBody*pFactory::createRigidBodyFull(CK3dEntity *referenceObject, CK3dEntity *worldReferenceObject) { //################################################################ // // Sanity checks // pWorld *world=getManager()->getWorld(worldReferenceObject,referenceObject); pRigidBody*result = world->getBody(referenceObject); if(result) { result->destroy(); delete result; result = NULL; } //################################################################ // // Construct the result // result = createBody(referenceObject,worldReferenceObject); if (result) { result->setWorld(world); //---------------------------------------------------------------- // // Handle different attribute types (Object or pBSetup) // int attTypeOld = GetPMan()->GetPAttribute(); int attTypeNew = GetPMan()->getAttributeTypeByGuid(VTS_PHYSIC_ACTOR); pObjectDescr *oDescr = NULL; //---------------------------------------------------------------- // // the old way : // if (referenceObject->HasAttribute(attTypeOld)) { result->retrieveSettingsFromAttribute(); oDescr = pFactory::Instance()->createPObjectDescrFromParameter(referenceObject->GetAttributeParameter(GetPMan()->GetPAttribute())); } bool hierarchy = result->getFlags() & BF_Hierarchy; bool isDeformable = result->getFlags() & BF_Deformable; bool trigger = (result->getFlags() & BF_TriggerShape); //---------------------------------------------------------------- // // the new way // if (referenceObject->HasAttribute(attTypeNew)) { oDescr = new pObjectDescr(); } result->checkDataFlags(); /* pObjectDescr *oDescr = if (!oDescr) return result; */ //################################################################ // // Older versions have the hierarchy mode settings not the body flags // We migrate it : // if (oDescr->hirarchy) { result->setFlags( (result->getFlags() | BF_Hierarchy )); } if (hierarchy) { oDescr->hirarchy = hierarchy; } //################################################################ // // Deformable ? // pCloth *cloth = NULL; pClothDesc cDescr; if (isDeformable) { cDescr.setToDefault(); cDescr.worldReference = worldReferenceObject->GetID(); if ( result->getFlags() & BF_Gravity ) { cDescr.flags |= PCF_Gravity; } if ( result->getFlags() & BF_Collision ) { } if (!cloth) { cloth = pFactory::Instance()->createCloth(referenceObject,cDescr); if (!cloth) { xLogger::xLog(XL_START,ELOGERROR,E_LI_MANAGER,"Cannot create cloth : factory object failed !"); } } } //################################################################ // // Some settings // if (result->getSkinWidth()==-1.0f) { result->setSkinWidth(0.01f); } float radius = referenceObject->GetRadius(); if (referenceObject->GetRadius() < 0.001f ) { radius = 1.0f; } float density = result->getDensity(); VxVector box_s= BoxGetZero(referenceObject); //################################################################ // // Calculate destination matrix // VxMatrix v_matrix ; VxVector position,scale; VxQuaternion quat; v_matrix = referenceObject->GetWorldMatrix(); Vx3DDecomposeMatrix(v_matrix,quat,position,scale); NxVec3 pos = pMath::getFrom(position); NxQuat rot = pMath::getFrom(quat); ////////////////////////////////////////////////////////////////////////// //create actors description NxActorDesc actorDesc;actorDesc.setToDefault(); NxBodyDesc bodyDesc;bodyDesc.setToDefault(); NxMaterialDesc *materialDescr = NULL; NxMaterial *material = NULL; switch(result->getHullType()) { ////////////////////////////////////////////////////////////////////////// case HT_Box: { NxBoxShapeDesc shape; if (! isDeformable ) { shape.dimensions = pMath::getFrom(box_s)*0.5f; } shape.density = density; //shape.localPose.t = pMath::getFrom(shapeOffset); if (result->getSkinWidth()!=-1.0f) shape.skinWidth = result->getSkinWidth(); actorDesc.shapes.pushBack(&shape); break; } ////////////////////////////////////////////////////////////////////////// case HT_Sphere: { NxSphereShapeDesc shape; if (! isDeformable ) { shape.radius = radius; } shape.density = density; //shape.localPose.t = pMath::getFrom(shapeOffset); if (result->getSkinWidth()!=-1.0f) shape.skinWidth = result->getSkinWidth(); actorDesc.shapes.pushBack(&shape); break; } ////////////////////////////////////////////////////////////////////////// case HT_Mesh: { if (! (result->getFlags() & BF_Deformable) ) { //xLogger::xLog(ELOGERROR,E_LI_AGEIA,"Can not use a mesh as de"); //return NULL; } NxTriangleMeshDesc myMesh; myMesh.setToDefault(); createMesh(world->getScene(),referenceObject->GetCurrentMesh(),myMesh); NxTriangleMeshShapeDesc shape; bool status = InitCooking(); if (!status) { xLogger::xLog(ELOGERROR,E_LI_AGEIA,"Couldn't initiate cooking lib!"); return NULL; } MemoryWriteBuffer buf; status = CookTriangleMesh(myMesh, buf); if (!status) { xLogger::xLog(ELOGERROR,E_LI_AGEIA,"Couldn't cook mesh!"); return NULL; } shape.meshData = getManager()->getPhysicsSDK()->createTriangleMesh(MemoryReadBuffer(buf.data)); shape.density = density; // shape.materialIndex = result->getMaterial()->getMaterialIndex(); actorDesc.shapes.pushBack(&shape); //shape.localPose.t = pMath::getFrom(shapeOffset); if (result->getSkinWidth()!=-1.0f) shape.skinWidth = result->getSkinWidth(); CloseCooking(); if (myMesh.points) { delete [] myMesh.points; } if (myMesh.triangles) { delete []myMesh.triangles; } break; } ////////////////////////////////////////////////////////////////////////// case HT_ConvexMesh: { if (referenceObject->GetCurrentMesh()) { if (referenceObject->GetCurrentMesh()->GetVertexCount()>=256 ) { xLogger::xLog(ELOGERROR,E_LI_AGEIA,"Only 256 vertices for convex meshs allowed, by Ageia!"); goto nothing; } }else { xLogger::xLog(ELOGERROR,E_LI_AGEIA,"Object has no mesh!"); goto nothing; } NxConvexMeshDesc myMesh; myMesh.setToDefault(); createConvexMesh(world->getScene(),referenceObject->GetCurrentMesh(),myMesh); NxConvexShapeDesc shape; bool status = InitCooking(); if (!status) { xLogger::xLog(XL_START,ELOGERROR,E_LI_AGEIA,"Couldn't initiate cooking lib!"); goto nothing; } MemoryWriteBuffer buf; status = CookConvexMesh(myMesh, buf); if (!status) { xLogger::xLog(XL_START,ELOGERROR,E_LI_AGEIA,"Couldn't cook convex mesh!"); goto nothing; } shape.meshData = getManager()->getPhysicsSDK()->createConvexMesh(MemoryReadBuffer(buf.data)); shape.density = density; // shape.materialIndex = result->getMaterial()->getMaterialIndex(); actorDesc.shapes.pushBack(&shape); //shape.localPose.t = pMath::getFrom(shapeOffset); if (result->getSkinWidth()!=-1.0f) shape.skinWidth = result->getSkinWidth(); int h = shape.isValid(); CloseCooking(); if (myMesh.points) { delete [] myMesh.points; } if (myMesh.triangles) { delete []myMesh.triangles; } break; } ////////////////////////////////////////////////////////////////////////// case HT_ConvexCylinder: { NxConvexShapeDesc shape; if (!_createConvexCylinder(&shape,referenceObject)) xLogger::xLog(XL_START,ELOGERROR,E_LI_AGEIA,"Couldn't create convex cylinder mesh"); shape.density = density; if (result->getSkinWidth()!=-1.0f) shape.skinWidth = result->getSkinWidth(); actorDesc.shapes.pushBack(&shape); break; } ////////////////////////////////////////////////////////////////////////// case HT_Capsule: { NxCapsuleShapeDesc shape; if ( !isDeformable ) { pCapsuleSettings cSettings; pFactory::Instance()->findSettings(cSettings,referenceObject); shape.radius = cSettings.radius > 0.0f ? cSettings.radius : box_s.v[cSettings.localRadiusAxis] * 0.5f; shape.height = cSettings.height > 0.0f ? cSettings.height : box_s.v[cSettings.localLengthAxis] - ( 2*shape.radius) ; } shape.density = density; // shape.materialIndex = result->getMaterial()->getMaterialIndex(); // shape.localPose.t = pMath::getFrom(shapeOffset); if (result->getSkinWidth()!=-1.0f) shape.skinWidth = result->getSkinWidth(); actorDesc.shapes.pushBack(&shape); break; } case HT_Wheel: { // xLogger::xLog(XL_START,ELOGERROR,E_LI_AGEIA,"Couldn't create convex cylinder mesh"); /* NxWheelShapeDesc shape; shape.radius = box_s.z*0.5f; shape.density = density; if (result->getSkinWidth()!=-1.0f) shape.skinWidth = result->getSkinWidth(); if (referenceObject && referenceObject->HasAttribute(GetPMan()->att_wheelDescr )) { CKParameter *par = referenceObject->GetAttributeParameter(GetPMan()->att_wheelDescr ); if (par) { pWheelDescr *wheelDescr = pFactory::Instance()->copyTo(par); if (wheelDescr) { float heightModifier = (wheelDescr->wheelSuspension + radius ) / wheelDescr->wheelSuspension; shape.suspension.damper = wheelDescr->springDamping * heightModifier; shape.suspension.targetValue = wheelDescr->springBias * heightModifier; shape.suspensionTravel = wheelDescr->wheelSuspension; shape.lateralTireForceFunction.stiffnessFactor *= wheelDescr->frictionToSide; shape.longitudalTireForceFunction.stiffnessFactor*=wheelDescr->frictionToFront; shape.inverseWheelMass = 0.1; int isValid = shape.isValid(); actorDesc.shapes.pushBack(&shape); } }else { XString name = result->GetVT3DObject()->GetName(); name << " needs to have an additional wheel attribute attached ! "; xLogger::xLog(XL_START,ELOGERROR,E_LI_AGEIA,name.CStr()); } } */ break; } } ////////////////////////////////////////////////////////////////////////// //dynamic object ? if (result->getFlags() & BF_Moving){ actorDesc.body = &bodyDesc; } else actorDesc.body = NULL; ////////////////////////////////////////////////////////////////////////// //set transformations actorDesc.density = result->getDensity(); if ( !isDeformable) { actorDesc.globalPose.t = pos; actorDesc.globalPose.M = rot; } ////////////////////////////////////////////////////////////////////////// //create the actor int v = actorDesc.isValid(); NxActor *actor = world->getScene()->createActor(actorDesc); if (!actor) { xLogger::xLog(ELOGERROR,E_LI_AGEIA,"Couldn't create actor"); delete result; return NULL; } ////////////////////////////////////////////////////////////////////////// //set additional settings : result->setActor(actor); actor->setName(referenceObject->GetName()); actor->userData= result; ////////////////////////////////////////////////////////////////////////// //Deformable : if (isDeformable && cloth) { pDeformableSettings dSettings; dSettings.ImpulsThresold = 50.0f; dSettings.PenetrationDepth= 0.1f ; dSettings.MaxDeform = 2.0f; CKParameterOut *poutDS = referenceObject->GetAttributeParameter(GetPMan()->att_deformable); if (poutDS) { pFactory::Instance()->copyTo(dSettings,poutDS); } cloth->attachToCore(referenceObject,dSettings.ImpulsThresold,dSettings.PenetrationDepth,dSettings.MaxDeform); result->setCloth(cloth); } ////////////////////////////////////////////////////////////////////////// // // Extra settings : // if (result->getFlags() & BF_Moving) { VxVector massOffsetOut;referenceObject->Transform(&massOffsetOut,&result->getMassOffset()); actor->setCMassOffsetGlobalPosition(pMath::getFrom(massOffsetOut)); } if (result->getFlags() & BF_Kinematic) { result->setKinematic(true); } if (result->getFlags() & BF_Moving ) { result->enableGravity(result->getFlags() & BF_Gravity); } //---------------------------------------------------------------- // // Special Parameters // //- look for optimization attribute : result->checkForOptimization(); //---------------------------------------------------------------- // // store mesh meta info in the first main mesh // NxShape *shape = result->getShapeByIndex(); if (shape) { pSubMeshInfo *sInfo = new pSubMeshInfo(); sInfo->entID = referenceObject->GetID(); sInfo->refObject = (CKBeObject*)referenceObject; shape->userData = (void*)sInfo; result->setMainShape(shape); shape->setName(referenceObject->GetName()); pMaterial bMaterial; bool hasMaterial = pFactory::Instance()->findSettings(bMaterial,referenceObject); if (!hasMaterial) { if (world->getDefaultMaterial()) { int z = (int)world->getDefaultMaterial()->userData; shape->setMaterial(world->getDefaultMaterial()->getMaterialIndex()); //pFactory::Instance()->copyTo(bMaterial,world->getDefaultMaterial()); } }else{ NxMaterialDesc nxMatDescr; pFactory::Instance()->copyTo(nxMatDescr,bMaterial); NxMaterial *nxMaterial = world->getScene()->createMaterial(nxMatDescr); if (nxMaterial) { shape->setMaterial(nxMaterial->getMaterialIndex()); } } } result->enableCollision( (result->getFlags() & BF_Collision), referenceObject ); //- handle collisions setup if (oDescr->version == pObjectDescr::E_OD_VERSION::OD_DECR_V1) result->updateCollisionSettings(*oDescr,referenceObject); xLogger::xLog(ELOGINFO,E_LI_MANAGER,"Rigid body creation successful : %s",referenceObject->GetName()); //---------------------------------------------------------------- // // Parse hierarchy // if (!oDescr->hirarchy) return result; CK3dEntity* subEntity = NULL; while (subEntity= referenceObject->HierarchyParser(subEntity) ) { if (subEntity->HasAttribute(GetPMan()->GetPAttribute())) { pObjectDescr *subDescr = pFactory::Instance()->createPObjectDescrFromParameter(subEntity->GetAttributeParameter(GetPMan()->GetPAttribute())); if (subDescr->flags & BF_SubShape) { ////////////////////////////////////////////////////////////////////////// // // Regular Mesh : // if (subDescr->hullType != HT_Cloth) { result->addSubShape(NULL,*oDescr,subEntity); } ////////////////////////////////////////////////////////////////////////// // // Cloth Mesh : // if (subDescr->hullType == HT_Cloth) { //pClothDesc *cloth = pFactory::Instance()->createPObjectDescrFromParameter(subEntity->GetAttributeParameter(GetPMan()->GetPAttribute())); } } } } if (oDescr->hirarchy) { if (oDescr->newDensity!=0.0f || oDescr->totalMass!=0.0f ) { result->updateMassFromShapes(oDescr->newDensity,oDescr->totalMass); } } return result; } nothing: return result; }
void Simulation::buildModelRobot(int indexRobot, int indexScene, int indexTeam) { //Veiculo descricao //Body Descricao NxActor* robotActor = Simulation::getActorRobot(indexScene, indexRobot); //NxBounds3 bodyBounds; //robotShapes[0]->getWorldBounds(bodyBounds); NxVehicleDesc vehicleDesc; vehicleDesc.position = NxVec3(robotActor->getGlobalPosition()); float mass = 5.; vehicleDesc.mass = mass;//robotActor->getMass(); //PLUGIN TAH COM PROBLEMA XML ERRADO //vehicleDesc.motorForce = 70000; //vehicleDesc.maxVelocity = 300.f; //vehicleDesc.cameraDistance = 8.0f; vehicleDesc.centerOfMass.set(NxVec3(0,0,0));//robotActor->getCMassLocalPosition()); //vehicleDesc.differentialRatio = 3.42f; //vehicleDesc.transmissionEfficiency vehicleDesc.actor = robotActor; vehicleDesc.actor->setMaxAngularVelocity(6.2); vehicleDesc.actor->setMass(mass); //TODO: LEVANTAR DAMPING float t = vehicleDesc.actor->getLinearDamping(); float b = vehicleDesc.actor->getAngularDamping(); //vehicleDesc.actor->setAngularDamping(0.5); //vehicleDesc.actor->setLinearDamping(0.5); //TODO: LEVANTAR CMASS E INERTIA TENSOR //vehicleDesc.actor->setCMassOffsetGlobalPosition(NxVec3(0, 0, 0)); NxMat33 inertiaTensor = NxMat33(NxVec3(1294.4362, 3.14502, -66.954), NxVec3(3.14502, 1094.42351, -0.24279), NxVec3(-66.954, -0.24279, 1754.80511)); vehicleDesc.actor->setCMassOffsetLocalPose( NxMat34( inertiaTensor, NxVec3(0,0,0) ) ); //TODO: Diagonalizar inertiaTensor e passar para setMassSpaceInertiaTensor vehicleDesc.actor->setMassSpaceInertiaTensor(/*vehicleDesc.actor->getMassSpaceInertiaTensor()*1000.*/NxVec3(1764.3, 1284.9, 1094.4) ); //Motor descricao //NxVehicleMotorDesc motorsDesc[4]; //for(NxU32 i=0;i<4;i++) //{ //motorsDesc[i].setToCorvette(); //vehicleDesc.motorsDesc.push_back(&motorsDesc[i]); //} //Roda (Wheel) descricao int numberWheels = Simulation::getNumberWheels(indexScene, indexRobot); NxWheelDesc* wheelDesc = new NxWheelDesc[numberWheels]; for(NxU32 i=0; i<numberWheels; i++) { //NxActor* wheelModel = Simulation::getActorWheel(indexScene,indexRobot,i); //NxActorDesc wheelActorDesc; //wheelModel->saveToDesc(wheelActorDesc); //Simulation::gScenes[0]->releaseActor(*wheelModel); NxActor* actorWheel = Simulation::getActorWheel(indexScene,indexRobot,i);//wheelModel;//Simulation::gScenes[0]->createActor(wheelActorDesc); //NxShape*const* wheelShapes = actorWheel->getShapes(); //NxBounds3 wheelBounds; //wheelShapes[0]->getWorldBounds(wheelBounds); //Para exportar modelo da roda do 3ds Max // NxWhee //wheelDesc[i] //robot1Shapes[0]->isConvexMesh()->getConvexMesh().saveToDesc(convexMesh); //NxWheelShape* wheelShape = (NxWheelShape*)wheel; //NxTriangleMeshDesc meshDesc = *((NxTriangleMeshDesc*)(mesh->userData)); //robot1Shapes[0]->isWheel()-> //wheelDesc[i].wheelApproximation = 10; wheelDesc[i].wheelOrientation = actorWheel->getGlobalOrientation(); wheelDesc[i].position.set(actorWheel->getGlobalPosition()-robotActor->getGlobalPosition()); //wheelDesc[i].position.z = 0; wheelDesc[i].id = i; wheelDesc[i].wheelRadius = 27.6; //wheelDesc[i].wheelWidth = 100; wheelDesc[i].wheelSuspension = 0; wheelDesc[i].springRestitution = 0; wheelDesc[i].springDamping = 0; wheelDesc[i].springBias = 0.0f; wheelDesc[i].maxBrakeForce = 100; wheelDesc[i].frictionToFront = 0.1f;//0.1f; //TODO: CONFIGURAR PARÂMETRO wheelDesc[i].frictionToSide = 0;//0.02f; //TODO: CONFIGURAR PARÂMETRO wheelDesc[i].inverseWheelMass = 0.1; //TODO: CONFIGURAR PARÂMETRO //Angulo das Rodas (Omni) NxVec3 wheelPosRel = actorWheel->getGlobalPosition() - robotActor->getGlobalPosition(); wheelDesc[i].angWheelRelRobot = NxMath::atan2( wheelPosRel.y, wheelPosRel.x ); vehicleDesc.robotWheels.pushBack(&wheelDesc[i]); Simulation::gScenes[indexScene]->scene->releaseActor(*actorWheel); //NxU32 flags = NX_WF_BUILD_LOWER_HALF; wheelDesc[i].wheelFlags = NX_WF_ACCELERATED | NX_WF_AFFECTED_BY_HANDBRAKE | NX_WF_USE_WHEELSHAPE | NX_WF_BUILD_LOWER_HALF;// |/*NX_WF_STEERABLE_INPUT |*/ flags; } //NxBall* teste = Simulation::gScenes[indexScene]->ball; //Criar robot, vehicle base NxRobot* robot = (NxRobot*)NxRobot::createVehicle(Simulation::gScenes[indexScene], &vehicleDesc); if(robot) { robot->setId(indexRobot); robot->setIdTeam(indexTeam); robot->indexScene = indexScene; //Dribbler and Kicker for(int i=0; i<robotActor->getNbShapes(); i++) { NxShape*const* robotShapes = robotActor->getShapes(); const char* shapeName = robotShapes[i]->getName(); if(shapeName) { char* dribblerName = new char[10];//"Driblador\0" dribblerName[9] = 0; memcpy(dribblerName, shapeName, strlen(dribblerName)); char* kickerName = new char[9];//"Chutador\0" kickerName[8] = 0; memcpy(kickerName, shapeName, strlen(kickerName)); if(strcmp(dribblerName, "Driblador") == 0) { robot->dribbler->dribblerShapes.push_back(robotShapes[i]); } else if(strcmp(kickerName, "Chutador") == 0) { robot->kicker->kickerShapeDesc = Simulation::copyShapeDesc(robotShapes[i]); robotActor->releaseShape(*(robotShapes[i])); } delete dribblerName; delete kickerName; } } //Initial Pose robot->setInitialPose(robotActor->getGlobalPose()); robotActor->putToSleep(); //Mudar pose do robo //NxQuat q; //q. //q.fromAngleAxis(180.0f, NxVec3(0.0f, 1.0f, 0.0f)); //robot->getActor()->setGlobalPose(pose); //Release no actor importado do 3ds Max //gScenes[0]->releaseActor(*robotActor); string label; string plabel = "Robo"; stringstream out; out << indexRobot; out << "-"; out << indexTeam; //out << "-"; //out << indexScene; label.append(plabel); label.append(out.str()); char* arrayLabel = new char[label.size()+1]; arrayLabel[label.size()]=0; memcpy(arrayLabel, label.c_str(), label.size()); robotActor->setName(arrayLabel); //delete arrayLabel; } }