Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2015
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.27 KB | None | 0 0
  1. // This code contains NVIDIA Confidential Information and is disclosed to you
  2. // under a form of NVIDIA software license agreement provided separately to you.
  3. //
  4. // Notice
  5. // NVIDIA Corporation and its licensors retain all intellectual property and
  6. // proprietary rights in and to this software and related documentation and
  7. // any modifications thereto. Any use, reproduction, disclosure, or
  8. // distribution of this software and related documentation without an express
  9. // license agreement from NVIDIA Corporation is strictly prohibited.
  10. //
  11. // ALL NVIDIA DESIGN SPECIFICATIONS, CODE ARE PROVIDED "AS IS.". NVIDIA MAKES
  12. // NO WARRANTIES, EXPRESSED, IMPLIED, STATUTORY, OR OTHERWISE WITH RESPECT TO
  13. // THE MATERIALS, AND EXPRESSLY DISCLAIMS ALL IMPLIED WARRANTIES OF NONINFRINGEMENT,
  14. // MERCHANTABILITY, AND FITNESS FOR A PARTICULAR PURPOSE.
  15. //
  16. // Information and code furnished is believed to be accurate and reliable.
  17. // However, NVIDIA Corporation assumes no responsibility for the consequences of use of such
  18. // information or for any infringement of patents or other rights of third parties that may
  19. // result from its use. No license is granted by implication or otherwise under any patent
  20. // or patent rights of NVIDIA Corporation. Details are subject to change without notice.
  21. // This code supersedes and replaces all information previously supplied.
  22. // NVIDIA Corporation products are not authorized for use as critical
  23. // components in life support devices or systems without express written approval of
  24. // NVIDIA Corporation.
  25. //
  26. // Copyright (c) 2008-2014 NVIDIA Corporation. All rights reserved.
  27. // Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
  28. // Copyright (c) 2001-2004 NovodeX AG. All rights reserved.  
  29.  
  30. // ****************************************************************************
  31. // This snippet illustrates simple use of PxVehicleDrive4W.
  32. //
  33. // It creates a vehicle on a plane and then controls the vehicle so that it performs a
  34. // number of choreographed manoeuvres such as accelerate, reverse, brake, handbrake, and turn.
  35.  
  36. // It is a good idea to record and playback with pvd (PhysX Visual Debugger).
  37. // ****************************************************************************
  38.  
  39. #include <ctype.h>
  40.  
  41. #include "PxPhysicsAPI.h"
  42.  
  43. #include "vehicle/PxVehicleUtil.h"
  44. #include "../SnippetVehicleCommon/SnippetVehicleRaycast.h"
  45. #include "../SnippetVehicleCommon/SnippetVehicleFilterShader.h"
  46. #include "../SnippetVehicleCommon/SnippetVehicleTireFriction.h"
  47. #include "../SnippetVehicleCommon/SnippetVehicleCreate.h"
  48.  
  49. #include "../SnippetCommon/SnippetPrint.h"
  50. #include "../SnippetCommon/SnippetPVD.h"
  51. #include "../SnippetUtils/SnippetUtils.h"
  52.  
  53.  
  54. using namespace physx;
  55.  
  56. PxDefaultAllocator      gAllocator;
  57. PxDefaultErrorCallback  gErrorCallback;
  58.  
  59. PxFoundation*           gFoundation = NULL;
  60. PxPhysics*              gPhysics    = NULL;
  61.  
  62. PxDefaultCpuDispatcher* gDispatcher = NULL;
  63. PxScene*                gScene      = NULL;
  64.  
  65. PxCooking*              gCooking    = NULL;
  66.  
  67. PxMaterial*             gMaterial   = NULL;
  68.  
  69. PxVisualDebuggerConnection*      
  70.                         gConnection = NULL;
  71.  
  72. VehicleSceneQueryData*  gVehicleSceneQueryData = NULL;
  73. PxBatchQuery*           gBatchQuery = NULL;
  74.  
  75. PxVehicleDrivableSurfaceToTireFrictionPairs* gFrictionPairs = NULL;
  76.  
  77. PxRigidStatic*          gGroundPlane = NULL;
  78. PxVehicleDrive4W*       gVehicle4W      = NULL;
  79.  
  80. bool                    gIsVehicleInAir = true;
  81.  
  82. PxF32 gSteerVsForwardSpeedData[2*8]=
  83. {
  84.     0.0f,       0.75f,
  85.     5.0f,       0.75f,
  86.     30.0f,      0.125f,
  87.     120.0f,     0.1f,
  88.     PX_MAX_F32, PX_MAX_F32,
  89.     PX_MAX_F32, PX_MAX_F32,
  90.     PX_MAX_F32, PX_MAX_F32,
  91.     PX_MAX_F32, PX_MAX_F32
  92. };
  93. PxFixedSizeLookupTable<8> gSteerVsForwardSpeedTable(gSteerVsForwardSpeedData,4);
  94.  
  95. PxVehicleKeySmoothingData gKeySmoothingData=
  96. {
  97.     {
  98.         6.0f,   //rise rate eANALOG_INPUT_ACCEL
  99.         6.0f,   //rise rate eANALOG_INPUT_BRAKE    
  100.         6.0f,   //rise rate eANALOG_INPUT_HANDBRAKE
  101.         2.5f,   //rise rate eANALOG_INPUT_STEER_LEFT
  102.         2.5f,   //rise rate eANALOG_INPUT_STEER_RIGHT
  103.     },
  104.     {
  105.         10.0f//fall rate eANALOG_INPUT_ACCEL
  106.         10.0f//fall rate eANALOG_INPUT_BRAKE    
  107.         10.0f//fall rate eANALOG_INPUT_HANDBRAKE
  108.         5.0f,   //fall rate eANALOG_INPUT_STEER_LEFT
  109.         5.0f    //fall rate eANALOG_INPUT_STEER_RIGHT
  110.     }
  111. };
  112.  
  113. PxVehiclePadSmoothingData gPadSmoothingData=
  114. {
  115.     {
  116.         6.0f,   //rise rate eANALOG_INPUT_ACCEL
  117.         6.0f,   //rise rate eANALOG_INPUT_BRAKE    
  118.         6.0f,   //rise rate eANALOG_INPUT_HANDBRAKE
  119.         2.5f,   //rise rate eANALOG_INPUT_STEER_LEFT
  120.         2.5f,   //rise rate eANALOG_INPUT_STEER_RIGHT
  121.     },
  122.     {
  123.         10.0f//fall rate eANALOG_INPUT_ACCEL
  124.         10.0f//fall rate eANALOG_INPUT_BRAKE    
  125.         10.0f//fall rate eANALOG_INPUT_HANDBRAKE
  126.         5.0f,   //fall rate eANALOG_INPUT_STEER_LEFT
  127.         5.0f    //fall rate eANALOG_INPUT_STEER_RIGHT
  128.     }
  129. };
  130.  
  131. PxVehicleDrive4WRawInputData gVehicleInputData;
  132.  
  133. VehicleDesc initVehicleDesc()
  134. {
  135.     //Set up the chassis mass, dimensions, moment of inertia, and center of mass offset.
  136.     //The moment of inertia is just the moment of inertia of a cuboid but modified for easier steering.
  137.     //Center of mass offset is 0.65m above the base of the chassis and 0.25m towards the front.
  138.     const PxF32 chassisMass = 1500.0f;
  139.     const PxVec3 chassisDims(2.5f,2.0f,5.0f);
  140.     const PxVec3 chassisMOI
  141.         ((chassisDims.y*chassisDims.y + chassisDims.z*chassisDims.z)*chassisMass/12.0f,
  142.          (chassisDims.x*chassisDims.x + chassisDims.z*chassisDims.z)*0.8f*chassisMass/12.0f,
  143.          (chassisDims.x*chassisDims.x + chassisDims.y*chassisDims.y)*chassisMass/12.0f);
  144.     const PxVec3 chassisCMOffset(0.0f, -chassisDims.y*0.5f + 0.65f, 0.25f);
  145.  
  146.     //Set up the wheel mass, radius, width, moment of inertia, and number of wheels.
  147.     //Moment of inertia is just the moment of inertia of a cylinder.
  148.     const PxF32 wheelMass = 20.0f;
  149.     const PxF32 wheelRadius = 0.5f;
  150.     const PxF32 wheelWidth = 0.4f;
  151.     const PxF32 wheelMOI = 0.5f*wheelMass*wheelRadius*wheelRadius;
  152.     const PxU32 nbWheels = 4;
  153.  
  154.     VehicleDesc vehicleDesc;
  155.     vehicleDesc.chassisMass = chassisMass;
  156.     vehicleDesc.chassisDims = chassisDims;
  157.     vehicleDesc.chassisMOI = chassisMOI;
  158.     vehicleDesc.chassisCMOffset = chassisCMOffset;
  159.     vehicleDesc.chassisMaterial = gMaterial;
  160.     vehicleDesc.wheelMass = wheelMass;
  161.     vehicleDesc.wheelRadius = wheelRadius;
  162.     vehicleDesc.wheelWidth = wheelWidth;
  163.     vehicleDesc.wheelMOI = wheelMOI;
  164.     vehicleDesc.numWheels = nbWheels;
  165.     vehicleDesc.wheelMaterial = gMaterial;
  166.     return vehicleDesc;
  167. }
  168.  
  169. void initPhysics()
  170. {
  171.     gFoundation = PxCreateFoundation(PX_PHYSICS_VERSION, gAllocator, gErrorCallback);
  172.     PxProfileZoneManager* profileZoneManager = &PxProfileZoneManager::createProfileZoneManager(gFoundation);
  173.     gPhysics = PxCreatePhysics(PX_PHYSICS_VERSION, *gFoundation, PxTolerancesScale(),true, profileZoneManager);
  174.  
  175.     if(gPhysics->getPvdConnectionManager())
  176.     {
  177.         gPhysics->getVisualDebugger()->setVisualizeConstraints(true);
  178.         gPhysics->getVisualDebugger()->setVisualDebuggerFlag(PxVisualDebuggerFlag::eTRANSMIT_CONTACTS, true);
  179.         gPhysics->getVisualDebugger()->setVisualDebuggerFlag(PxVisualDebuggerFlag::eTRANSMIT_SCENEQUERIES, true);  
  180.         gConnection = PxVisualDebuggerExt::createConnection(gPhysics->getPvdConnectionManager(), PVD_HOST, 5425, 10);
  181.     }
  182.  
  183.     PxSceneDesc sceneDesc(gPhysics->getTolerancesScale());
  184.     sceneDesc.gravity = PxVec3(0.0f, -9.81f, 0.0f);
  185.    
  186.     PxU32 numWorkers = 1;
  187.     gDispatcher = PxDefaultCpuDispatcherCreate(numWorkers);
  188.     sceneDesc.cpuDispatcher = gDispatcher;
  189.     sceneDesc.filterShader  = VehicleFilterShader;
  190.  
  191.     gScene = gPhysics->createScene(sceneDesc);
  192.  
  193.     gMaterial = gPhysics->createMaterial(0.5f, 0.5f, 0.6f);
  194.  
  195.     gCooking =  PxCreateCooking(PX_PHYSICS_VERSION, *gFoundation, PxCookingParams(PxTolerancesScale()));   
  196.  
  197.     /////////////////////////////////////////////
  198.  
  199.     PxInitVehicleSDK(*gPhysics);
  200.     PxVehicleSetBasisVectors(PxVec3(0,1,0), PxVec3(0,0,1));
  201.     PxVehicleSetUpdateMode(PxVehicleUpdateMode::eVELOCITY_CHANGE);
  202.  
  203.     //Create the batched scene queries for the suspension raycasts.
  204.     gVehicleSceneQueryData = VehicleSceneQueryData::allocate(1, PX_MAX_NB_WHEELS, 1, gAllocator);
  205.     gBatchQuery = VehicleSceneQueryData::setUpBatchedSceneQuery(0, *gVehicleSceneQueryData, gScene);
  206.  
  207.     //Create the friction table for each combination of tire and surface type.
  208.     gFrictionPairs = createFrictionPairs(gMaterial);
  209.    
  210.     //Create a plane to drive on.
  211.     gGroundPlane = createDrivablePlane(gMaterial, gPhysics);
  212.     gScene->addActor(*gGroundPlane);
  213.  
  214.     //Create a vehicle that will drive on the plane.
  215.     VehicleDesc vehicleDesc = initVehicleDesc();
  216.     gVehicle4W = createVehicle4W(vehicleDesc, gPhysics, gCooking);
  217.     PxTransform startTransform(PxVec3(0, (vehicleDesc.chassisDims.y*0.5f + vehicleDesc.wheelRadius + 1.0f), 0), PxQuat(PxIdentity));
  218.     gVehicle4W->getRigidDynamicActor()->setGlobalPose(startTransform);
  219.     gScene->addActor(*gVehicle4W->getRigidDynamicActor());
  220.  
  221.     //Set the vehicle to rest in first gear.
  222.     //Set the vehicle to use auto-gears.
  223.     gVehicle4W->setToRestState();
  224.     gVehicle4W->mDriveDynData.forceGearChange(PxVehicleGearsData::eFIRST);
  225.     gVehicle4W->mDriveDynData.setUseAutoGears(true);
  226.  
  227.     gVehicleInputData.setAnalogAccel(1.0f);
  228. }
  229.  
  230. void stepPhysics()
  231. {
  232.     const PxF32 timestep = 1.0f/60.0f;
  233.  
  234.     //Cycle through the driving modes to demonstrate how to accelerate/reverse/brake/turn etc.
  235.     //incrementDrivingMode(timestep);
  236.     //Update the control inputs for the vehicle.
  237.  
  238.     //PxVehicleDrive4WSmoothDigitalRawInputsAndSetAnalogInputs(gKeySmoothingData, gSteerVsForwardSpeedTable, gVehicleInputData, timestep, gIsVehicleInAir, *gVehicle4W);
  239.    
  240.     PxVehicleDrive4WSmoothAnalogRawInputsAndSetAnalogInputs(gPadSmoothingData, gSteerVsForwardSpeedTable, gVehicleInputData, timestep, gIsVehicleInAir, *gVehicle4W);
  241.  
  242.     //Raycasts.
  243.     PxVehicleWheels* vehicles[1] = {gVehicle4W};
  244.     PxRaycastQueryResult* raycastResults = gVehicleSceneQueryData->getRaycastQueryResultBuffer(0);
  245.     const PxU32 raycastResultsSize = gVehicleSceneQueryData->getRaycastQueryResultBufferSize();
  246.     PxVehicleSuspensionRaycasts(gBatchQuery, 1, vehicles, raycastResultsSize, raycastResults);
  247.  
  248.     //Vehicle update.
  249.     const PxVec3 grav = gScene->getGravity();
  250.     PxWheelQueryResult wheelQueryResults[PX_MAX_NB_WHEELS];
  251.     PxVehicleWheelQueryResult vehicleQueryResults[1] = {{wheelQueryResults, gVehicle4W->mWheelsSimData.getNbWheels()}};
  252.     PxVehicleUpdates(timestep, grav, *gFrictionPairs, 1, vehicles, vehicleQueryResults);
  253.  
  254.     //Work out if the vehicle is in the air.
  255.     gIsVehicleInAir = gVehicle4W->getRigidDynamicActor()->isSleeping() ? false : PxVehicleIsInAir(vehicleQueryResults[0]);
  256.  
  257.     //Scene update.
  258.     gScene->simulate(timestep);
  259.     gScene->fetchResults(true);
  260. }
  261.    
  262. void cleanupPhysics()
  263. {
  264.     gVehicle4W->getRigidDynamicActor()->release();
  265.     gVehicle4W->free();
  266.     gGroundPlane->release();
  267.     gBatchQuery->release();
  268.     gVehicleSceneQueryData->free(gAllocator);
  269.     gFrictionPairs->release();
  270.     PxCloseVehicleSDK();
  271.  
  272.     gMaterial->release();
  273.     gCooking->release();
  274.     gScene->release();
  275.     gDispatcher->release();
  276.     PxProfileZoneManager* profileZoneManager = gPhysics->getProfileZoneManager();
  277.     if(gConnection != NULL)
  278.         gConnection->release();
  279.     gPhysics->release();   
  280.     profileZoneManager->release();
  281.     gFoundation->release();
  282.  
  283.     printf("SnippetVehicle4W done.\n");
  284. }
  285.  
  286. void keyPress(const char key, const PxTransform& camera)
  287. {
  288.     PX_UNUSED(camera);
  289.     PX_UNUSED(key);
  290. }
  291.  
  292. int snippetMain(int, const char*const*)
  293. {
  294. #ifdef RENDER_SNIPPET
  295.     extern void renderLoop();
  296.     renderLoop();
  297. #else
  298.     initPhysics();
  299.     while(!gVehicleOrderComplete)
  300.     {
  301.         stepPhysics();
  302.     }
  303.     cleanupPhysics();
  304. #endif
  305.  
  306.     return 0;
  307. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement