// // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions // are met: // * Redistributions of source code must retain the above copyright // notice, this list of conditions and the following disclaimer. // * Redistributions in binary form must reproduce the above copyright // notice, this list of conditions and the following disclaimer in the // documentation and/or other materials provided with the distribution. // * Neither the name of NVIDIA CORPORATION nor the names of its // contributors may be used to endorse or promote products derived // from this software without specific prior written permission. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY // OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // // Copyright (c) 2018 NVIDIA Corporation. All rights reserved. #include "SceneVehicleSceneQuery.h" #include "VehicleManager.h" #include "VehicleWheelQueryResults.h" #include "vehicle/PxVehicleUtilSetup.h" #include "PxRigidActorExt.h" #include "PxSceneLock.h" #include "PxD6Joint.h" VehicleManager::VehicleManager() : mSqWheelRaycastBatchQuery(NULL) { } VehicleManager::~VehicleManager() { PxCloseVehicleSDK(); } void VehicleManager::init(PxPhysics& physics, const PxMaterial** drivableSurfaceMaterials, const PxVehicleDrivableSurfaceType* drivableSurfaceTypes) { //Initialise the sdk. PxInitVehicleSDK(physics, NULL); //Set the basis vectors. PxVec3 up(0, 1, 0); PxVec3 forward(0, 0, 1); PxVehicleSetBasisVectors(up, forward); //Set the vehicle update mode to be immediate velocity changes. PxVehicleSetUpdateMode(PxVehicleUpdateMode::eVELOCITY_CHANGE); //Initialise vehicle ptrs to null. mVehicle = NULL; //Allocate simulation data so we can switch from 3-wheeled to 4-wheeled cars by switching simulation data. mWheelsSimData4W = PxVehicleWheelsSimData::allocate(4); //Scene query data for to allow raycasts for all suspensions of all vehicles. mSqData = VehicleSceneQueryData::allocate(4); //Data to store reports for each wheel. mWheelQueryResults = VehicleWheelQueryResults::allocate(4); //Set up the friction values arising from combinations of tire type and surface type. mSurfaceTirePairs = PxVehicleDrivableSurfaceToTireFrictionPairs::allocate(MAX_NUM_TIRE_TYPES, MAX_NUM_SURFACE_TYPES); mSurfaceTirePairs->setup(MAX_NUM_TIRE_TYPES, MAX_NUM_SURFACE_TYPES, drivableSurfaceMaterials, drivableSurfaceTypes); for (PxU32 i = 0; isetTypePairFriction(i, j, 1.3f*TireFrictionMultipliers::getValue(i, j)); } } } void VehicleManager::computeWheelWidthsAndRadii(PxConvexMesh** wheelConvexMeshes, PxF32* wheelWidths, PxF32* wheelRadii) { for (PxU32 i = 0; i<4; i++) { const PxU32 numWheelVerts = wheelConvexMeshes[i]->getNbVertices(); const PxVec3* wheelVerts = wheelConvexMeshes[i]->getVertices(); PxVec3 wheelMin(PX_MAX_F32, PX_MAX_F32, PX_MAX_F32); PxVec3 wheelMax(-PX_MAX_F32, -PX_MAX_F32, -PX_MAX_F32); for (PxU32 j = 0; jgetNbVertices(); const PxVec3* chassisVerts = chassisConvexMesh->getVertices(); PxVec3 chassisMin(PX_MAX_F32, PX_MAX_F32, PX_MAX_F32); PxVec3 chassisMax(-PX_MAX_F32, -PX_MAX_F32, -PX_MAX_F32); for (PxU32 i = 0; isetQueryFilterData(vehQryFilterData); wheelShape->setFlag(PxShapeFlag::eSCENE_QUERY_SHAPE, false); //wheelShape->setFlag(PxShapeFlag::eSIMULATION_SHAPE, false); wheelShape->setSimulationFilterData(wheelCollFilterData); wheelShape->setLocalPose(wheelLocalPoses[i]); } //Add the chassis shapes to the actor. for (PxU32 i = 0; isetFlag(PxShapeFlag::eSCENE_QUERY_SHAPE, false); chassisShape->setQueryFilterData(vehQryFilterData); chassisShape->setSimulationFilterData(chassisCollFilterData); chassisShape->setLocalPose(chassisLocalPoses[i]); } vehActor->setMass(chassisData.mMass); vehActor->setMassSpaceInertiaTensor(chassisData.mMOI); vehActor->setCMassLocalPose(PxTransform(chassisData.mCMOffset, PxQuat(PxIdentity))); } PxRigidDynamic* createVehicleActor4W (const PxVehicleChassisData& chassisData, PxConvexMesh** wheelConvexMeshes, PxConvexMesh* chassisConvexMesh, PxScene& scene, PxPhysics& physics, const PxMaterial& material) { //We need a rigid body actor for the vehicle. //Don't forget to add the actor the scene after setting up the associated vehicle. PxRigidDynamic* vehActor = physics.createRigidDynamic(PxTransform(PxIdentity)); //We need to add wheel collision shapes, their local poses, a material for the wheels, and a simulation filter for the wheels. PxConvexMeshGeometry frontLeftWheelGeom(wheelConvexMeshes[0]); PxConvexMeshGeometry frontRightWheelGeom(wheelConvexMeshes[1]); PxConvexMeshGeometry rearLeftWheelGeom(wheelConvexMeshes[2]); PxConvexMeshGeometry rearRightWheelGeom(wheelConvexMeshes[3]); const PxGeometry* wheelGeometries[4] = { &frontLeftWheelGeom, &frontRightWheelGeom, &rearLeftWheelGeom, &rearRightWheelGeom }; const PxTransform wheelLocalPoses[4] = { PxTransform(PxIdentity), PxTransform(PxIdentity), PxTransform(PxIdentity), PxTransform(PxIdentity) }; const PxMaterial& wheelMaterial = material; PxFilterData wheelCollFilterData; wheelCollFilterData.word0 = COLLISION_FLAG_WHEEL; wheelCollFilterData.word1 = COLLISION_FLAG_WHEEL_AGAINST; wheelCollFilterData.word2 = PxPairFlag::eMODIFY_CONTACTS; //We need to add chassis collision shapes, their local poses, a material for the chassis, and a simulation filter for the chassis. PxConvexMeshGeometry chassisConvexGeom(chassisConvexMesh); const PxGeometry* chassisGeoms[1] = { &chassisConvexGeom }; const PxTransform chassisLocalPoses[1] = { PxTransform(PxIdentity) }; const PxMaterial& chassisMaterial = material; PxFilterData chassisCollFilterData; chassisCollFilterData.word0 = COLLISION_FLAG_CHASSIS; chassisCollFilterData.word1 = COLLISION_FLAG_CHASSIS_AGAINST; //Create a query filter data for the car to ensure that cars //do not attempt to drive on themselves. PxFilterData vehQryFilterData; VehicleSetupVehicleShapeQueryFilterData(&vehQryFilterData); //Set up the physx rigid body actor with shapes, local poses, and filters. setupActor (vehActor, vehQryFilterData, wheelGeometries, wheelLocalPoses, 4, &wheelMaterial, wheelCollFilterData, chassisGeoms, chassisLocalPoses, 1, &chassisMaterial, chassisCollFilterData, chassisData, &physics); return vehActor; } void VehicleManager::resetNWCar(const PxTransform& startTransform, PxVehicleWheels* vehWheels) { PxVehicleDrive4W* vehDrive4W = (PxVehicleDrive4W*)vehWheels; //Set the car back to its rest state. vehDrive4W->setToRestState(); //Set the car to first gear. vehDrive4W->mDriveDynData.forceGearChange(PxVehicleGearsData::eFIRST); //Set the car's transform to be the start transform. PxRigidDynamic* actor = vehWheels->getRigidDynamicActor(); PxSceneWriteLock scopedLock(*actor->getScene()); actor->setGlobalPose(startTransform); } void VehicleManager::suspensionRaycasts(PxScene* scene) { //Create a scene query if we haven't already done so. if (NULL == mSqWheelRaycastBatchQuery) { mSqWheelRaycastBatchQuery = mSqData->setUpBatchedSceneQuery(scene); } //Raycasts. PxSceneReadLock scopedLock(*scene); PxVehicleWheels* vehicles[1] = { mVehicle }; PxVehicleSuspensionRaycasts(mSqWheelRaycastBatchQuery, 1, vehicles, mSqData->getRaycastQueryResultBufferSize(), mSqData->getRaycastQueryResultBuffer()); } void VehicleManager::suspensionSweeps(PxScene* scene) { //Create a scene query if we haven't already done so. if (NULL == mSqWheelRaycastBatchQuery) { mSqWheelRaycastBatchQuery = mSqData->setUpBatchedSceneQuerySweep(scene); } //Raycasts. PxSceneReadLock scopedLock(*scene); PxVehicleWheels* vehicles[1] = { mVehicle }; PxVehicleSuspensionSweeps(mSqWheelRaycastBatchQuery, 1, vehicles, mSqData->getSweepQueryResultBufferSize(), mSqData->getSweepQueryResultBuffer(), 1); } void VehicleManager::update(const PxF32 timestep, const PxVec3& gravity) { PxVehicleWheels* vehicles[1] = { mVehicle }; PxVehicleUpdates(timestep, gravity, *mSurfaceTirePairs, 1, vehicles, &mVehicleWheelQueryResults); } void VehicleManager::create4WVehicle(PxScene& scene, PxPhysics& physics, PxCooking& cooking, const PxMaterial& material, const PxF32 chassisMass, const PxVec3* wheelCentreOffsets4, PxConvexMesh* chassisConvexMesh, PxConvexMesh** wheelConvexMeshes4, const PxTransform& startTransform, const bool useAutoGearFlag) { PxVehicleWheelsSimData* wheelsSimData = PxVehicleWheelsSimData::allocate(4); PxVehicleDriveSimData4W driveSimData; PxVehicleChassisData chassisData; createVehicle4WSimulationData (chassisMass, chassisConvexMesh, 20.0f, wheelConvexMeshes4, wheelCentreOffsets4, *wheelsSimData, driveSimData, chassisData); //Instantiate and finalize the vehicle using physx. PxRigidDynamic* vehActor = createVehicleActor4W(chassisData, wheelConvexMeshes4, chassisConvexMesh, scene, physics, material); //Create a car. PxVehicleDrive4W* car = PxVehicleDrive4W::allocate(4); car->setup(&physics, vehActor, *wheelsSimData, driveSimData, 0); //Free the sim data because we don't need that any more. wheelsSimData->free(); //Don't forget to add the actor to the scene. { PxSceneWriteLock scopedLock(scene); scene.addActor(*vehActor); } //Set up the mapping between wheel and actor shape. car->mWheelsSimData.setWheelShapeMapping(0, 0); car->mWheelsSimData.setWheelShapeMapping(1, 1); car->mWheelsSimData.setWheelShapeMapping(2, 2); car->mWheelsSimData.setWheelShapeMapping(3, 3); //Set up the scene query filter data for each suspension line. PxFilterData vehQryFilterData; VehicleSetupVehicleShapeQueryFilterData(&vehQryFilterData); car->mWheelsSimData.setSceneQueryFilterData(0, vehQryFilterData); car->mWheelsSimData.setSceneQueryFilterData(1, vehQryFilterData); car->mWheelsSimData.setSceneQueryFilterData(2, vehQryFilterData); car->mWheelsSimData.setSceneQueryFilterData(3, vehQryFilterData); //Set the autogear mode of the instantiate car. car->mDriveDynData.setUseAutoGears(useAutoGearFlag); car->mDriveDynData.forceGearChange(PxVehicleGearsData::eFIRST); //Increment the number of vehicles mVehicle = car; mVehicleWheelQueryResults.nbWheelQueryResults = 4; mVehicleWheelQueryResults.wheelQueryResults = mWheelQueryResults->addVehicle(4); PxQuat rotation(3.1415 / 2.f, PxVec3(0.f, 0.f, 1.f)); PxD6Joint* joint = PxD6JointCreate(physics, vehActor, PxTransform(rotation), NULL, PxTransform(rotation)); joint->setMotion(PxD6Axis::eX, PxD6Motion::eFREE); joint->setMotion(PxD6Axis::eY, PxD6Motion::eFREE); joint->setMotion(PxD6Axis::eZ, PxD6Motion::eFREE); joint->setMotion(PxD6Axis::eTWIST, PxD6Motion::eFREE); joint->setMotion(PxD6Axis::eSWING1, PxD6Motion::eLIMITED); joint->setMotion(PxD6Axis::eSWING2, PxD6Motion::eLIMITED); PxJointLimitCone limitCone(3.1415/4.f, 3.1415 / 4.f); joint->setSwingLimit(limitCone); }