aboutsummaryrefslogtreecommitdiff
path: root/PhysX_3.4/Source/LowLevelParticles/src/PtCollisionCapsule.cpp
blob: 991545bc3bcc28305531b143fd366d7229892166 (plain) (blame)
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
//
// 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 "PtCollisionMethods.h"
#if PX_USE_PARTICLE_SYSTEM_API

using namespace physx;
using namespace Pt;

namespace
{

void collideWithCapsuleNonContinuous(ParticleCollData& collData, const PxVec3& q, const PxReal& h, const PxReal& r,
                                     const PxReal& proxRadius)
{
	if(collData.localFlags & ParticleCollisionFlags::CC)
		return; // Only apply discrete and proximity collisions if no continuous collisions was detected so far (for any
	// colliding shape)

	PxVec3 segPoint;
	segPoint = PxVec3(q.x, 0.0f, 0.0f);
	segPoint.x = PxMax(segPoint.x, -h);
	segPoint.x = PxMin(segPoint.x, h);
	collData.localSurfaceNormal = q - segPoint;
	PxReal dist = collData.localSurfaceNormal.magnitude();
	if(dist < (r + proxRadius))
	{
		if(dist != 0.0f)
			collData.localSurfaceNormal *= (1.0f / dist);
		else
			collData.localSurfaceNormal = PxVec3(0);

		// Push particle to surface such that the distance to the surface is equal to the collision radius
		collData.localSurfacePos = segPoint + (collData.localSurfaceNormal * (r + collData.restOffset));
		collData.localFlags |= ParticleCollisionFlags::L_PROX;

		if(dist < (r + collData.restOffset))
			collData.localFlags |= ParticleCollisionFlags::L_DC;
	}
}

void collideWithCapsuleTestSphere(ParticleCollData& collData, const PxVec3& p, const PxVec3& q, const PxVec3& d,
                                  const PxReal& h, const PxReal& r, const PxReal& sphereH, const PxReal& discS,
                                  const PxReal& aS, const PxReal& bS, const PxReal& proxRadius)
{
	if(discS <= 0.0f || aS == 0.0f)
	{
		collideWithCapsuleNonContinuous(collData, q, h, r, proxRadius);
	}
	else
	{
		PxReal t = -(bS + PxSqrt(discS)) / aS;
		if(t < 0.0f || t > 1.0f)
		{
			// intersection lies outside p-q interval
			collideWithCapsuleNonContinuous(collData, q, h, r, proxRadius);
		}
		else if(t < collData.ccTime)
		{
			// intersection point lies on sphere, add lcc
			// collData.localSurfacePos = p + (d * t);
			// collData.localSurfaceNormal = collData.localSurfacePos;
			// collData.localSurfaceNormal.x -= sphereH;
			// collData.localSurfaceNormal *= (1.0f / r);
			// collData.localSurfacePos += (collData.localSurfaceNormal * collData.restOffset);
			PxVec3 relativePOSITION = (d * t);
			collData.localSurfaceNormal = p + relativePOSITION;
			collData.localSurfaceNormal.x -= sphereH;
			collData.localSurfaceNormal *= (1.0f / r);
			computeContinuousTargetPosition(collData.localSurfacePos, p, relativePOSITION, collData.localSurfaceNormal,
			                                collData.restOffset);
			collData.ccTime = t;
			collData.localFlags |= ParticleCollisionFlags::L_CC;
		}
	}
}

// ----------------------------------------------------------------
//
//		Note: this code is based on the hardware implementation
//
//      Terminology:
//      Starting point: p
//      End point:      q
//      Ray direction:  d
//
//      Infinite cylinder I:  all (y,z)   : y^2 + z^2 < r^2
//      "Fat plane"       F:  all (x)     : -h < x < h
//      Top sphere        S0: all (x,y,z) : y^2 + z^2 + (x-h)^2 < r^2
//      Bottom sphere     S1: all (x,y,z) : y^2 + z^2 + (x+h)^2 < r^2
//
//      Cylinder          Z = (I & F)
//      Capsule           C = Z | S0 | S1
//
//      coefficients a, b, c for the squared distance functions sqd(t) = a * t^2 + b * t + c, for I, S0 and S1:
//
//      aI =  d.y*d.y + d.z*d.z
//      aS0 = d.y*d.y + d.z*d.z + d.x*d.x
//      aS1 = d.y*d.y + d.z*d.z + d.x*d.x
//
//      bI =  d.y*p.y + d.z*p.z
//      bS0 = d.y*p.y + d.z*p.z + d.x*p.x - h*d.x
//      bS1 = d.y*p.y + d.z*p.z + d.x*p.x + h*d.x
//
//      cI =  p.y*p.y + p.z*p.z - r*r.
//      cS0 = p.y*p.y + p.z*p.z - r*r + p.x*p.x + h*h - 2*h*p.x
//      cS1 = p.y*p.y + p.z*p.z - r*r + p.x*p.x + h*h + 2*h*p.x
//
//      these will be treated in vectorized fashion:
//      I  <--> .y
//      S0 <--> .x
//      S1 <--> .z
//
//      for p, we have sqd(0) = c
//      ( for q, we have sqd(1) = a + b + c )
//
// ----------------------------------------------------------------
PX_FORCE_INLINE void collideWithCapsule(ParticleCollData& collData, const PxCapsuleGeometry& capsuleShapeData,
                                        PxReal proxRadius)
{
	// Note: The local coordinate system of a capsule is defined such that the cylindrical part is
	//       wrapped around the x-axis

	PxVec3& p = collData.localOldPos;
	PxVec3& q = collData.localNewPos;

	PxReal r = capsuleShapeData.radius;
	PxReal h = capsuleShapeData.halfHeight;

	PxVec3 a, b, c;

	// all c values
	PxReal tmp;
	c.y = p.y * p.y + p.z * p.z - r * r;
	tmp = c.y + p.x * p.x + h * h;
	c.x = tmp - 2 * h * p.x;
	c.z = tmp + 2 * h * p.x;

	bool pInI = c.y < 0.0f;  // Old particle position inside the infinite zylinder
	bool pInS0 = c.x < 0.0f; // Old particle position inside the right sphere
	bool pInS1 = c.z < 0.0f; // Old particle position inside the left sphere
	bool pRightOfH = p.x > h;
	bool pLeftOfMinusH = p.x < -h;
	bool pInZ = (!pRightOfH && !pLeftOfMinusH && pInI);

	if(pInZ || pInS0 || pInS1)
	{
		// p is inside the skeleton
		// add ccd with time 0.0

		PxVec3 segPoint;
		segPoint = PxVec3(p.x, 0.0f, 0.0f);
		segPoint.x = PxMax(segPoint.x, -h);
		segPoint.x = PxMin(segPoint.x, h);
		PxVec3 normal = p - segPoint;
		collData.localSurfaceNormal = normal.isZero() ? PxVec3(0.0f, 1.0f, 0.0f) : normal.getNormalized();
		// Push particle to surface such that the distance to the surface is equal to the collision radius
		collData.localSurfacePos = segPoint + (collData.localSurfaceNormal * (r + collData.restOffset));
		collData.ccTime = 0.0;
		collData.localFlags |= ParticleCollisionFlags::L_CC;
	}
	else
	{
		// p is outside of the skeleton

		PxVec3 d = q - p;

		// all b values
		b.y = d.y * p.y + d.z * p.z;
		tmp = b.y + d.x * p.x;
		b.x = tmp - h * d.x;
		b.z = tmp + h * d.x;

		// all a values
		a.y = d.y * d.y + d.z * d.z;
		a.x = a.y + d.x * d.x;
		a.z = a.x;

		// all discriminants
		PxVec3 tmpVec0, tmpVec1;
		tmpVec0 = b.multiply(b);
		tmpVec1 = c.multiply(a);
		PxVec3 discs = tmpVec0 - tmpVec1;

		// this made cases fail with d.y == 0.0 and d.z == 0.0
		// bool dInI  = discs.y > 0.0f;
		bool dInI = discs.y >= 0.0f;

		// bool dInS0 = discs.x > 0.0f;
		// bool dInS1 = discs.z > 0.0f;

		if(!dInI)
		{
			// the ray does not intersect the infinite cylinder
			collideWithCapsuleNonContinuous(collData, q, h, r, proxRadius);
		}
		else
		{
			// d intersects the infinite cylinder
			if(pInI)
			{
				// p is contained in the infinite cylinder, either above the top sphere or below the bottom sphere.
				// -> directly test against the nearest sphere
				if(p.x > 0)
				{
					// check sphere 0
					collideWithCapsuleTestSphere(collData, p, q, d, h, r, h, discs.x, a.x, b.x, proxRadius);
				}
				else
				{
					// check sphere 1
					collideWithCapsuleTestSphere(collData, p, q, d, h, r, -h, discs.z, a.z, b.z, proxRadius);
				}
			}
			else if(discs.y <= 0.0f || a.y == 0.0f)
			{
				// d is zero or tangential to cylinder surface
				collideWithCapsuleNonContinuous(collData, q, h, r, proxRadius);
			}
			else
			{
				// p lies outside of infinite cylinder, compute intersection point with it
				PxReal t = -(b.y + PxSqrt(discs.y)) / a.y;
				if(t < 0.0f || t > 1.0f)
				{
					// intersection lies outside p-q interval
					collideWithCapsuleNonContinuous(collData, q, h, r, proxRadius);
				}
				else
				{
					PxVec3 relativePOSITION = (d * t);
					PxVec3 impact = p + relativePOSITION;
					if(impact.x > h)
					{
						// if above the actual cylinder, check sphere 0
						collideWithCapsuleTestSphere(collData, p, q, d, h, r, h, discs.x, a.x, b.x, proxRadius);
					}
					else if(impact.x < -h)
					{
						// if below the actual cylinder, check sphere 1
						collideWithCapsuleTestSphere(collData, p, q, d, h, r, -h, discs.z, a.z, b.z, proxRadius);
					}
					else if(t < collData.ccTime)
					{
						// intersection point lies on cylinder, add cc
						// collData.localSurfaceNormal = collData.localSurfacePos / r;
						// collData.localSurfaceNormal.x = 0.0f;
						// collData.localSurfacePos += (collData.localSurfaceNormal * collData.restOffset);
						collData.localSurfaceNormal = impact / r;
						collData.localSurfaceNormal.x = 0.0f;
						computeContinuousTargetPosition(collData.localSurfacePos, p, relativePOSITION,
						                                collData.localSurfaceNormal, collData.restOffset);
						collData.ccTime = t;
						collData.localFlags |= ParticleCollisionFlags::L_CC;
					}
				}
			}
		}
	}
}

} // namespace

void physx::Pt::collideWithCapsule(ParticleCollData* collShapeData, PxU32 numCollData,
                                   const Gu::GeometryUnion& capsuleShape, PxReal proxRadius)
{
	PX_ASSERT(collShapeData);
	PX_ASSERT(capsuleShape.getType() == PxGeometryType::eCAPSULE);

	const PxCapsuleGeometry& capsuleShapeData = capsuleShape.get<const PxCapsuleGeometry>();

	for(PxU32 p = 0; p < numCollData; p++)
	{
		::collideWithCapsule(collShapeData[p], capsuleShapeData, proxRadius);
	}
}

#endif // PX_USE_PARTICLE_SYSTEM_API