void GravitateToGravitators() { NxU32 nbActors = gScene->getNbActors(); NxActor** actors = gScene->getActors(); NxReal m1,m2,G=gUniversalBigG,R; NxVec3 r,F; while (nbActors--) { NxActor* actor = *actors++; const char* name=actor->getName(); if (strcmp(name,"~lander")==0) { F.zero(); m1=actor->getMass(); for (int k=0; k<gRavitators.size(); k++) { r = (gRavitators[k]->getCMassGlobalPosition() - actor->getCMassGlobalPosition()); R = r.magnitudeSquared(); m2= gRavitators[k]->getMass(); F = r; F.setMagnitude(G*m1*m2/R); actor->addForce(F); } } } }
void IdentifyGravitators() { gRavitators.clear(); NxU32 nbActors = gScene->getNbActors(); NxActor** actors = gScene->getActors(); while (nbActors--) { NxActor* actor = *actors++; if (actor->getMass()>gRavitatorThresholdMass) gRavitators.push_back(actor); } }
CPhysicUserData* CPhysicsManager::RaycastClosestActorShoot (const Vect3f posRay, const Vect3f& dirRay, uint32 impactMask, SCollisionInfo& info, float _fPower) { //NxUserRaycastReport::ALL_SHAPES assert(m_pScene != NULL); NxRay ray; ray.dir = NxVec3(dirRay.x, dirRay.y, dirRay.z); ray.orig = NxVec3(posRay.x, posRay.y, posRay.z); NxRaycastHit hit; NxShape* closestShape = NULL; closestShape = m_pScene->raycastClosestShape(ray, NX_ALL_SHAPES, hit, impactMask); if (!closestShape) { //No hemos tokado a ningún objeto físico de la escena. return NULL; } NxActor* actor = &closestShape->getActor(); CPhysicUserData* impactObject =(CPhysicUserData*)actor->userData; //Si está petando aquí quiere decir que se ha registrado un objeto físico sin proporcionarle UserData assert(impactObject); info.m_fDistance = hit.distance; info.m_Normal = Vect3f(hit.worldNormal.x, hit.worldNormal.y, hit.worldNormal.z ); info.m_CollisionPoint = Vect3f(hit.worldImpact.x, hit.worldImpact.y, hit.worldImpact.z ); Vect3f l_vDirection(dirRay.x-posRay.x,dirRay.y-posRay.y,dirRay.z-posRay.z); l_vDirection.Normalize(); NxVec3 l_vDirectionVec(dirRay.x,dirRay.y,dirRay.z); NxF32 coeff = actor->getMass() * _fPower; actor->addForceAtLocalPos(l_vDirectionVec*coeff, NxVec3(0,0,0), NX_IMPULSE,true); return impactObject; }
//----------------------------------------------------------------------- PhysicsActor* PhysXActorExtern::createPhysicsActor(PhysicsActorDesc* physicsActorDesc, PhysicsShapeDesc* physicsShapeDesc) { if (!PhysXBridge::getSingletonPtr()->getScene() || !physicsActorDesc || !physicsShapeDesc) return 0; NxBodyDesc bodyDesc; bodyDesc.setToDefault(); NxReal angularDamping = bodyDesc.angularDamping; NxVec3 angularVelocity = bodyDesc.angularVelocity; NxVec3 linearVelocity = bodyDesc.linearVelocity; bodyDesc.angularDamping = physicsShapeDesc->mAngularDamping; bodyDesc.angularVelocity = PhysXMath::convert(physicsShapeDesc->mAngularVelocity); bodyDesc.linearVelocity = PhysXMath::convert(physicsActorDesc->direction); NxActorDesc actorDesc; NxActorDesc defaultActorDesc; actorDesc.setToDefault(); defaultActorDesc.setToDefault(); switch (physicsShapeDesc->mPhysicsShapeType) { case ST_BOX: { PhysicsBoxDesc* physicsBoxDesc = static_cast<PhysicsBoxDesc*>(physicsShapeDesc); NxBoxShapeDesc boxDesc; boxDesc.setToDefault(); boxDesc.dimensions = PhysXMath::convert(physicsBoxDesc->mDimensions); boxDesc.group = physicsBoxDesc->mCollisionGroup; boxDesc.groupsMask = PhysXMath::convert(physicsBoxDesc->mGroupMask); boxDesc.materialIndex = physicsBoxDesc->mMaterialIndex; actorDesc.density = NxComputeBoxDensity(2 * boxDesc.dimensions, physicsActorDesc->mass); actorDesc.shapes.pushBack(&boxDesc); } break; case ST_SPHERE: { PhysicsSphereDesc* physicsSphereDesc = static_cast<PhysicsSphereDesc*>(physicsShapeDesc); NxSphereShapeDesc sphereDec; sphereDec.setToDefault(); sphereDec.radius = physicsSphereDesc->mRadius; sphereDec.group = physicsSphereDesc->mCollisionGroup; sphereDec.groupsMask = PhysXMath::convert(physicsSphereDesc->mGroupMask); sphereDec.materialIndex = physicsSphereDesc->mMaterialIndex; actorDesc.density = NxComputeSphereDensity(sphereDec.radius, physicsActorDesc->mass); actorDesc.shapes.pushBack(&sphereDec); } break; case ST_CAPSULE: { PhysicsCapsuleDesc* physicsCapsuleDesc = static_cast<PhysicsCapsuleDesc*>(physicsShapeDesc); NxCapsuleShapeDesc capsuleDec; capsuleDec.setToDefault(); capsuleDec.radius = physicsCapsuleDesc->mRadius; capsuleDec.height = physicsCapsuleDesc->mHeight; capsuleDec.group = physicsCapsuleDesc->mCollisionGroup; capsuleDec.groupsMask = PhysXMath::convert(physicsCapsuleDesc->mGroupMask); capsuleDec.materialIndex = physicsCapsuleDesc->mMaterialIndex; actorDesc.density = NxComputeCylinderDensity(capsuleDec.radius, capsuleDec.height, physicsActorDesc->mass); actorDesc.shapes.pushBack(&capsuleDec); } break; } actorDesc.globalPose.t = PhysXMath::convert(physicsActorDesc->position); actorDesc.body = &bodyDesc; actorDesc.group = physicsActorDesc->collisionGroup; PhysXActor* physXActor = 0; if (!actorDesc.isValid()) { actorDesc = defaultActorDesc; Ogre::LogManager::getSingleton().logMessage("ParticleUniverse PhysXActor: Cannot create actor; use default attributes."); } NxActor* nxActor = PhysXBridge::getSingletonPtr()->getScene()->createActor(actorDesc); if (nxActor) { physXActor = OGRE_NEW_T(PhysXActor, Ogre::MEMCATEGORY_SCENE_OBJECTS)(); physXActor->position = PhysXMath::convert(nxActor->getGlobalPosition()); physXActor->direction = PhysXMath::convert(nxActor->getLinearVelocity()); nxActor->setGlobalOrientationQuat(PhysXMath::convert(physicsActorDesc->orientation)); physXActor->orientation = physicsActorDesc->orientation; physXActor->mass = nxActor->getMass(); physXActor->collisionGroup = nxActor->getGroup(); physXActor->nxActor = nxActor; } return physXActor; }
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; }