aboutsummaryrefslogtreecommitdiff
path: root/PhysX_3.4/Snippets/SnippetVehicleCommon/SnippetVehicleNoDriveCreate.cpp
diff options
context:
space:
mode:
authorgit perforce import user <a@b>2016-10-25 12:29:14 -0600
committerSheikh Dawood Abdul Ajees <Sheikh Dawood Abdul Ajees>2016-10-25 18:56:37 -0500
commit3dfe2108cfab31ba3ee5527e217d0d8e99a51162 (patch)
treefa6485c169e50d7415a651bf838f5bcd0fd3bfbd /PhysX_3.4/Snippets/SnippetVehicleCommon/SnippetVehicleNoDriveCreate.cpp
downloadphysx-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/SnippetVehicleCommon/SnippetVehicleNoDriveCreate.cpp')
-rw-r--r--PhysX_3.4/Snippets/SnippetVehicleCommon/SnippetVehicleNoDriveCreate.cpp246
1 files changed, 246 insertions, 0 deletions
diff --git a/PhysX_3.4/Snippets/SnippetVehicleCommon/SnippetVehicleNoDriveCreate.cpp b/PhysX_3.4/Snippets/SnippetVehicleCommon/SnippetVehicleNoDriveCreate.cpp
new file mode 100644
index 00000000..6f08b9dd
--- /dev/null
+++ b/PhysX_3.4/Snippets/SnippetVehicleCommon/SnippetVehicleNoDriveCreate.cpp
@@ -0,0 +1,246 @@
+// 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.
+
+#include "SnippetVehicleCreate.h"
+#include "SnippetVehicleTireFriction.h"
+#include "SnippetVehicleSceneQuery.h"
+
+
+
+namespace snippetvehicle
+{
+
+using namespace physx;
+
+namespace nodrive
+{
+
+void computeWheelCenterActorOffsets
+(const PxF32 wheelFrontZ, const PxF32 wheelRearZ, const PxVec3& chassisDims, const PxF32 wheelWidth, const PxF32 wheelRadius, const PxU32 numWheels, PxVec3* wheelCentreOffsets)
+{
+ //chassisDims.z is the distance from the rear of the chassis to the front of the chassis.
+ //The front has z = 0.5*chassisDims.z and the rear has z = -0.5*chassisDims.z.
+ //Compute a position for the front wheel and the rear wheel along the z-axis.
+ //Compute the separation between each wheel along the z-axis.
+ const PxF32 numLeftWheels = numWheels/2.0f;
+ const PxF32 deltaZ = (wheelFrontZ - wheelRearZ)/(numLeftWheels-1.0f);
+ //Set the outside of the left and right wheels to be flush with the chassis.
+ //Set the top of the wheel to be just touching the underside of the chassis.
+ for(PxU32 i = 0; i < numWheels; i+=2)
+ {
+ //Left wheel offset from origin.
+ wheelCentreOffsets[i + 0] = PxVec3((-chassisDims.x + wheelWidth)*0.5f, -(chassisDims.y/2 + wheelRadius), wheelRearZ + i*deltaZ*0.5f);
+ //Right wheel offsets from origin.
+ wheelCentreOffsets[i + 1] = PxVec3((+chassisDims.x - wheelWidth)*0.5f, -(chassisDims.y/2 + wheelRadius), wheelRearZ + i*deltaZ*0.5f);
+ }
+}
+
+void setupWheelsSimulationData
+(const PxF32 wheelMass, const PxF32 wheelMOI, const PxF32 wheelRadius, const PxF32 wheelWidth,
+ const PxU32 numWheels, const PxVec3* wheelCenterActorOffsets,
+ const PxVec3& chassisCMOffset, const PxF32 chassisMass,
+ PxVehicleWheelsSimData* wheelsSimData)
+{
+ //Set up the wheels.
+ PxVehicleWheelData wheels[PX_MAX_NB_WHEELS];
+ {
+ //Set up the wheel data structures with mass, moi, radius, width.
+ for(PxU32 i = 0; i < numWheels; i++)
+ {
+ wheels[i].mMass = wheelMass;
+ wheels[i].mMOI = wheelMOI;
+ wheels[i].mRadius = wheelRadius;
+ wheels[i].mWidth = wheelWidth;
+ }
+ }
+
+ //Set up the tires.
+ PxVehicleTireData tires[PX_MAX_NB_WHEELS];
+ {
+ //Set up the tires.
+ for(PxU32 i = 0; i < numWheels; i++)
+ {
+ tires[i].mType = TIRE_TYPE_NORMAL;
+ }
+ }
+
+ //Set up the suspensions
+ PxVehicleSuspensionData suspensions[PX_MAX_NB_WHEELS];
+ {
+ //Compute the mass supported by each suspension spring.
+ PxF32 suspSprungMasses[PX_MAX_NB_WHEELS];
+ PxVehicleComputeSprungMasses(numWheels, wheelCenterActorOffsets, chassisCMOffset, chassisMass, 1, suspSprungMasses);
+
+ //Set the suspension data.
+ for(PxU32 i = 0; i < numWheels; i++)
+ {
+ suspensions[i].mMaxCompression = 0.3f;
+ suspensions[i].mMaxDroop = 0.1f;
+ suspensions[i].mSpringStrength = 35000.0f;
+ suspensions[i].mSpringDamperRate = 4500.0f;
+ suspensions[i].mSprungMass = suspSprungMasses[i];
+ }
+
+ //Set the camber angles.
+ const PxF32 camberAngleAtRest=0.0;
+ const PxF32 camberAngleAtMaxDroop=0.01f;
+ const PxF32 camberAngleAtMaxCompression=-0.01f;
+ for(PxU32 i = 0; i < numWheels; i+=2)
+ {
+ suspensions[i + 0].mCamberAtRest = camberAngleAtRest;
+ suspensions[i + 1].mCamberAtRest = -camberAngleAtRest;
+ suspensions[i + 0].mCamberAtMaxDroop = camberAngleAtMaxDroop;
+ suspensions[i + 1].mCamberAtMaxDroop = -camberAngleAtMaxDroop;
+ suspensions[i + 0].mCamberAtMaxCompression = camberAngleAtMaxCompression;
+ suspensions[i + 1].mCamberAtMaxCompression = -camberAngleAtMaxCompression;
+ }
+ }
+
+ //Set up the wheel geometry.
+ PxVec3 suspTravelDirections[PX_MAX_NB_WHEELS];
+ PxVec3 wheelCentreCMOffsets[PX_MAX_NB_WHEELS];
+ PxVec3 suspForceAppCMOffsets[PX_MAX_NB_WHEELS];
+ PxVec3 tireForceAppCMOffsets[PX_MAX_NB_WHEELS];
+ {
+ //Set the geometry data.
+ for(PxU32 i = 0; i < numWheels; i++)
+ {
+ //Vertical suspension travel.
+ suspTravelDirections[i] = PxVec3(0,-1,0);
+
+ //Wheel center offset is offset from rigid body center of mass.
+ wheelCentreCMOffsets[i] = wheelCenterActorOffsets[i] - chassisCMOffset;
+
+ //Suspension force application point 0.3 metres below rigid body center of mass.
+ suspForceAppCMOffsets[i]=PxVec3(wheelCentreCMOffsets[i].x,-0.3f,wheelCentreCMOffsets[i].z);
+
+ //Tire force application point 0.3 metres below rigid body center of mass.
+ tireForceAppCMOffsets[i]=PxVec3(wheelCentreCMOffsets[i].x,-0.3f,wheelCentreCMOffsets[i].z);
+ }
+ }
+
+ //Set up the filter data of the raycast that will be issued by each suspension.
+ PxFilterData qryFilterData;
+ setupNonDrivableSurface(qryFilterData);
+
+ //Set the wheel, tire and suspension data.
+ //Set the geometry data.
+ //Set the query filter data
+ for(PxU32 i = 0; i < numWheels; i++)
+ {
+ wheelsSimData->setWheelData(i, wheels[i]);
+ wheelsSimData->setTireData(i, tires[i]);
+ wheelsSimData->setSuspensionData(i, suspensions[i]);
+ wheelsSimData->setSuspTravelDirection(i, suspTravelDirections[i]);
+ wheelsSimData->setWheelCentreOffset(i, wheelCentreCMOffsets[i]);
+ wheelsSimData->setSuspForceAppPointOffset(i, suspForceAppCMOffsets[i]);
+ wheelsSimData->setTireForceAppPointOffset(i, tireForceAppCMOffsets[i]);
+ wheelsSimData->setSceneQueryFilterData(i, qryFilterData);
+ wheelsSimData->setWheelShapeMapping(i, PxI32(i));
+ }
+}
+
+}// namespace nodrive
+
+PxVehicleNoDrive* createVehicleNoDrive(const VehicleDesc& vehicleDesc, PxPhysics* physics, PxCooking* cooking)
+{
+ const PxVec3 chassisDims = vehicleDesc.chassisDims;
+ const PxF32 wheelWidth = vehicleDesc.wheelWidth;
+ const PxF32 wheelRadius = vehicleDesc.wheelRadius;
+ const PxU32 numWheels = vehicleDesc.numWheels;
+
+ const PxFilterData& chassisSimFilterData = vehicleDesc.chassisSimFilterData;
+ const PxFilterData& wheelSimFilterData = vehicleDesc.wheelSimFilterData;
+
+ //Construct a physx actor with shapes for the chassis and wheels.
+ //Set the rigid body mass, moment of inertia, and center of mass offset.
+ PxRigidDynamic* vehActor = NULL;
+ {
+ //Construct a convex mesh for a cylindrical wheel.
+ PxConvexMesh* wheelMesh = createWheelMesh(wheelWidth, wheelRadius, *physics, *cooking);
+ //Assume all wheels are identical for simplicity.
+ PxConvexMesh* wheelConvexMeshes[PX_MAX_NB_WHEELS];
+ PxMaterial* wheelMaterials[PX_MAX_NB_WHEELS];
+
+ //Set the meshes and materials for the driven wheels.
+ for(PxU32 i = 0; i < numWheels; i++)
+ {
+ wheelConvexMeshes[i] = wheelMesh;
+ wheelMaterials[i] = vehicleDesc.wheelMaterial;
+ }
+
+ //Chassis just has a single convex shape for simplicity.
+ PxConvexMesh* chassisConvexMesh = createChassisMesh(chassisDims, *physics, *cooking);
+ PxConvexMesh* chassisConvexMeshes[1] = {chassisConvexMesh};
+ PxMaterial* chassisMaterials[1] = {vehicleDesc.chassisMaterial};
+
+ //Rigid body data.
+ PxVehicleChassisData rigidBodyData;
+ rigidBodyData.mMOI = vehicleDesc.chassisMOI;
+ rigidBodyData.mMass = vehicleDesc.chassisMass;
+ rigidBodyData.mCMOffset = vehicleDesc.chassisCMOffset;
+
+ vehActor = createVehicleActor
+ (rigidBodyData,
+ wheelMaterials, wheelConvexMeshes, numWheels, wheelSimFilterData,
+ chassisMaterials, chassisConvexMeshes, 1, chassisSimFilterData,
+ *physics);
+ }
+
+ //Set up the sim data for the wheels.
+ PxVehicleWheelsSimData* wheelsSimData = PxVehicleWheelsSimData::allocate(numWheels);
+ {
+ //Compute the wheel center offsets from the origin.
+ PxVec3 wheelCentreActorOffsets[PX_MAX_NB_WHEELS];
+ const PxF32 frontZ = chassisDims.z*0.3f;
+ const PxF32 rearZ = -chassisDims.z*0.3f;
+ nodrive::computeWheelCenterActorOffsets(frontZ, rearZ, chassisDims, wheelWidth, wheelRadius, numWheels, wheelCentreActorOffsets);
+
+ nodrive::setupWheelsSimulationData
+ (vehicleDesc.wheelMass, vehicleDesc.wheelMOI, wheelRadius, wheelWidth,
+ numWheels, wheelCentreActorOffsets,
+ vehicleDesc.chassisCMOffset, vehicleDesc.chassisMass,
+ wheelsSimData);
+ }
+
+ //Create a vehicle from the wheels and drive sim data.
+ PxVehicleNoDrive* vehDriveNoDrive = PxVehicleNoDrive::allocate(numWheels);
+ vehDriveNoDrive->setup(physics, vehActor, *wheelsSimData);
+
+ //Configure the userdata
+ configureUserData(vehDriveNoDrive, vehicleDesc.actorUserData, vehicleDesc.shapeUserDatas);
+
+ //Free the sim data because we don't need that any more.
+ wheelsSimData->free();
+
+ return vehDriveNoDrive;
+}
+
+} // namespace snippetvehicle
+