AGX Dynamics 2.38.0.2
Loading...
Searching...
No Matches
BasicIntersectionCode.h
Go to the documentation of this file.
1/*
2Copyright 2007-2024. Algoryx Simulation AB.
3
4All AGX source code, intellectual property, documentation, sample code,
5tutorials, scene files and technical white papers, are copyrighted, proprietary
6and confidential material of Algoryx Simulation AB. You may not download, read,
7store, distribute, publish, copy or otherwise disseminate, use or expose this
8material unless having a written signed agreement with Algoryx Simulation AB, or having been
9advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10valid commercial license from Algoryx Simulation AB.
11
12Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13from using this software, unless otherwise stated in written agreements with
14Algoryx Simulation AB.
15*/
16
17#pragma once
18
20#include <agx/Vec3.h>
21#include <agx/AffineMatrix4x4.h>
24#include <agx/Plane.h>
25
27
28
29namespace agxCollide {
30 class ClosestPointsSolution;
31
35 namespace ShapeColliders {
36
45 void AGXPHYSICS_EXPORT collideSphereSphere(
46 const agx::Vec3& position1,
47 const agx::Real radius1,
48 const agx::Vec3& position2,
49 const agx::Real radius2,
50 agxCollide::ShapeCollider::LocalContactPointVector& result);
51
52
64 void AGXPHYSICS_EXPORT collideSphereCylinder(
65 const agx::Vec3& spherePosition,
66 const agx::Real sphereRadius,
67 const agx::AffineMatrix4x4& cylinderTransform,
68 const agx::Real cylinderRadius,
69 const agx::Real cylinderHeight,
70 const agx::Real normalFlipSign,
71 agxCollide::ShapeCollider::LocalContactPointVector& result );
72
73
84 void AGXPHYSICS_EXPORT collideCapsulePlane(
85 const agx::AffineMatrix4x4& capsuleTransform,
86 const agx::Real capsuleRadius,
87 const agx::Real capsuleHeight,
88 const agx::Plane& transformedPlane,
89 const agx::Real normalFlipSign,
90 agxCollide::ShapeCollider::LocalContactPointVector& result );
91
92
100 void AGXPHYSICS_EXPORT collideSpherePlane(
101 const agx::Vec3& spherePosition,
102 const agx::Real sphereRadius,
103 const agx::Plane& transformedPlane,
104 agxCollide::ShapeCollider::LocalContactPointVector& result);
105
106
114 void AGXPHYSICS_EXPORT collideBoxPlane(
115 const agx::AffineMatrix4x4& boxTransform,
116 const agx::Vec3& boxHalfExtents,
117 const agx::Plane& transformedPlane,
118 agxCollide::ShapeCollider::LocalContactPointVector& result );
119
120
131 void AGXPHYSICS_EXPORT collideCylinderPlane(
132 const agx::AffineMatrix4x4& cylinderTransform,
133 const agx::Real cylinderRadius,
134 const agx::Real cylinderHeight,
135 const agx::Plane& transformedPlane,
136 agxCollide::ShapeCollider::LocalContactPointVector& result );
137
138
158 void AGXPHYSICS_EXPORT addSupportPointsForCylindersAlongMantle(
159 const ClosestPointsSolution& closest,
160 const agx::Real cyl1Radius,
161 const agx::Real cyl1Height,
162 const agx::Vec3& cyl1World1,
163 const agx::Vec3& cyl1World2,
164 const agx::AffineMatrix4x4& transform1,
165 const agx::Real cyl2Radius,
166 const agx::Real cyl2Height,
167 const agx::Vec3& cyl2World1,
168 const agx::Vec3& cyl2World2,
169 const agx::AffineMatrix4x4& transform2,
170 const agx::Vec3& normal,
171 agxCollide::ShapeCollider::LocalContactPointVector& result,
172 bool createOnlyForCyl1InCyl2 = false);
173
191 AGXPHYSICS_EXPORT void collideCylinderCylinder(
192 agx::Real radius1,
193 agx::Real radius2,
194 agx::Real height1,
195 agx::Real height2,
196 agx::Physics::Geometry::ShapeInstance shape1,
197 agx::Physics::Geometry::ShapeInstance shape2,
198 const agx::AffineMatrix4x4& transform1,
199 const agx::AffineMatrix4x4& transform2,
200 const agx::Vec3& linearVelocity1,
201 const agx::Vec3& linearVelocity2,
202 const agx::Vec3& angularVelocity1,
203 const agx::Vec3& angularVelocity2,
204 bool earlyOut,
205 ShapeCollider::LocalContactPointVector& result);
206
207 }
208
209
210
211 // Implementations
212
213 AGX_FORCE_INLINE void ShapeColliders::collideCapsulePlane(
214 const agx::AffineMatrix4x4& capsuleTransform,
215 const agx::Real capsuleRadius,
216 const agx::Real capsuleHeight,
217 const agx::Plane& transformedPlane,
218 const agx::Real normalFlipSign,
219 agxCollide::ShapeCollider::LocalContactPointVector& result )
220
221 {
222 // World coordinates for capsule points.
223 const agx::Vec3 cap1 = agx::Vec3(0, capsuleHeight * agx::Real(0.5), 0);
224 const agx::Vec3 cap2 = agx::Vec3(0, -capsuleHeight * agx::Real(0.5), 0);
225
226 const agx::Vec3 p1 = cap1 * capsuleTransform;
227 const agx::Vec3 p2 = cap2 * capsuleTransform;
228
229 const agx::Vec3 normal = transformedPlane.getNormal();
230
231 // Get the distance from the point to the plane.
232 // If negative or distance < the radius of the sphere, a collision has occurred.
233 const agx::Real d1 = transformedPlane.signedDistanceToPoint(p1);
234 if (d1 <= capsuleRadius) {
235 // The contact point is the capsule endpoint projected onto the plane.
236 const agx::Real depth = capsuleRadius - d1;
237 const agx::Vec3 point = p1 - normal * (d1 + depth * agx::Real(0.5));
238 ShapeCollider::addContactPoint( result, point, normal * normalFlipSign, depth );
239 }
240
241 const agx::Real d2 = transformedPlane.signedDistanceToPoint(p2);
242 if (d2 <= capsuleRadius) {
243 // The contact point is the capsule endpoint projected onto the plane.
244 const agx::Real depth = capsuleRadius - d2;
245 const agx::Vec3 point = p2 - normal * (d2 + depth * agx::Real(0.5));
246 ShapeCollider::addContactPoint( result, point, normal * normalFlipSign, depth );
247 }
248 }
249
250
251
252 AGX_FORCE_INLINE void ShapeColliders::collideSphereSphere(
253 const agx::Vec3& position1,
254 const agx::Real radius1,
255 const agx::Vec3& position2,
256 const agx::Real radius2,
257 agxCollide::ShapeCollider::LocalContactPointVector& result)
258 {
259 const agx::Real radiusSum = radius1 + radius2;
260 agx::Vec3 normal = position1 - position2;
261
262 const agx::Real len2 = normal.length2();
263 if (radiusSum * radiusSum < len2) {
264 // Mathematically no contact.
265 // Make sure we don't lose contacts due to floating point precision.
266 agx::Real epsilon = agx::RealEpsilon * (agx::Real(1.0) + radius1 + radius2);
267 if ((radiusSum + epsilon) * (radiusSum + epsilon) < len2)
268 return; // No contact.
269 else {
270 // Contact which might have been missed due to floating point precision.
271 // We keep it (conservative on collision), but give it depth 0.
272 normal.normalize();
273 ShapeCollider::addContactPoint( result,
274 position2 + normal * radius2, normal, agx::Real(0) );
275 }
276 }
277 else {
278 agx::Real distance;
279 // Calculate the normalized contact normal pointing
280 // from sphere2 towards sphere1.
281 if ( len2 == 0 ) {
282 // The sphere centers are at the exact same position.
283 distance = agx::Real( 0 );
284 normal[2] = 1;
285 }
286 else
287 distance = normal.normalize();
288
289 const agx::Real depth = radiusSum - distance;
290 if (depth >= agx::Real(0)) {
291 ShapeCollider::addContactPoint( result,
292 position2 + normal * (radius2 - depth * agx::Real(0.5)),
293 normal, depth );
294 }
295 }
296 }
297
298
299 AGX_FORCE_INLINE void ShapeColliders::collideSpherePlane(
300 const agx::Vec3& spherePosition,
301 const agx::Real sphereRadius,
302 const agx::Plane& transformedPlane,
303 agxCollide::ShapeCollider::LocalContactPointVector& result)
304 {
305 const agx::Real distance = transformedPlane.signedDistanceToPoint(spherePosition);
306 if (distance > sphereRadius)
307 return;
308 const agx::Vec3 normal = transformedPlane.getNormal();
309 const agx::Real depth = sphereRadius - distance;
310 ShapeCollider::addContactPoint( result, spherePosition +
311 normal * (depth * agx::Real(0.5) - sphereRadius), normal, depth );
312 }
313}
314
316
317
#define AGXPHYSICS_EXPORT
Class representing the mathematical concept of a plane, also called a half- space.
Definition agx/Plane.h:32
agx::Vec3T< T > getNormal() const
Definition agx/Plane.h:191
T signedDistanceToPoint(const agx::Vec3T< T > &point) const
Calculates the distance from the plane to a point.
Definition agx/Plane.h:209
Real normalize()
Normalize the vector so that it has length unity.
Real length2() const
Length squared of the vector = vec .
#define DOXYGEN_END_INTERNAL_BLOCK()
Definition macros.h:97
#define DOXYGEN_START_INTERNAL_BLOCK()
Definition macros.h:96
#define AGX_FORCE_INLINE
Definition macros.h:58
This namespace consists of a set of classes for handling geometric intersection tests including boole...
Vec3T< Real > Vec3
The object holding 3 dimensional vectors and providing basic arithmetic.
Definition agx/Vec3.h:36
AGXCORE_EXPORT const Real RealEpsilon
double Real
Definition Real.h:42