1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
|
//
// 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) 2008-2018 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 "PulleyJoint.h"
#include <assert.h>
#include "PxConstraint.h"
using namespace physx;
PxConstraintShaderTable PulleyJoint::sShaderTable = { &PulleyJoint::solverPrep, &PulleyJoint::project, &PulleyJoint::visualize, PxConstraintFlag::Enum(0) };
PulleyJoint::PulleyJoint(PxPhysics& physics, PxRigidBody& body0, const PxTransform& localFrame0, const PxVec3& attachment0,
PxRigidBody& body1, const PxTransform& localFrame1, const PxVec3& attachment1)
{
mConstraint = physics.createConstraint(&body0, &body1, *this, PulleyJoint::sShaderTable, sizeof(PulleyJointData));
mBody[0] = &body0;
mBody[1] = &body1;
// keep these around in case the CoM gets relocated
mLocalPose[0] = localFrame0.getNormalized();
mLocalPose[1] = localFrame1.getNormalized();
// the data which will be fed to the joint solver and projection shaders
mData.attachment0 = attachment0;
mData.attachment1 = attachment1;
mData.distance = 1.0f;
mData.ratio = 1.0f;
mData.c2b[0] = body0.getCMassLocalPose().transformInv(mLocalPose[0]);
mData.c2b[1] = body1.getCMassLocalPose().transformInv(mLocalPose[1]);
}
void PulleyJoint::release()
{
mConstraint->release();
}
///////////////////////////////////////////// attribute accessors and mutators
void PulleyJoint::setAttachment0(const PxVec3& pos)
{
mData.attachment0 = pos;
mConstraint->markDirty();
}
PxVec3 PulleyJoint::getAttachment0() const
{
return mData.attachment0;
}
void PulleyJoint::setAttachment1(const PxVec3& pos)
{
mData.attachment1 = pos;
mConstraint->markDirty();
}
PxVec3 PulleyJoint::getAttachment1() const
{
return mData.attachment1;
}
void PulleyJoint::setDistance(float totalDistance)
{
mData.distance = totalDistance;
mConstraint->markDirty();
}
float PulleyJoint::getDistance() const
{
return mData.distance;
}
void PulleyJoint::setRatio(float ratio)
{
mData.ratio = ratio;
mConstraint->markDirty();
}
float PulleyJoint::getRatio() const
{
return mData.ratio;
}
///////////////////////////////////////////// PxConstraintConnector methods
void* PulleyJoint::prepareData()
{
return &mData;
}
void PulleyJoint::onConstraintRelease()
{
delete this;
}
void PulleyJoint::onComShift(PxU32 actor)
{
mData.c2b[actor] = mBody[actor]->getCMassLocalPose().transformInv(mLocalPose[actor]);
mConstraint->markDirty();
}
void PulleyJoint::onOriginShift(const PxVec3& shift)
{
mData.attachment0 -= shift;
mData.attachment1 -= shift;
}
void* PulleyJoint::getExternalReference(PxU32& typeID)
{
typeID = TYPE_ID;
return this;
}
///////////////////////////////////////////// work functions
PxU32 PulleyJoint::solverPrep(Px1DConstraint* constraints,
PxVec3& body0WorldOffset,
PxU32 maxConstraints,
PxConstraintInvMassScale&,
const void* constantBlock,
const PxTransform& bA2w,
const PxTransform& bB2w)
{
PX_UNUSED(maxConstraints);
const PulleyJointData& data = *reinterpret_cast<const PulleyJointData*>(constantBlock);
PxTransform cA2w = bA2w.transform(data.c2b[0]);
PxTransform cB2w = bB2w.transform(data.c2b[1]);
body0WorldOffset = cB2w.p - bA2w.p;
PxVec3 directionA = data.attachment0 - cA2w.p;
PxReal distanceA = directionA.normalize();
PxVec3 directionB = data.attachment1 - cB2w.p;
PxReal distanceB = directionB.normalize();
directionB *= data.ratio;
PxReal totalDistance = distanceA + distanceB;
// compute geometric error:
PxReal geometricError = (data.distance - totalDistance);
Px1DConstraint *c = constraints;
// constraint is breakable, so we need to output forces
c->flags = Px1DConstraintFlag::eOUTPUT_FORCE;
if (geometricError < 0.0f)
{
c->maxImpulse = PX_MAX_F32;
c->minImpulse = 0;
c->geometricError = geometricError;
}
else if(geometricError > 0.0f)
{
c->maxImpulse = 0;
c->minImpulse = -PX_MAX_F32;
c->geometricError = geometricError;
}
c->linear0 = directionA; c->angular0 = (cA2w.p - bA2w.p).cross(c->linear0);
c->linear1 = -directionB; c->angular1 = (cB2w.p - bB2w.p).cross(c->linear1);
return 1;
}
void PulleyJoint::project(const void* constantBlock,
PxTransform& bodyAToWorld,
PxTransform& bodyBToWorld,
bool projectToA)
{
PX_UNUSED(constantBlock);
PX_UNUSED(bodyAToWorld);
PX_UNUSED(bodyBToWorld);
PX_UNUSED(projectToA);
}
void PulleyJoint::visualize(PxConstraintVisualizer& viz,
const void* constantBlock,
const PxTransform& body0Transform,
const PxTransform& body1Transform,
PxU32 flags)
{
PX_UNUSED(flags);
const PulleyJointData& data = *reinterpret_cast<const PulleyJointData*>(constantBlock);
PxTransform cA2w = body0Transform * data.c2b[0];
PxTransform cB2w = body1Transform * data.c2b[1];
viz.visualizeJointFrames(cA2w, cB2w);
viz.visualizeJointFrames(PxTransform(data.attachment0), PxTransform(data.attachment1));
}
|