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
|
//
// 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 "ExtFixedJoint.h"
#include "ExtConstraintHelper.h"
#include "CmVisualization.h"
#include "common/PxSerialFramework.h"
using namespace physx;
using namespace Ext;
namespace physx
{
PxFixedJoint* PxFixedJointCreate(PxPhysics& physics,
PxRigidActor* actor0, const PxTransform& localFrame0,
PxRigidActor* actor1, const PxTransform& localFrame1);
}
PxFixedJoint* physx::PxFixedJointCreate(PxPhysics& physics,
PxRigidActor* actor0, const PxTransform& localFrame0,
PxRigidActor* actor1, const PxTransform& localFrame1)
{
PX_CHECK_AND_RETURN_NULL(localFrame0.isSane(), "PxFixedJointCreate: local frame 0 is not a valid transform");
PX_CHECK_AND_RETURN_NULL(localFrame1.isSane(), "PxFixedJointCreate: local frame 1 is not a valid transform");
PX_CHECK_AND_RETURN_NULL((actor0 && actor0->is<PxRigidBody>()) || (actor1 && actor1->is<PxRigidBody>()), "PxFixedJointCreate: at least one actor must be dynamic");
PX_CHECK_AND_RETURN_NULL(actor0 != actor1, "PxFixedJointCreate: actors must be different");
FixedJoint* j;
PX_NEW_SERIALIZED(j,FixedJoint)(physics.getTolerancesScale(), actor0, localFrame0, actor1, localFrame1);
if(j->attach(physics, actor0, actor1))
return j;
PX_DELETE(j);
return NULL;
}
PxReal FixedJoint::getProjectionLinearTolerance() const
{
return data().projectionLinearTolerance;
}
void FixedJoint::setProjectionLinearTolerance(PxReal tolerance)
{
PX_CHECK_AND_RETURN(PxIsFinite(tolerance) && tolerance >=0, "PxFixedJoint::setProjectionLinearTolerance: invalid parameter");
data().projectionLinearTolerance = tolerance;
markDirty();
}
PxReal FixedJoint::getProjectionAngularTolerance() const
{
return data().projectionAngularTolerance;
}
void FixedJoint::setProjectionAngularTolerance(PxReal tolerance)
{
PX_CHECK_AND_RETURN(PxIsFinite(tolerance) && tolerance >=0 && tolerance <= PxPi, "PxFixedJoint::setProjectionAngularTolerance: invalid parameter");
data().projectionAngularTolerance = tolerance; markDirty();
}
namespace
{
void FixedJointVisualize(PxConstraintVisualizer& viz,
const void* constantBlock,
const PxTransform& body0Transform,
const PxTransform& body1Transform,
PxU32 flags)
{
if(flags & PxConstraintVisualizationFlag::eLOCAL_FRAMES)
{
const FixedJointData& data = *reinterpret_cast<const FixedJointData*>(constantBlock);
const PxTransform& t0 = body0Transform * data.c2b[0];
const PxTransform& t1 = body1Transform * data.c2b[1];
viz.visualizeJointFrames(t0, t1);
}
}
void FixedJointProject(const void* constantBlock,
PxTransform& bodyAToWorld,
PxTransform& bodyBToWorld,
bool projectToA)
{
using namespace joint;
const FixedJointData &data = *reinterpret_cast<const FixedJointData*>(constantBlock);
PxTransform cA2w, cB2w, cB2cA, projected;
computeDerived(data, bodyAToWorld, bodyBToWorld, cA2w, cB2w, cB2cA);
bool linearTrunc, angularTrunc;
projected.p = truncateLinear(cB2cA.p, data.projectionLinearTolerance, linearTrunc);
projected.q = truncateAngular(cB2cA.q, PxSin(data.projectionAngularTolerance/2), PxCos(data.projectionAngularTolerance/2), angularTrunc);
if(linearTrunc || angularTrunc)
projectTransforms(bodyAToWorld, bodyBToWorld, cA2w, cB2w, projected, data, projectToA);
}
}
bool Ext::FixedJoint::attach(PxPhysics &physics, PxRigidActor* actor0, PxRigidActor* actor1)
{
mPxConstraint = physics.createConstraint(actor0, actor1, *this, sShaders, sizeof(FixedJointData));
return mPxConstraint!=NULL;
}
void FixedJoint::exportExtraData(PxSerializationContext& stream) const
{
if(mData)
{
stream.alignData(PX_SERIAL_ALIGN);
stream.writeData(mData, sizeof(FixedJointData));
}
stream.writeName(mName);
}
void FixedJoint::importExtraData(PxDeserializationContext& context)
{
if(mData)
mData = context.readExtraData<FixedJointData, PX_SERIAL_ALIGN>();
context.readName(mName);
}
void FixedJoint::resolveReferences(PxDeserializationContext& context)
{
setPxConstraint(resolveConstraintPtr(context, getPxConstraint(), getConnector(), sShaders));
}
FixedJoint* FixedJoint::createObject(PxU8*& address, PxDeserializationContext& context)
{
FixedJoint* obj = new (address) FixedJoint(PxBaseFlag::eIS_RELEASABLE);
address += sizeof(FixedJoint);
obj->importExtraData(context);
obj->resolveReferences(context);
return obj;
}
// global function to share the joint shaders with API capture
const PxConstraintShaderTable* Ext::GetFixedJointShaderTable()
{
return &FixedJoint::getConstraintShaderTable();
}
//~PX_SERIALIZATION
PxConstraintShaderTable Ext::FixedJoint::sShaders = { Ext::FixedJointSolverPrep, FixedJointProject, FixedJointVisualize, PxConstraintFlag::Enum(0) };
|