RavEngine
Loading...
Searching...
No Matches
PxVehicleFunctions.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
34#include "foundation/PxTransform.h"
35#include "foundation/PxMat33.h"
36#include "foundation/PxSimpleTypes.h"
37#include "PxRigidBody.h"
38#include "PxVehicleParams.h"
39#include "roadGeometry/PxVehicleRoadGeometryState.h"
40#include "rigidBody/PxVehicleRigidBodyStates.h"
41#include "physxRoadGeometry/PxVehiclePhysXRoadGeometryState.h"
42#include "physxActor/PxVehiclePhysXActorStates.h"
43
44#if !PX_DOXYGEN
45namespace physx
46{
47namespace vehicle2
48{
49#endif
50PX_FORCE_INLINE PxVec3 PxVehicleTransformFrameToFrame
51(const PxVehicleFrame& srcFrame, const PxVehicleFrame& trgFrame, const PxVec3& v)
52{
53 PxVec3 result = v;
54 if ((srcFrame.lngAxis != trgFrame.lngAxis) || (srcFrame.latAxis != trgFrame.latAxis) || (srcFrame.vrtAxis != trgFrame.vrtAxis))
55 {
56 const PxMat33 a = srcFrame.getFrame();
57 const PxMat33 r = trgFrame.getFrame();
58 result = (r * a.getTranspose() * v);
59 }
60 return result;
61}
62
63PX_FORCE_INLINE PxVec3 PxVehicleTransformFrameToFrame
64(const PxVehicleFrame& srcFrame, const PxVehicleFrame& trgFrame,
65 const PxVehicleScale& srcScale, const PxVehicleScale& trgScale,
66 const PxVec3& v)
67{
68 PxVec3 result = PxVehicleTransformFrameToFrame(srcFrame, trgFrame, v);
69 if((srcScale.scale != trgScale.scale))
70 result *= (trgScale.scale / srcScale.scale);
71 return result;
72}
73
74PX_FORCE_INLINE PxTransform PxVehicleTransformFrameToFrame
75(const PxVehicleFrame& srcFrame, const PxVehicleFrame& trgFrame,
76 const PxVehicleScale& srcScale, const PxVehicleScale& trgScale,
77 const PxTransform& v)
78{
79 PxTransform result(PxVehicleTransformFrameToFrame(srcFrame, trgFrame, srcScale, trgScale, v.p), v.q);
80 if ((srcFrame.lngAxis != trgFrame.lngAxis) || (srcFrame.latAxis != trgFrame.latAxis) || (srcFrame.vrtAxis != trgFrame.vrtAxis))
81 {
82 PxF32 angle;
83 PxVec3 axis;
84 v.q.toRadiansAndUnitAxis(angle, axis);
85 result.q = PxQuat(angle, PxVehicleTransformFrameToFrame(srcFrame, trgFrame, axis));
86 }
87 return result;
88}
89
90PX_FORCE_INLINE PxVec3 PxVehicleComputeTranslation(const PxVehicleFrame& frame, const PxReal lng, const PxReal lat, const PxReal vrt)
91{
92 const PxVec3 v = frame.getFrame()*PxVec3(lng, lat, vrt);
93 return v;
94}
95
96PX_FORCE_INLINE PxQuat PxVehicleComputeRotation(const PxVehicleFrame& frame, const PxReal roll, const PxReal pitch, const PxReal yaw)
97{
98 const PxMat33 m = frame.getFrame();
99 const PxVec3& lngAxis = m.column0;
100 const PxVec3& latAxis = m.column1;
101 const PxVec3& vrtAxis = m.column2;
102 const PxQuat quatPitch(pitch, latAxis);
103 const PxQuat quatRoll(roll, lngAxis);
104 const PxQuat quatYaw(yaw, vrtAxis);
105 const PxQuat result = quatYaw * quatRoll * quatPitch;
106 return result;
107}
108
109PX_FORCE_INLINE PxF32 PxVehicleComputeSign(const PxReal f)
110{
111 return physx::intrinsics::fsel(f, physx::intrinsics::fsel(-f, 0.0f, 1.0f), -1.0f);
112}
113
114
136(const PxVehicleAxleDescription& axleDesc, const PxVec3& shift,
137 PxVehicleRigidBodyState& rigidBodyState, PxVehicleRoadGeometryState* roadGeometryStates,
138 PxVehiclePhysXActor* physxActor = NULL, PxVehiclePhysXRoadGeometryQueryState* physxQueryStates = NULL)
139{
140 //Adjust the vehicle's internal pose.
141 rigidBodyState.pose.p -= shift;
142
143 //Optionally adjust the PxRigidActor pose.
144 if (physxActor && !physxActor->rigidBody->getScene())
145 {
146 const PxTransform oldPose = physxActor->rigidBody->getGlobalPose();
147 const PxTransform newPose(oldPose.p - shift, oldPose.q);
148 physxActor->rigidBody->setGlobalPose(newPose);
149 }
150
151 for (PxU32 i = 0; i < axleDesc.nbWheels; i++)
152 {
153 const PxU32 wheelId = axleDesc.wheelIdsInAxleOrder[i];
154
155 //Optionally adjust the hit position.
156 if (physxQueryStates && physxQueryStates[wheelId].actor)
157 physxQueryStates[wheelId].hitPosition -= shift;
158
159 //Adjust the hit plane.
160 if (roadGeometryStates[wheelId].hitState)
161 {
162 const PxPlane plane = roadGeometryStates[wheelId].plane;
163 PxU32 largestNormalComponentAxis = 0;
164 PxReal largestNormalComponent = 0.0f;
165 const PxF32 normalComponents[3] = { plane.n.x, plane.n.y, plane.n.z };
166 for (PxU32 k = 0; k < 3; k++)
167 {
168 if (PxAbs(normalComponents[k]) > largestNormalComponent)
169 {
170 largestNormalComponent = PxAbs(normalComponents[k]);
171 largestNormalComponentAxis = k;
172 }
173 }
174 PxVec3 pointInPlane(PxZero);
175 switch (largestNormalComponentAxis)
176 {
177 case 0:
178 pointInPlane.x = -plane.d / plane.n.x;
179 break;
180 case 1:
181 pointInPlane.y = -plane.d / plane.n.y;
182 break;
183 case 2:
184 pointInPlane.z = -plane.d / plane.n.z;
185 break;
186 default:
187 break;
188 }
189 roadGeometryStates[wheelId].plane.d = -plane.n.dot(pointInPlane - shift);
190 }
191 }
192}
193
194#if !PX_DOXYGEN
195} // namespace vehicle2
196} // namespace physx
197#endif
198
GLM_FUNC_DECL T angle(qua< T, Q > const &x)
Definition quaternion_trigonometric.inl:6
#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
PX_CUDA_CALLABLE PX_FORCE_INLINE float PxAbs(float a)
abs returns the absolute value of its argument.
Definition PxMath.h:109
PX_DEPRECATED void PxVehicleShiftOrigin(const PxVec3 &shift, const PxU32 nbVehicles, PxVehicleWheels **vehicles)
Shift the origin of vehicles by the specified vector.
Definition PxVehicleUpdate.cpp:7105