RavEngine
Loading...
Searching...
No Matches
PhysXIntegration.h
1// Redistribution and use in source and binary forms, with or without
2// modification, are permitted provided that the following conditions
3// are met:
4// * Redistributions of source code must retain the above copyright
5// notice, this list of conditions and the following disclaimer.
6// * Redistributions in binary form must reproduce the above copyright
7// notice, this list of conditions and the following disclaimer in the
8// documentation and/or other materials provided with the distribution.
9// * Neither the name of NVIDIA CORPORATION nor the names of its
10// contributors may be used to endorse or promote products derived
11// from this software without specific prior written permission.
12//
13// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
14// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
17// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24//
25// Copyright (c) 2008-2022 NVIDIA Corporation. All rights reserved.
26// Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
27// Copyright (c) 2001-2004 NovodeX AG. All rights reserved.
28
29#pragma once
30
31#include "PxScene.h"
32#include "vehicle2/PxVehicleAPI.h"
33#include "../base/Base.h"
34
35namespace snippetvehicle2
36{
37
38using namespace physx;
39using namespace physx::vehicle2;
40
42{
43 PxVehiclePhysXRoadGeometryQueryParams physxRoadGeometryQueryParams;
44 PxVehiclePhysXMaterialFrictionParams physxMaterialFrictionParams[PxVehicleLimits::eMAX_NB_WHEELS];
45 PxVehiclePhysXSuspensionLimitConstraintParams physxSuspensionLimitConstraintParams[PxVehicleLimits::eMAX_NB_WHEELS];
46 PxTransform physxActorCMassLocalPose;
47 PxVec3 physxActorBoxShapeHalfExtents;
48 PxTransform physxActorBoxShapeLocalPose;
49 PxTransform physxWheelShapeLocalPoses[PxVehicleLimits::eMAX_NB_WHEELS];
50
51 void create
52 (const PxVehicleAxleDescription& axleDescription,
53 const PxQueryFilterData& queryFilterData, PxQueryFilterCallback* queryFilterCallback,
54 PxVehiclePhysXMaterialFriction* materialFrictions, const PxU32 nbMaterialFrictions, const PxReal defaultFriction,
55 const PxTransform& physXActorCMassLocalPose,
56 const PxVec3& physXActorBoxShapeHalfExtents, const PxTransform& physxActorBoxShapeLocalPose);
57
58 PhysXIntegrationParams transformAndScale(
59 const PxVehicleFrame& srcFrame, const PxVehicleFrame& trgFrame, const PxVehicleScale& srcScale, const PxVehicleScale& trgScale) const;
60
61 PX_FORCE_INLINE bool isValid(const PxVehicleAxleDescription& axleDesc) const
62 {
63 if (!physxRoadGeometryQueryParams.isValid())
64 return false;
65 for (PxU32 i = 0; i < axleDesc.nbWheels; i++)
66 {
67 const PxU32 wheelId = axleDesc.wheelIdsInAxleOrder[i];
68 if (!physxMaterialFrictionParams[wheelId].isValid())
69 return false;
70 if (!physxSuspensionLimitConstraintParams[wheelId].isValid())
71 return false;
72 }
73 return true;
74 }
75};
76
78{
79 PxVehiclePhysXActor physxActor; //physx actor
80 PxVehiclePhysXSteerState physxSteerState;
81 PxVehiclePhysXConstraints physxConstraints; //susp limit and sticky tire constraints
82
83 PX_FORCE_INLINE void setToDefault()
84 {
85 physxActor.setToDefault();
86 physxSteerState.setToDefault();
87 physxConstraints.setToDefault();
88 }
89
90 void create
91 (const BaseVehicleParams& baseParams, const PhysXIntegrationParams& physxParams,
92 PxPhysics& physics, const PxCookingParams& params, PxMaterial& defaultMaterial);
93
94 void destroy();
95
96};
97
98
99void setPhysXIntegrationParams(const PxVehicleAxleDescription&,
100 PxVehiclePhysXMaterialFriction*, PxU32 nbPhysXMaterialFrictions,
101 PxReal physXDefaultMaterialFriction, PhysXIntegrationParams&);
102
103
104//
105//This class holds the parameters, state and logic needed to implement a vehicle that
106//is using a PhysX actor to potentially interact with other objects in a PhysX scene.
107//
108//See BaseVehicle for more details on the snippet code design.
109//
111 : public BaseVehicle
116{
117public:
118 bool initialize(PxPhysics& physics, const PxCookingParams& params, PxMaterial& defaultMaterial);
119 virtual void destroy();
120
121 void setUpActor(PxScene& scene, const PxTransform& pose, const char* vehicleName);
122
124 const PxVehicleAxleDescription*& axleDescription,
125 const PxVehicleCommandState*& commands,
126 const PxVehicleEngineDriveTransmissionCommandState*& transmissionCommands,
127 const PxVehicleGearboxParams*& gearParams,
128 const PxVehicleGearboxState*& gearState,
129 const PxVehicleEngineParams*& engineParams,
130 PxVehiclePhysXActor*& physxActor,
131 PxVehiclePhysXSteerState*& physxSteerState,
132 PxVehiclePhysXConstraints*& physxConstraints,
133 PxVehicleRigidBodyState*& rigidBodyState,
135 PxVehicleEngineState*& engineState)
136 {
137 axleDescription = &mBaseParams.axleDescription;
138 commands = &mCommandState;
139 physxActor = &mPhysXState.physxActor;
140 physxSteerState = &mPhysXState.physxSteerState;
141 physxConstraints = &mPhysXState.physxConstraints;
142 rigidBodyState = &mBaseState.rigidBodyState;
143 wheelRigidBody1dStates.setData(mBaseState.wheelRigidBody1dStates);
144
145 transmissionCommands = NULL;
146 gearParams = NULL;
147 gearState = NULL;
148 engineParams = NULL;
149 engineState = NULL;
150 }
151
153 const PxVehicleAxleDescription*& axleDescription,
154 const PxVehicleRigidBodyState*& rigidBodyState,
156 PxVehicleArrayData<const PxTransform>& wheelShapeLocalPoses,
159 const PxVehicleGearboxState*& gearState,
160 PxVehiclePhysXActor*& physxActor)
161 {
162 axleDescription = &mBaseParams.axleDescription;
163 rigidBodyState = &mBaseState.rigidBodyState;
164 wheelParams.setData(mBaseParams.wheelParams);
165 wheelShapeLocalPoses.setData(mPhysXParams.physxWheelShapeLocalPoses);
166 wheelRigidBody1dStates.setData(mBaseState.wheelRigidBody1dStates);
167 wheelLocalPoses.setData(mBaseState.wheelLocalPoses);
168 physxActor = &mPhysXState.physxActor;
169
170 gearState = NULL;
171 }
172
173 virtual void getDataForPhysXConstraintComponent(
174 const PxVehicleAxleDescription*& axleDescription,
175 const PxVehicleRigidBodyState*& rigidBodyState,
183 PxVehiclePhysXConstraints*& constraints)
184 {
185 axleDescription = &mBaseParams.axleDescription;
186 rigidBodyState = &mBaseState.rigidBodyState;
187 suspensionParams.setData(mBaseParams.suspensionParams);
188 suspensionLimitParams.setData(mPhysXParams.physxSuspensionLimitConstraintParams);
189 suspensionStates.setData(mBaseState.suspensionStates);
190 suspensionComplianceStates.setData(mBaseState.suspensionComplianceStates);
191 wheelRoadGeomStates.setData(mBaseState.roadGeomStates);
192 tireDirectionStates.setData(mBaseState.tireDirectionStates);
193 tireStickyStates.setData(mBaseState.tireStickyStates);
194 constraints = &mPhysXState.physxConstraints;
195 }
196
198 const PxVehicleAxleDescription*& axleDescription,
199 const PxVehiclePhysXRoadGeometryQueryParams*& roadGeomParams,
200 PxVehicleArrayData<const PxReal>& steerResponseStates,
201 const PxVehicleRigidBodyState*& rigidBodyState,
207 {
208 axleDescription = &mBaseParams.axleDescription;
209 roadGeomParams = &mPhysXParams.physxRoadGeometryQueryParams;
210 steerResponseStates.setData(mBaseState.steerCommandResponseStates);
211 rigidBodyState = &mBaseState.rigidBodyState;
212 wheelParams.setData(mBaseParams.wheelParams);
213 suspensionParams.setData(mBaseParams.suspensionParams);
214 materialFrictionParams.setData(mPhysXParams.physxMaterialFrictionParams);
215 roadGeometryStates.setData(mBaseState.roadGeomStates);
216 physxRoadGeometryStates.setEmpty();
217 }
218
219
220 //Parameters and states of the vehicle's physx integration.
221 PhysXIntegrationParams mPhysXParams;
222 PhysXIntegrationState mPhysXState;
223
224 //The commands that will control the vehicle
225 //
226 // Note that this is not related to a PhysX actor based vehicle as such but
227 // put in here to be shared by all vehicle types that will be based on this
228 // class. It keeps the code simpler for the purpose of the snippets.
229 //
230 PxVehicleCommandState mCommandState;
231};
232
233}//namespace snippetvehicle2
Material class to represent a set of surface properties.
Definition PxMaterial.h:149
Abstract singleton factory class used for instancing objects in the Physics SDK.
Definition PxPhysics.h:91
Scene query filtering callbacks.
Definition PxQueryFiltering.h:175
A scene is a collection of bodies and constraints which can interact.
Definition PxScene.h:178
class representing a rigid euclidean transform as a quaternion and a vector
Definition PxTransform.h:49
3 Element vector class.
Definition PxVec3.h:50
Work items at the beginning of an update step for a PhysX actor based vehicle.
Definition PxVehiclePhysXActorComponents.h:62
Work items at the end of an update step for a PhysX actor based vehicle.
Definition PxVehiclePhysXActorComponents.h:158
Definition PxVehiclePhysXConstraintComponents.h:57
Definition PxVehiclePhysXRoadGeometryComponents.h:56
Definition Base.h:192
Definition PhysXIntegration.h:116
virtual void getDataForPhysXRoadGeometrySceneQueryComponent(const PxVehicleAxleDescription *&axleDescription, const PxVehiclePhysXRoadGeometryQueryParams *&roadGeomParams, PxVehicleArrayData< const PxReal > &steerResponseStates, const PxVehicleRigidBodyState *&rigidBodyState, PxVehicleArrayData< const PxVehicleWheelParams > &wheelParams, PxVehicleArrayData< const PxVehicleSuspensionParams > &suspensionParams, PxVehicleArrayData< const PxVehiclePhysXMaterialFrictionParams > &materialFrictionParams, PxVehicleArrayData< PxVehicleRoadGeometryState > &roadGeometryStates, PxVehicleArrayData< PxVehiclePhysXRoadGeometryQueryState > &physxRoadGeometryStates)
Provide vehicle data items for this component.
Definition PhysXIntegration.h:197
virtual void getDataForPhysXActorBeginComponent(const PxVehicleAxleDescription *&axleDescription, const PxVehicleCommandState *&commands, const PxVehicleEngineDriveTransmissionCommandState *&transmissionCommands, const PxVehicleGearboxParams *&gearParams, const PxVehicleGearboxState *&gearState, const PxVehicleEngineParams *&engineParams, PxVehiclePhysXActor *&physxActor, PxVehiclePhysXSteerState *&physxSteerState, PxVehiclePhysXConstraints *&physxConstraints, PxVehicleRigidBodyState *&rigidBodyState, PxVehicleArrayData< PxVehicleWheelRigidBody1dState > &wheelRigidBody1dStates, PxVehicleEngineState *&engineState)
Provide vehicle data items for this component.
Definition PhysXIntegration.h:123
virtual void getDataForPhysXActorEndComponent(const PxVehicleAxleDescription *&axleDescription, const PxVehicleRigidBodyState *&rigidBodyState, PxVehicleArrayData< const PxVehicleWheelParams > &wheelParams, PxVehicleArrayData< const PxTransform > &wheelShapeLocalPoses, PxVehicleArrayData< const PxVehicleWheelRigidBody1dState > &wheelRigidBody1dStates, PxVehicleArrayData< const PxVehicleWheelLocalPose > &wheelLocalPoses, const PxVehicleGearboxState *&gearState, PxVehiclePhysXActor *&physxActor)
Provide vehicle data items for this component.
Definition PhysXIntegration.h:152
#define PX_FORCE_INLINE
Definition PxPreprocessor.h:335
Sorts an array of objects in ascending order, assuming that the predicate implements the < operator:
Definition PxBoxController.h:39
Structure describing parameters affecting mesh cooking.
Definition PxCooking.h:185
Scene query filtering data.
Definition PxQueryFiltering.h:138
Helper struct to pass array type data to vehice components and functions.
Definition PxVehicleParams.h:249
PX_FORCE_INLINE void setEmpty()
Set as empty.
Definition PxVehicleParams.h:333
PX_FORCE_INLINE void setData(T *data)
Set the data as an array of structs.
Definition PxVehicleParams.h:261
Definition PxVehicleParams.h:53
PxU32 wheelIdsInAxleOrder[PxVehicleLimits::eMAX_NB_WHEELS]
The list of all wheel ids on the vehicle.
Definition PxVehicleParams.h:149
PxU32 nbWheels
The number of wheels on the vehicle.
Definition PxVehicleParams.h:150
A description of the state of commands that are applied to the vehicle.
Definition PxVehicleCommandStates.h:53
A description of the state of transmission-related commands that are applied to a vehicle with engine...
Definition PxVehicleCommandStates.h:92
Definition PxVehicleDrivetrainParams.h:192
Definition PxVehicleDrivetrainStates.h:67
Definition PxVehicleParams.h:174
Definition PxVehicleDrivetrainParams.h:320
Definition PxVehicleDrivetrainStates.h:85
A description of the PhysX actor and shapes that represent the vehicle in an associated PxScene.
Definition PxVehiclePhysXActorStates.h:55
A mapping between constraint state data and the associated PxConstraint instances.
Definition PxVehiclePhysXConstraintStates.h:317
A mappping between PxMaterial instance and friction for multiple PxMaterial intances.
Definition PxVehiclePhysXRoadGeometryParams.h:142
Definition PxVehiclePhysXRoadGeometryParams.h:115
A description of type of PhysX scene query and the filter data to apply to the query.
Definition PxVehiclePhysXRoadGeometryParams.h:70
A description of the previous steer command applied to the vehicle.
Definition PxVehiclePhysXActorStates.h:80
A description of the PhysX models employed to resolve suspension limit constraints.
Definition PxVehiclePhysXConstraintParams.h:52
Definition PxVehicleRigidBodyStates.h:47
Definition PxVehicleParams.h:224
Definition PhysXIntegration.h:42
Definition PhysXIntegration.h:78