1 | // |
2 | // Redistribution and use in source and binary forms, with or without |
3 | // modification, are permitted provided that the following conditions |
4 | // are met: |
5 | // * Redistributions of source code must retain the above copyright |
6 | // notice, this list of conditions and the following disclaimer. |
7 | // * Redistributions in binary form must reproduce the above copyright |
8 | // notice, this list of conditions and the following disclaimer in the |
9 | // documentation and/or other materials provided with the distribution. |
10 | // * Neither the name of NVIDIA CORPORATION nor the names of its |
11 | // contributors may be used to endorse or promote products derived |
12 | // from this software without specific prior written permission. |
13 | // |
14 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY |
15 | // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
16 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
17 | // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
18 | // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
19 | // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
20 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
21 | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
22 | // OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
23 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
24 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
25 | // |
26 | // Copyright (c) 2008-2021 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 | #include "foundation/PxMemory.h" |
31 | #include "vehicle/PxVehicleNoDrive.h" |
32 | #include "vehicle/PxVehicleWheels.h" |
33 | #include "PxVehicleDefaults.h" |
34 | #include "PxRigidDynamic.h" |
35 | #include "CmPhysXCommon.h" |
36 | #include "CmUtils.h" |
37 | #include "PsFoundation.h" |
38 | |
39 | namespace physx |
40 | { |
41 | |
42 | extern PxF32 gToleranceScaleLength; |
43 | |
44 | bool PxVehicleNoDrive::isValid() const |
45 | { |
46 | PX_CHECK_AND_RETURN_VAL(PxVehicleWheels::isValid(), "invalid PxVehicleDrive" , false); |
47 | return true; |
48 | } |
49 | |
50 | PxVehicleNoDrive* PxVehicleNoDrive::allocate(const PxU32 numWheels) |
51 | { |
52 | PX_CHECK_AND_RETURN_NULL(numWheels>0, "Cars with zero wheels are illegal" ); |
53 | PX_CHECK_AND_RETURN_NULL(gToleranceScaleLength > 0, "PxVehicleNoDrive::allocate - need to call PxInitVehicleSDK" ); |
54 | |
55 | //Compute the bytes needed. |
56 | const PxU32 numWheels4 = (((numWheels + 3) & ~3) >> 2); |
57 | const PxU32 inputByteSize16 = sizeof(PxReal)*numWheels4*4; |
58 | const PxU32 byteSize = sizeof(PxVehicleNoDrive) + 3*inputByteSize16 + PxVehicleWheels::computeByteSize(nbWheels: numWheels); |
59 | |
60 | //Allocate the memory. |
61 | PxVehicleNoDrive* veh = static_cast<PxVehicleNoDrive*>(PX_ALLOC(byteSize, "PxVehicleNoDrive" )); |
62 | Cm::markSerializedMem(veh, byteSize); |
63 | new(veh) PxVehicleNoDrive(); |
64 | |
65 | //Patch up the pointers. |
66 | PxU8* ptr = reinterpret_cast<PxU8*>(veh) + sizeof(PxVehicleNoDrive); |
67 | veh->mSteerAngles = reinterpret_cast<PxReal*>(ptr); |
68 | ptr += inputByteSize16; |
69 | veh->mDriveTorques = reinterpret_cast<PxReal*>(ptr); |
70 | ptr += inputByteSize16; |
71 | veh->mBrakeTorques = reinterpret_cast<PxReal*>(ptr); |
72 | ptr += inputByteSize16; |
73 | ptr = PxVehicleWheels::patchupPointers(nbWheels: numWheels, vehWheels: veh, ptr); |
74 | |
75 | //Initialise. |
76 | PxMemZero(dest: veh->mSteerAngles, count: inputByteSize16); |
77 | PxMemZero(dest: veh->mDriveTorques, count: inputByteSize16); |
78 | PxMemZero(dest: veh->mBrakeTorques, count: inputByteSize16); |
79 | veh->init(numWheels); |
80 | |
81 | //Set the vehicle type. |
82 | veh->mType = PxVehicleTypes::eNODRIVE; |
83 | |
84 | return veh; |
85 | } |
86 | |
87 | void PxVehicleNoDrive::free() |
88 | { |
89 | PxVehicleWheels::free(); |
90 | } |
91 | |
92 | void PxVehicleNoDrive::setup |
93 | (PxPhysics* physics, PxRigidDynamic* vehActor, const PxVehicleWheelsSimData& wheelsData) |
94 | { |
95 | //Set up the wheels. |
96 | PxVehicleWheels::setup(physics,vehActor,wheelsData,nbDrivenWheels: 0,nbNonDrivenWheels: wheelsData.getNbWheels()); |
97 | } |
98 | |
99 | PxVehicleNoDrive* PxVehicleNoDrive::create |
100 | (PxPhysics* physics, PxRigidDynamic* vehActor, |
101 | const PxVehicleWheelsSimData& wheelsData) |
102 | { |
103 | PxVehicleNoDrive* veh=PxVehicleNoDrive::allocate(numWheels: wheelsData.getNbWheels()); |
104 | veh->setup(physics,vehActor,wheelsData); |
105 | return veh; |
106 | } |
107 | |
108 | void PxVehicleNoDrive::setToRestState() |
109 | { |
110 | const PxU32 numWheels4 = (((mWheelsSimData.getNbWheels() + 3) & ~3) >> 2); |
111 | const PxU32 inputByteSize = sizeof(PxReal)*numWheels4*4; |
112 | const PxU32 inputByteSize16 = (inputByteSize + 15) & ~15; |
113 | PxMemZero(dest: mSteerAngles, count: 3*inputByteSize16); |
114 | |
115 | //Set core to rest state. |
116 | PxVehicleWheels::setToRestState(); |
117 | } |
118 | |
119 | void PxVehicleNoDrive::setBrakeTorque(const PxU32 id, const PxReal brakeTorque) |
120 | { |
121 | PX_CHECK_AND_RETURN(id < mWheelsSimData.getNbWheels(), "PxVehicleNoDrive::setBrakeTorque - Illegal wheel" ); |
122 | PX_CHECK_AND_RETURN(brakeTorque>=0, "PxVehicleNoDrive::setBrakeTorque - negative brake torques are illegal" ); |
123 | mBrakeTorques[id] = brakeTorque; |
124 | } |
125 | |
126 | void PxVehicleNoDrive::setDriveTorque(const PxU32 id, const PxReal driveTorque) |
127 | { |
128 | PX_CHECK_AND_RETURN(id < mWheelsSimData.getNbWheels(), "PxVehicleNoDrive::setDriveTorque - Illegal wheel" ); |
129 | mDriveTorques[id] = driveTorque; |
130 | } |
131 | |
132 | void PxVehicleNoDrive::setSteerAngle(const PxU32 id, const PxReal steerAngle) |
133 | { |
134 | PX_CHECK_AND_RETURN(id < mWheelsSimData.getNbWheels(), "PxVehicleNoDrive::setSteerAngle - Illegal wheel" ); |
135 | mSteerAngles[id] = steerAngle; |
136 | } |
137 | |
138 | PxReal PxVehicleNoDrive::getBrakeTorque(const PxU32 id) const |
139 | { |
140 | PX_CHECK_AND_RETURN_VAL(id < mWheelsSimData.getNbWheels(), "PxVehicleNoDrive::getBrakeTorque - Illegal wheel" , 0); |
141 | return mBrakeTorques[id]; |
142 | } |
143 | |
144 | PxReal PxVehicleNoDrive::getDriveTorque(const PxU32 id) const |
145 | { |
146 | PX_CHECK_AND_RETURN_VAL(id < mWheelsSimData.getNbWheels(), "PxVehicleNoDrive::getDriveTorque - Illegal wheel" ,0); |
147 | return mDriveTorques[id]; |
148 | } |
149 | |
150 | PxReal PxVehicleNoDrive::getSteerAngle(const PxU32 id) const |
151 | { |
152 | PX_CHECK_AND_RETURN_VAL(id < mWheelsSimData.getNbWheels(), "PxVehicleNoDrive::getSteerAngle - Illegal wheel" ,0); |
153 | return mSteerAngles[id]; |
154 | } |
155 | |
156 | } //namespace physx |
157 | |
158 | |