AGXDynamics  2.31.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
BasicIntersectionCode.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2021. Algoryx Simulation AB.
3 
4 All AGX source code, intellectual property, documentation, sample code,
5 tutorials, scene files and technical white papers, are copyrighted, proprietary
6 and confidential material of Algoryx Simulation AB. You may not download, read,
7 store, distribute, publish, copy or otherwise disseminate, use or expose this
8 material unless having a written signed agreement with Algoryx Simulation AB, or having been
9 advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10 valid commercial license from Algoryx Simulation AB.
11 
12 Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13 from using this software, unless otherwise stated in written agreements with
14 Algoryx Simulation AB.
15 */
16 
17 #ifndef AGXCOLLIDE_BASICINTERSECTIONCODE_H
18 #define AGXCOLLIDE_BASICINTERSECTIONCODE_H
19 
20 #include <agx/agxPhysics_export.h>
21 #include <agx/Vec3.h>
22 #include <agx/AffineMatrix4x4.h>
25 #include <agx/Plane.h>
26 
28 
29 
30 namespace agxCollide {
31  class ClosestPointsSolution;
32 
36  namespace ShapeColliders {
37 
46  void AGXPHYSICS_EXPORT collideSphereSphere(
47  const agx::Vec3& position1,
48  const agx::Real radius1,
49  const agx::Vec3& position2,
50  const agx::Real radius2,
52 
53 
65  void AGXPHYSICS_EXPORT collideSphereCylinder(
66  const agx::Vec3& spherePosition,
67  const agx::Real sphereRadius,
68  const agx::AffineMatrix4x4& cylinderTransform,
69  const agx::Real cylinderRadius,
70  const agx::Real cylinderHeight,
71  const agx::Real normalFlipSign,
73 
74 
85  void AGXPHYSICS_EXPORT collideCapsulePlane(
86  const agx::AffineMatrix4x4& capsuleTransform,
87  const agx::Real capsuleRadius,
88  const agx::Real capsuleHeight,
89  const agx::Plane& transformedPlane,
90  const agx::Real normalFlipSign,
92 
93 
101  void AGXPHYSICS_EXPORT collideSpherePlane(
102  const agx::Vec3& spherePosition,
103  const agx::Real sphereRadius,
104  const agx::Plane& transformedPlane,
106 
107 
115  void AGXPHYSICS_EXPORT collideBoxPlane(
116  const agx::AffineMatrix4x4& boxTransform,
117  const agx::Vec3& boxHalfExtents,
118  const agx::Plane& transformedPlane,
120 
121 
132  void AGXPHYSICS_EXPORT collideCylinderPlane(
133  const agx::AffineMatrix4x4& cylinderTransform,
134  const agx::Real cylinderRadius,
135  const agx::Real cylinderHeight,
136  const agx::Plane& transformedPlane,
138 
139 
159  void AGXPHYSICS_EXPORT addSupportPointsForCylindersAlongMantle(
160  const ClosestPointsSolution& closest,
161  const agx::Real cyl1Radius,
162  const agx::Real cyl1Height,
163  const agx::Vec3& cyl1World1,
164  const agx::Vec3& cyl1World2,
165  const agx::AffineMatrix4x4& transform1,
166  const agx::Real cyl2Radius,
167  const agx::Real cyl2Height,
168  const agx::Vec3& cyl2World1,
169  const agx::Vec3& cyl2World2,
170  const agx::AffineMatrix4x4& transform2,
171  const agx::Vec3& normal,
173  bool createOnlyForCyl1InCyl2 = false);
174  }
175 
176 
177 
178  // Implementations
179 
180  AGX_FORCE_INLINE void ShapeColliders::collideCapsulePlane(
181  const agx::AffineMatrix4x4& capsuleTransform,
182  const agx::Real capsuleRadius,
183  const agx::Real capsuleHeight,
184  const agx::Plane& transformedPlane,
185  const agx::Real normalFlipSign,
187 
188  {
189  // World coordinates for capsule points.
190  const agx::Vec3 cap1 = agx::Vec3(0, capsuleHeight * agx::Real(0.5), 0);
191  const agx::Vec3 cap2 = agx::Vec3(0, -capsuleHeight * agx::Real(0.5), 0);
192 
193  const agx::Vec3 p1 = cap1 * capsuleTransform;
194  const agx::Vec3 p2 = cap2 * capsuleTransform;
195 
196  const agx::Vec3 normal = transformedPlane.getNormal();
197 
198  // Get the distance from the point to the plane.
199  // If negative or distance < the radius of the sphere, a collision has occurred.
200  const agx::Real d1 = transformedPlane.signedDistanceToPoint(p1);
201  if (d1 <= capsuleRadius) {
202  // The contact point is the capsule endpoint projected onto the plane.
203  const agx::Real depth = capsuleRadius - d1;
204  const agx::Vec3 point = p1 - normal * (d1 + depth * agx::Real(0.5));
205  ShapeCollider::addContactPoint( result, point, normal * normalFlipSign, depth );
206  }
207 
208  const agx::Real d2 = transformedPlane.signedDistanceToPoint(p2);
209  if (d2 <= capsuleRadius) {
210  // The contact point is the capsule endpoint projected onto the plane.
211  const agx::Real depth = capsuleRadius - d2;
212  const agx::Vec3 point = p2 - normal * (d2 + depth * agx::Real(0.5));
213  ShapeCollider::addContactPoint( result, point, normal * normalFlipSign, depth );
214  }
215  }
216 
217 
218 
219  AGX_FORCE_INLINE void ShapeColliders::collideSphereSphere(
220  const agx::Vec3& position1,
221  const agx::Real radius1,
222  const agx::Vec3& position2,
223  const agx::Real radius2,
225  {
226  const agx::Real radiusSum = radius1 + radius2;
227  agx::Vec3 normal = position1 - position2;
228 
229  const agx::Real len2 = normal.length2();
230  if (radiusSum * radiusSum < len2) {
231  // Mathematically no contact.
232  // Make sure we don't lose contacts due to floating point precision.
233  agx::Real epsilon = agx::RealEpsilon * (agx::Real(1.0) + radius1 + radius2);
234  if ((radiusSum + epsilon) * (radiusSum + epsilon) < len2)
235  return; // No contact.
236  else {
237  // Contact which might have been missed due to floating point precision.
238  // We keep it (conservative on collision), but give it depth 0.
239  normal.normalize();
240  ShapeCollider::addContactPoint( result,
241  position2 + normal * radius2, normal, agx::Real(0) );
242  }
243  }
244  else {
245  agx::Real distance;
246  // Calculate the normalized contact normal pointing
247  // from sphere2 towards sphere1.
248  if ( len2 == 0 ) {
249  // The sphere centers are at the exact same position.
250  distance = agx::Real( 0 );
251  normal[2] = 1;
252  }
253  else
254  distance = normal.normalize();
255 
256  const agx::Real depth = radiusSum - distance;
257  if (depth >= agx::Real(0)) {
258  ShapeCollider::addContactPoint( result,
259  position2 + normal * (radius2 - depth * agx::Real(0.5)),
260  normal, depth );
261  }
262  }
263  }
264 
265 
266  AGX_FORCE_INLINE void ShapeColliders::collideSpherePlane(
267  const agx::Vec3& spherePosition,
268  const agx::Real sphereRadius,
269  const agx::Plane& transformedPlane,
271  {
272  const agx::Real distance = transformedPlane.signedDistanceToPoint(spherePosition);
273  if (distance > sphereRadius)
274  return;
275  const agx::Vec3 normal = transformedPlane.getNormal();
276  const agx::Real depth = sphereRadius - distance;
277  ShapeCollider::addContactPoint( result, spherePosition +
278  normal * (depth * agx::Real(0.5) - sphereRadius), normal, depth );
279  }
280 
281 }
282 
284 
285 #endif
286 
Vec3T< Real > Vec3
The object holding 3 dimensional vectors and providing basic arithmetic.
Definition: Vec3.h:36
AGXCORE_EXPORT const Real RealEpsilon
#define AGXPHYSICS_EXPORT
#define AGX_FORCE_INLINE
Definition: Object.h:62
Real length2() const
Length squared of the vector = vec .
Definition: Vec3Template.h:695
Class representing the mathematical concept of a plane, also called a half- space.
Definition: Plane.h:31
Real normalize()
Normalize the vector so that it has length unity.
Definition: Vec3Template.h:707
agx::Vec3T< T > getNormal() const
/return The normal of the plane.
Definition: Plane.h:191
T signedDistanceToPoint(const agx::Vec3T< T > &point) const
Calculates the distance from the plane to a point.
Definition: Plane.h:209
double Real
Definition: Real.h:42
#define DOXYGEN_END_INTERNAL_BLOCK()
Definition: Object.h:101
agx::VectorPOD< LocalContactPoint > LocalContactPointVector
#define DOXYGEN_START_INTERNAL_BLOCK()
Definition: Object.h:100