void PhysicsEngine::createVehicle(Vehicle* vehicle, PxTransform transform) { VehicleTuning* tuning = &vehicle->tuning; tuningFromUserTuning(vehicle); PxVehicleDrive4W* physVehicle = vehCreator->createVehicle4W(vehicle); //PxTransform startTransform(PxVec3(0, (tuning->chassisDims.y*0.5f + tuning->wheelRadius + 1.0f), 0), PxQuat(PxIdentity)); PxRigidDynamic* actor = physVehicle->getRigidDynamicActor(); actor->setGlobalPose(transform); scene->addActor(*actor); physVehicle->setToRestState(); physVehicle->mDriveDynData.forceGearChange(PxVehicleGearsData::eFIRST); physVehicle->mDriveDynData.setUseAutoGears(true); vehicle->setActor(actor); vehicle->setPhysicsVehicle(physVehicle); actor->userData = vehicle; vehicles.push_back(physVehicle); }
void initPhysics() { ///////////////////////////////////////////// //Initialise the sdk and scene ///////////////////////////////////////////// gFoundation = PxCreateFoundation(PX_PHYSICS_VERSION, gAllocator, gErrorCallback); PxProfileZoneManager* profileZoneManager = &PxProfileZoneManager::createProfileZoneManager(gFoundation); gPhysics = PxCreatePhysics(PX_PHYSICS_VERSION, *gFoundation, PxTolerancesScale(),true, profileZoneManager); if(gPhysics->getPvdConnectionManager()) { gPhysics->getVisualDebugger()->setVisualizeConstraints(true); gPhysics->getVisualDebugger()->setVisualDebuggerFlag(PxVisualDebuggerFlag::eTRANSMIT_CONTACTS, false); gPhysics->getVisualDebugger()->setVisualDebuggerFlag(PxVisualDebuggerFlag::eTRANSMIT_SCENEQUERIES, false); gPhysics->getVisualDebugger()->setVisualDebuggerFlag(PxVisualDebuggerFlag::eTRANSMIT_CONSTRAINTS, false); gConnection = PxVisualDebuggerExt::createConnection(gPhysics->getPvdConnectionManager(), PVD_HOST, 5425, 10, gConnectionFlags); } PxSceneDesc sceneDesc(gPhysics->getTolerancesScale()); sceneDesc.gravity = PxVec3(0.0f, -9.81f, 0.0f); gDispatcher = PxDefaultCpuDispatcherCreate(NUM_WORKER_THREADS); sceneDesc.cpuDispatcher = gDispatcher; sceneDesc.filterShader = VehicleFilterShader; gScene = gPhysics->createScene(sceneDesc); gCooking = PxCreateCooking(PX_PHYSICS_VERSION, *gFoundation, PxCookingParams(PxTolerancesScale())); ///////////////////////////////////////////// //Create a task manager that will be used to //update the vehicles concurrently across //multiple threads. ///////////////////////////////////////////// gTaskManager = physx::PxTaskManager::createTaskManager(gDispatcher, NULL, NULL); ///////////////////////////////////////////// //Initialise the vehicle sdk and create //vehicles that will drive on a plane ///////////////////////////////////////////// PxInitVehicleSDK(*gPhysics); PxVehicleSetBasisVectors(PxVec3(0,1,0), PxVec3(0,0,1)); PxVehicleSetUpdateMode(PxVehicleUpdateMode::eVELOCITY_CHANGE); //Create the batched scene queries for the suspension raycasts. gVehicleSceneQueryData = VehicleSceneQueryData::allocate(NUM_VEHICLES, PX_MAX_NB_WHEELS, 1, gAllocator); for(PxU32 i = 0; i < NUM_VEHICLES; i++) { gBatchQueries[i] = VehicleSceneQueryData::setUpBatchedSceneQuery(i, *gVehicleSceneQueryData, gScene); } //Create the friction table for each combination of tire and surface type. //For simplicity we only have a single surface type. gMaterial = gPhysics->createMaterial(0.5f, 0.5f, 0.6f); gFrictionPairs = createFrictionPairs(gMaterial); //Create a plane to drive on. gGroundPlane = createDrivablePlane(gMaterial, gPhysics); gScene->addActor(*gGroundPlane); //Create vehicles that will drive on the plane. for(PxU32 i = 0; i < NUM_VEHICLES; i++) { VehicleDesc vehicleDesc = initVehicleDesc(); PxVehicleDrive4W* vehicle = createVehicle4W(vehicleDesc, gPhysics, gCooking); PxTransform startTransform(PxVec3(vehicleDesc.chassisDims.x*3.0f*i, (vehicleDesc.chassisDims.y*0.5f + vehicleDesc.wheelRadius + 1.0f), 0), PxQuat(PxIdentity)); vehicle->getRigidDynamicActor()->setGlobalPose(startTransform); gScene->addActor(*vehicle->getRigidDynamicActor()); //Set the vehicle to rest in first gear. //Set the vehicle to use auto-gears. vehicle->setToRestState(); vehicle->mDriveDynData.forceGearChange(PxVehicleGearsData::eFIRST); vehicle->mDriveDynData.setUseAutoGears(true); //Set each car to accelerate forwards vehicle->mDriveDynData.setAnalogInput(PxVehicleDrive4WControl::eANALOG_INPUT_ACCEL, 1.0f); gVehicles[i] = vehicle; } //Set up the wheel query results that are used to query the state of the vehicle after calling PxVehicleUpdates gVehicleWheelQueryResults = VehicleWheelQueryResults::allocate(NUM_VEHICLES, PX_MAX_NB_WHEELS, gAllocator); //Set up the data required for concurrent calls to PxVehicleUpdates gVehicleConcurrency = VehicleConcurrency::allocate(NUM_VEHICLES, PX_MAX_NB_WHEELS, gAllocator); //Set up the profile zones so that the advantages of parallelism can be measured in pvd. #ifdef PX_PROFILE gProfiler = VehicleProfiler::allocate(gProfileZoneNames, eMAX_NUM_PROFILES, profileZoneManager, gAllocator); #endif }