diff options
| author | git perforce import user <a@b> | 2016-10-25 12:29:14 -0600 |
|---|---|---|
| committer | Sheikh Dawood Abdul Ajees <Sheikh Dawood Abdul Ajees> | 2016-10-25 18:56:37 -0500 |
| commit | 3dfe2108cfab31ba3ee5527e217d0d8e99a51162 (patch) | |
| tree | fa6485c169e50d7415a651bf838f5bcd0fd3bfbd /PhysX_3.4/Snippets/SnippetVehicleScale/SnippetVehicleScale.cpp | |
| download | physx-3.4-3dfe2108cfab31ba3ee5527e217d0d8e99a51162.tar.xz physx-3.4-3dfe2108cfab31ba3ee5527e217d0d8e99a51162.zip | |
Initial commit:
PhysX 3.4.0 Update @ 21294896
APEX 1.4.0 Update @ 21275617
[CL 21300167]
Diffstat (limited to 'PhysX_3.4/Snippets/SnippetVehicleScale/SnippetVehicleScale.cpp')
| -rw-r--r-- | PhysX_3.4/Snippets/SnippetVehicleScale/SnippetVehicleScale.cpp | 552 |
1 files changed, 552 insertions, 0 deletions
diff --git a/PhysX_3.4/Snippets/SnippetVehicleScale/SnippetVehicleScale.cpp b/PhysX_3.4/Snippets/SnippetVehicleScale/SnippetVehicleScale.cpp new file mode 100644 index 00000000..f066c2d2 --- /dev/null +++ b/PhysX_3.4/Snippets/SnippetVehicleScale/SnippetVehicleScale.cpp @@ -0,0 +1,552 @@ +// 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-2016 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 how to configure a PhysX vehicle when meters are not +// the chosen length scale. The snippet sets up a vehicle with meters as the +// adopted length scale and then modifies the vehicle parameters so that they represent +// the same vehicle but with centimeters as the chosen length scale. It is written in +// a way that allows any length scale to be chosen. A key function here is the function +// customizeVehicleToLengthScale. + +// 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/SnippetVehicleSceneQuery.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; +using namespace snippetvehicle; + +PxDefaultAllocator gAllocator; +PxDefaultErrorCallback gErrorCallback; + +PxFoundation* gFoundation = NULL; +PxPhysics* gPhysics = NULL; + +PxDefaultCpuDispatcher* gDispatcher = NULL; +PxScene* gScene = NULL; + +PxCooking* gCooking = NULL; + +PxMaterial* gMaterial = NULL; + +PxPvd* gPvd = 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; + +enum DriveMode +{ + eDRIVE_MODE_ACCEL_FORWARDS=0, + eDRIVE_MODE_ACCEL_REVERSE, + eDRIVE_MODE_HARD_TURN_LEFT, + eDRIVE_MODE_HANDBRAKE_TURN_LEFT, + eDRIVE_MODE_HARD_TURN_RIGHT, + eDRIVE_MODE_HANDBRAKE_TURN_RIGHT, + eDRIVE_MODE_BRAKE, + eDRIVE_MODE_NONE +}; + +DriveMode gDriveModeOrder[] = +{ + eDRIVE_MODE_BRAKE, + eDRIVE_MODE_ACCEL_FORWARDS, + eDRIVE_MODE_BRAKE, + eDRIVE_MODE_ACCEL_REVERSE, + eDRIVE_MODE_BRAKE, + eDRIVE_MODE_HARD_TURN_LEFT, + eDRIVE_MODE_BRAKE, + eDRIVE_MODE_HARD_TURN_RIGHT, + eDRIVE_MODE_ACCEL_FORWARDS, + eDRIVE_MODE_HANDBRAKE_TURN_LEFT, + eDRIVE_MODE_ACCEL_FORWARDS, + eDRIVE_MODE_HANDBRAKE_TURN_RIGHT, + eDRIVE_MODE_NONE +}; + +PxF32 gVehicleModeLifetime = 4.0f; +PxF32 gVehicleModeTimer = 0.0f; +PxU32 gVehicleOrderProgress = 0; +bool gVehicleOrderComplete = false; +bool gMimicKeyInputs = true; + +enum +{ + eLengthScaleCentimeters=0, + eLengthScaleInches +}; +PxF32 gLengthScales[2] = {100.0f, 39.3701f}; +PxF32 gLengthScale = gLengthScales[eLengthScaleInches]; + +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.chassisSimFilterData = PxFilterData(COLLISION_FLAG_CHASSIS, COLLISION_FLAG_CHASSIS_AGAINST, 0, 0); + + vehicleDesc.wheelMass = wheelMass; + vehicleDesc.wheelRadius = wheelRadius; + vehicleDesc.wheelWidth = wheelWidth; + vehicleDesc.wheelMOI = wheelMOI; + vehicleDesc.numWheels = nbWheels; + vehicleDesc.wheelMaterial = gMaterial; + vehicleDesc.chassisSimFilterData = PxFilterData(COLLISION_FLAG_WHEEL, COLLISION_FLAG_WHEEL_AGAINST, 0, 0); + + return vehicleDesc; +} + +void startAccelerateForwardsMode() +{ + if(gMimicKeyInputs) + { + gVehicleInputData.setDigitalAccel(true); + } + else + { + gVehicleInputData.setAnalogAccel(1.0f); + } +} + +void startAccelerateReverseMode() +{ + gVehicle4W->mDriveDynData.forceGearChange(PxVehicleGearsData::eREVERSE); + + if(gMimicKeyInputs) + { + gVehicleInputData.setDigitalAccel(true); + } + else + { + gVehicleInputData.setAnalogAccel(1.0f); + } +} + +void startBrakeMode() +{ + if(gMimicKeyInputs) + { + gVehicleInputData.setDigitalBrake(true); + } + else + { + gVehicleInputData.setAnalogBrake(1.0f); + } +} + +void startTurnHardLeftMode() +{ + if(gMimicKeyInputs) + { + gVehicleInputData.setDigitalAccel(true); + gVehicleInputData.setDigitalSteerLeft(true); + } + else + { + gVehicleInputData.setAnalogAccel(true); + gVehicleInputData.setAnalogSteer(-1.0f); + } +} + +void startTurnHardRightMode() +{ + if(gMimicKeyInputs) + { + gVehicleInputData.setDigitalAccel(true); + gVehicleInputData.setDigitalSteerRight(true); + } + else + { + gVehicleInputData.setAnalogAccel(1.0f); + gVehicleInputData.setAnalogSteer(1.0f); + } +} + +void startHandbrakeTurnLeftMode() +{ + if(gMimicKeyInputs) + { + gVehicleInputData.setDigitalSteerLeft(true); + gVehicleInputData.setDigitalHandbrake(true); + } + else + { + gVehicleInputData.setAnalogSteer(-1.0f); + gVehicleInputData.setAnalogHandbrake(1.0f); + } +} + +void startHandbrakeTurnRightMode() +{ + if(gMimicKeyInputs) + { + gVehicleInputData.setDigitalSteerRight(true); + gVehicleInputData.setDigitalHandbrake(true); + } + else + { + gVehicleInputData.setAnalogSteer(1.0f); + gVehicleInputData.setAnalogHandbrake(1.0f); + } +} + + +void releaseAllControls() +{ + if(gMimicKeyInputs) + { + gVehicleInputData.setDigitalAccel(false); + gVehicleInputData.setDigitalSteerLeft(false); + gVehicleInputData.setDigitalSteerRight(false); + gVehicleInputData.setDigitalBrake(false); + gVehicleInputData.setDigitalHandbrake(false); + } + else + { + gVehicleInputData.setAnalogAccel(0.0f); + gVehicleInputData.setAnalogSteer(0.0f); + gVehicleInputData.setAnalogBrake(0.0f); + gVehicleInputData.setAnalogHandbrake(0.0f); + } +} + + +void initPhysics() +{ + gFoundation = PxCreateFoundation(PX_FOUNDATION_VERSION, gAllocator, gErrorCallback); + PxTolerancesScale scale; + scale.length = gLengthScale; + scale.speed = 10.0f * gLengthScale; + gPvd = PxCreatePvd(*gFoundation); + PxPvdTransport* transport = PxDefaultPvdSocketTransportCreate(PVD_HOST, 5425, 10); + gPvd->connect(*transport,PxPvdInstrumentationFlag::eALL); + gPhysics = PxCreatePhysics(PX_PHYSICS_VERSION, *gFoundation, scale, true, gPvd); + + PxSceneDesc sceneDesc(gPhysics->getTolerancesScale()); + sceneDesc.gravity = PxVec3(0.0f, -9.81f*gLengthScale, 0.0f); + + PxU32 numWorkers = 1; + gDispatcher = PxDefaultCpuDispatcherCreate(numWorkers); + sceneDesc.cpuDispatcher = gDispatcher; + sceneDesc.filterShader = VehicleFilterShader; + + gScene = gPhysics->createScene(sceneDesc); + + PxPvdSceneClient* pvdClient = gScene->getScenePvdClient(); + if(pvdClient) + { + pvdClient->setScenePvdFlag(PxPvdSceneFlag::eTRANSMIT_CONSTRAINTS, true); + pvdClient->setScenePvdFlag(PxPvdSceneFlag::eTRANSMIT_CONTACTS, true); + pvdClient->setScenePvdFlag(PxPvdSceneFlag::eTRANSMIT_SCENEQUERIES, true); + } + + 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, 1, WheelSceneQueryPreFilterBlocking, NULL, 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. + PxFilterData groundPlaneSimFilterData(COLLISION_FLAG_GROUND, COLLISION_FLAG_GROUND_AGAINST, 0, 0); + gGroundPlane = createDrivablePlane(groundPlaneSimFilterData, gMaterial, gPhysics); + gScene->addActor(*gGroundPlane); + + //Create a vehicle that will drive on the plane. + VehicleDesc vehicleDesc = initVehicleDesc(); + gVehicle4W = createVehicle4W(vehicleDesc, gPhysics, gCooking); + //Convert the vehicle from meters to the chosen length scale. + customizeVehicleToLengthScale(gLengthScale, gVehicle4W->getRigidDynamicActor(), &gVehicle4W->mWheelsSimData, &gVehicle4W->mDriveSimData); + //Convert the steer angle vs forward speed table to the chosen length scale. + for(PxU32 i = 0; i < gSteerVsForwardSpeedTable.mNbDataPairs; i++) + { + gSteerVsForwardSpeedTable.mDataPairs[2*i +0] *= gLengthScale; + } + PxTransform startTransform(PxVec3(0, ((vehicleDesc.chassisDims.y*0.5f + vehicleDesc.wheelRadius + 1.0f)*gLengthScale), 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); + + gVehicleModeTimer = 0.0f; + gVehicleOrderProgress = 0; + startBrakeMode(); +} + +void incrementDrivingMode(const PxF32 timestep) +{ + gVehicleModeTimer += timestep; + if(gVehicleModeTimer > gVehicleModeLifetime) + { + //If the mode just completed was eDRIVE_MODE_ACCEL_REVERSE then switch back to forward gears. + if(eDRIVE_MODE_ACCEL_REVERSE == gDriveModeOrder[gVehicleOrderProgress]) + { + gVehicle4W->mDriveDynData.forceGearChange(PxVehicleGearsData::eFIRST); + } + + //Increment to next driving mode. + gVehicleModeTimer = 0.0f; + gVehicleOrderProgress++; + releaseAllControls(); + + //If we are at the end of the list of driving modes then start again. + if(eDRIVE_MODE_NONE == gDriveModeOrder[gVehicleOrderProgress]) + { + gVehicleOrderProgress = 0; + gVehicleOrderComplete = true; + } + + //Start driving in the selected mode. + DriveMode eDriveMode = gDriveModeOrder[gVehicleOrderProgress]; + switch(eDriveMode) + { + case eDRIVE_MODE_ACCEL_FORWARDS: + startAccelerateForwardsMode(); + break; + case eDRIVE_MODE_ACCEL_REVERSE: + startAccelerateReverseMode(); + break; + case eDRIVE_MODE_HARD_TURN_LEFT: + startTurnHardLeftMode(); + break; + case eDRIVE_MODE_HANDBRAKE_TURN_LEFT: + startHandbrakeTurnLeftMode(); + break; + case eDRIVE_MODE_HARD_TURN_RIGHT: + startTurnHardRightMode(); + break; + case eDRIVE_MODE_HANDBRAKE_TURN_RIGHT: + startHandbrakeTurnRightMode(); + break; + case eDRIVE_MODE_BRAKE: + startBrakeMode(); + break; + case eDRIVE_MODE_NONE: + break; + }; + + //If the mode about to start is eDRIVE_MODE_ACCEL_REVERSE then switch to reverse gears. + if(eDRIVE_MODE_ACCEL_REVERSE == gDriveModeOrder[gVehicleOrderProgress]) + { + gVehicle4W->mDriveDynData.forceGearChange(PxVehicleGearsData::eREVERSE); + } + } +} + +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. + if(gMimicKeyInputs) + { + PxVehicleDrive4WSmoothDigitalRawInputsAndSetAnalogInputs(gKeySmoothingData, gSteerVsForwardSpeedTable, gVehicleInputData, timestep, gIsVehicleInAir, *gVehicle4W); + } + else + { + PxVehicleDrive4WSmoothAnalogRawInputsAndSetAnalogInputs(gPadSmoothingData, gSteerVsForwardSpeedTable, gVehicleInputData, timestep, gIsVehicleInAir, *gVehicle4W); + } + + //Raycasts. + PxVehicleWheels* vehicles[1] = {gVehicle4W}; + PxRaycastQueryResult* raycastResults = gVehicleSceneQueryData->getRaycastQueryResultBuffer(0); + const PxU32 raycastResultsSize = gVehicleSceneQueryData->getQueryResultBufferSize(); + 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(); + gPhysics->release(); + PxPvdTransport* transport = gPvd->getTransport(); + gPvd->release(); + transport->release(); + + gFoundation->release(); + + printf("SnippetVehicleScale done.\n"); +} + +void keyPress(unsigned 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; +} |