Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This code contains NVIDIA Confidential Information and is disclosed to you
- // under a form of NVIDIA software license agreement provided separately to you.
- //
- // Notice
- // NVIDIA Corporation and its licensors retain all intellectual property and
- // proprietary rights in and to this software and related documentation and
- // any modifications thereto. Any use, reproduction, disclosure, or
- // distribution of this software and related documentation without an express
- // license agreement from NVIDIA Corporation is strictly prohibited.
- //
- // ALL NVIDIA DESIGN SPECIFICATIONS, CODE ARE PROVIDED "AS IS.". NVIDIA MAKES
- // NO WARRANTIES, EXPRESSED, IMPLIED, STATUTORY, OR OTHERWISE WITH RESPECT TO
- // THE MATERIALS, AND EXPRESSLY DISCLAIMS ALL IMPLIED WARRANTIES OF NONINFRINGEMENT,
- // MERCHANTABILITY, AND FITNESS FOR A PARTICULAR PURPOSE.
- //
- // Information and code furnished is believed to be accurate and reliable.
- // However, NVIDIA Corporation assumes no responsibility for the consequences of use of such
- // information or for any infringement of patents or other rights of third parties that may
- // result from its use. No license is granted by implication or otherwise under any patent
- // or patent rights of NVIDIA Corporation. Details are subject to change without notice.
- // This code supersedes and replaces all information previously supplied.
- // NVIDIA Corporation products are not authorized for use as critical
- // components in life support devices or systems without express written approval of
- // NVIDIA Corporation.
- //
- // Copyright (c) 2008-2014 NVIDIA Corporation. All rights reserved.
- // Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
- // Copyright (c) 2001-2004 NovodeX AG. All rights reserved.
- // ****************************************************************************
- // This snippet illustrates simple use of PxVehicleDrive4W.
- //
- // It creates a vehicle on a plane and then controls the vehicle so that it performs a
- // number of choreographed manoeuvres such as accelerate, reverse, brake, handbrake, and turn.
- // It is a good idea to record and playback with pvd (PhysX Visual Debugger).
- // ****************************************************************************
- #include <ctype.h>
- #include "PxPhysicsAPI.h"
- #include "vehicle/PxVehicleUtil.h"
- #include "../SnippetVehicleCommon/SnippetVehicleRaycast.h"
- #include "../SnippetVehicleCommon/SnippetVehicleFilterShader.h"
- #include "../SnippetVehicleCommon/SnippetVehicleTireFriction.h"
- #include "../SnippetVehicleCommon/SnippetVehicleCreate.h"
- #include "../SnippetCommon/SnippetPrint.h"
- #include "../SnippetCommon/SnippetPVD.h"
- #include "../SnippetUtils/SnippetUtils.h"
- using namespace physx;
- PxDefaultAllocator gAllocator;
- PxDefaultErrorCallback gErrorCallback;
- PxFoundation* gFoundation = NULL;
- PxPhysics* gPhysics = NULL;
- PxDefaultCpuDispatcher* gDispatcher = NULL;
- PxScene* gScene = NULL;
- PxCooking* gCooking = NULL;
- PxMaterial* gMaterial = NULL;
- PxVisualDebuggerConnection*
- gConnection = NULL;
- VehicleSceneQueryData* gVehicleSceneQueryData = NULL;
- PxBatchQuery* gBatchQuery = NULL;
- PxVehicleDrivableSurfaceToTireFrictionPairs* gFrictionPairs = NULL;
- PxRigidStatic* gGroundPlane = NULL;
- PxVehicleDrive4W* gVehicle4W = NULL;
- bool gIsVehicleInAir = true;
- PxF32 gSteerVsForwardSpeedData[2*8]=
- {
- 0.0f, 0.75f,
- 5.0f, 0.75f,
- 30.0f, 0.125f,
- 120.0f, 0.1f,
- PX_MAX_F32, PX_MAX_F32,
- PX_MAX_F32, PX_MAX_F32,
- PX_MAX_F32, PX_MAX_F32,
- PX_MAX_F32, PX_MAX_F32
- };
- PxFixedSizeLookupTable<8> gSteerVsForwardSpeedTable(gSteerVsForwardSpeedData,4);
- PxVehicleKeySmoothingData gKeySmoothingData=
- {
- {
- 6.0f, //rise rate eANALOG_INPUT_ACCEL
- 6.0f, //rise rate eANALOG_INPUT_BRAKE
- 6.0f, //rise rate eANALOG_INPUT_HANDBRAKE
- 2.5f, //rise rate eANALOG_INPUT_STEER_LEFT
- 2.5f, //rise rate eANALOG_INPUT_STEER_RIGHT
- },
- {
- 10.0f, //fall rate eANALOG_INPUT_ACCEL
- 10.0f, //fall rate eANALOG_INPUT_BRAKE
- 10.0f, //fall rate eANALOG_INPUT_HANDBRAKE
- 5.0f, //fall rate eANALOG_INPUT_STEER_LEFT
- 5.0f //fall rate eANALOG_INPUT_STEER_RIGHT
- }
- };
- PxVehiclePadSmoothingData gPadSmoothingData=
- {
- {
- 6.0f, //rise rate eANALOG_INPUT_ACCEL
- 6.0f, //rise rate eANALOG_INPUT_BRAKE
- 6.0f, //rise rate eANALOG_INPUT_HANDBRAKE
- 2.5f, //rise rate eANALOG_INPUT_STEER_LEFT
- 2.5f, //rise rate eANALOG_INPUT_STEER_RIGHT
- },
- {
- 10.0f, //fall rate eANALOG_INPUT_ACCEL
- 10.0f, //fall rate eANALOG_INPUT_BRAKE
- 10.0f, //fall rate eANALOG_INPUT_HANDBRAKE
- 5.0f, //fall rate eANALOG_INPUT_STEER_LEFT
- 5.0f //fall rate eANALOG_INPUT_STEER_RIGHT
- }
- };
- PxVehicleDrive4WRawInputData gVehicleInputData;
- VehicleDesc initVehicleDesc()
- {
- //Set up the chassis mass, dimensions, moment of inertia, and center of mass offset.
- //The moment of inertia is just the moment of inertia of a cuboid but modified for easier steering.
- //Center of mass offset is 0.65m above the base of the chassis and 0.25m towards the front.
- const PxF32 chassisMass = 1500.0f;
- const PxVec3 chassisDims(2.5f,2.0f,5.0f);
- const PxVec3 chassisMOI
- ((chassisDims.y*chassisDims.y + chassisDims.z*chassisDims.z)*chassisMass/12.0f,
- (chassisDims.x*chassisDims.x + chassisDims.z*chassisDims.z)*0.8f*chassisMass/12.0f,
- (chassisDims.x*chassisDims.x + chassisDims.y*chassisDims.y)*chassisMass/12.0f);
- const PxVec3 chassisCMOffset(0.0f, -chassisDims.y*0.5f + 0.65f, 0.25f);
- //Set up the wheel mass, radius, width, moment of inertia, and number of wheels.
- //Moment of inertia is just the moment of inertia of a cylinder.
- const PxF32 wheelMass = 20.0f;
- const PxF32 wheelRadius = 0.5f;
- const PxF32 wheelWidth = 0.4f;
- const PxF32 wheelMOI = 0.5f*wheelMass*wheelRadius*wheelRadius;
- const PxU32 nbWheels = 4;
- VehicleDesc vehicleDesc;
- vehicleDesc.chassisMass = chassisMass;
- vehicleDesc.chassisDims = chassisDims;
- vehicleDesc.chassisMOI = chassisMOI;
- vehicleDesc.chassisCMOffset = chassisCMOffset;
- vehicleDesc.chassisMaterial = gMaterial;
- vehicleDesc.wheelMass = wheelMass;
- vehicleDesc.wheelRadius = wheelRadius;
- vehicleDesc.wheelWidth = wheelWidth;
- vehicleDesc.wheelMOI = wheelMOI;
- vehicleDesc.numWheels = nbWheels;
- vehicleDesc.wheelMaterial = gMaterial;
- return vehicleDesc;
- }
- void initPhysics()
- {
- 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, true);
- gPhysics->getVisualDebugger()->setVisualDebuggerFlag(PxVisualDebuggerFlag::eTRANSMIT_SCENEQUERIES, true);
- gConnection = PxVisualDebuggerExt::createConnection(gPhysics->getPvdConnectionManager(), PVD_HOST, 5425, 10);
- }
- PxSceneDesc sceneDesc(gPhysics->getTolerancesScale());
- sceneDesc.gravity = PxVec3(0.0f, -9.81f, 0.0f);
- PxU32 numWorkers = 1;
- gDispatcher = PxDefaultCpuDispatcherCreate(numWorkers);
- sceneDesc.cpuDispatcher = gDispatcher;
- sceneDesc.filterShader = VehicleFilterShader;
- gScene = gPhysics->createScene(sceneDesc);
- gMaterial = gPhysics->createMaterial(0.5f, 0.5f, 0.6f);
- gCooking = PxCreateCooking(PX_PHYSICS_VERSION, *gFoundation, PxCookingParams(PxTolerancesScale()));
- /////////////////////////////////////////////
- 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(1, PX_MAX_NB_WHEELS, 1, gAllocator);
- gBatchQuery = VehicleSceneQueryData::setUpBatchedSceneQuery(0, *gVehicleSceneQueryData, gScene);
- //Create the friction table for each combination of tire and surface type.
- gFrictionPairs = createFrictionPairs(gMaterial);
- //Create a plane to drive on.
- gGroundPlane = createDrivablePlane(gMaterial, gPhysics);
- gScene->addActor(*gGroundPlane);
- //Create a vehicle that will drive on the plane.
- VehicleDesc vehicleDesc = initVehicleDesc();
- gVehicle4W = createVehicle4W(vehicleDesc, gPhysics, gCooking);
- PxTransform startTransform(PxVec3(0, (vehicleDesc.chassisDims.y*0.5f + vehicleDesc.wheelRadius + 1.0f), 0), PxQuat(PxIdentity));
- gVehicle4W->getRigidDynamicActor()->setGlobalPose(startTransform);
- gScene->addActor(*gVehicle4W->getRigidDynamicActor());
- //Set the vehicle to rest in first gear.
- //Set the vehicle to use auto-gears.
- gVehicle4W->setToRestState();
- gVehicle4W->mDriveDynData.forceGearChange(PxVehicleGearsData::eFIRST);
- gVehicle4W->mDriveDynData.setUseAutoGears(true);
- gVehicleInputData.setAnalogAccel(1.0f);
- }
- void stepPhysics()
- {
- const PxF32 timestep = 1.0f/60.0f;
- //Cycle through the driving modes to demonstrate how to accelerate/reverse/brake/turn etc.
- //incrementDrivingMode(timestep);
- //Update the control inputs for the vehicle.
- //PxVehicleDrive4WSmoothDigitalRawInputsAndSetAnalogInputs(gKeySmoothingData, gSteerVsForwardSpeedTable, gVehicleInputData, timestep, gIsVehicleInAir, *gVehicle4W);
- PxVehicleDrive4WSmoothAnalogRawInputsAndSetAnalogInputs(gPadSmoothingData, gSteerVsForwardSpeedTable, gVehicleInputData, timestep, gIsVehicleInAir, *gVehicle4W);
- //Raycasts.
- PxVehicleWheels* vehicles[1] = {gVehicle4W};
- PxRaycastQueryResult* raycastResults = gVehicleSceneQueryData->getRaycastQueryResultBuffer(0);
- const PxU32 raycastResultsSize = gVehicleSceneQueryData->getRaycastQueryResultBufferSize();
- PxVehicleSuspensionRaycasts(gBatchQuery, 1, vehicles, raycastResultsSize, raycastResults);
- //Vehicle update.
- const PxVec3 grav = gScene->getGravity();
- PxWheelQueryResult wheelQueryResults[PX_MAX_NB_WHEELS];
- PxVehicleWheelQueryResult vehicleQueryResults[1] = {{wheelQueryResults, gVehicle4W->mWheelsSimData.getNbWheels()}};
- PxVehicleUpdates(timestep, grav, *gFrictionPairs, 1, vehicles, vehicleQueryResults);
- //Work out if the vehicle is in the air.
- gIsVehicleInAir = gVehicle4W->getRigidDynamicActor()->isSleeping() ? false : PxVehicleIsInAir(vehicleQueryResults[0]);
- //Scene update.
- gScene->simulate(timestep);
- gScene->fetchResults(true);
- }
- void cleanupPhysics()
- {
- gVehicle4W->getRigidDynamicActor()->release();
- gVehicle4W->free();
- gGroundPlane->release();
- gBatchQuery->release();
- gVehicleSceneQueryData->free(gAllocator);
- gFrictionPairs->release();
- PxCloseVehicleSDK();
- gMaterial->release();
- gCooking->release();
- gScene->release();
- gDispatcher->release();
- PxProfileZoneManager* profileZoneManager = gPhysics->getProfileZoneManager();
- if(gConnection != NULL)
- gConnection->release();
- gPhysics->release();
- profileZoneManager->release();
- gFoundation->release();
- printf("SnippetVehicle4W done.\n");
- }
- void keyPress(const char key, const PxTransform& camera)
- {
- PX_UNUSED(camera);
- PX_UNUSED(key);
- }
- int snippetMain(int, const char*const*)
- {
- #ifdef RENDER_SNIPPET
- extern void renderLoop();
- renderLoop();
- #else
- initPhysics();
- while(!gVehicleOrderComplete)
- {
- stepPhysics();
- }
- cleanupPhysics();
- #endif
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement