Box2D 3.1.0
A 2D physics engine for games
Loading...
Searching...
No Matches
Distance

Functions for computing the distance between shapes. More...

Data Structures

struct  b2SegmentDistanceResult
 Result of computing the distance between two line segments. More...
 
struct  b2DistanceProxy
 A distance proxy is used by the GJK algorithm. It encapsulates any shape. More...
 
struct  b2DistanceCache
 Used to warm start b2Distance. More...
 
struct  b2DistanceInput
 Input for b2ShapeDistance. More...
 
struct  b2DistanceOutput
 Output for b2ShapeDistance. More...
 
struct  b2SimplexVertex
 Simplex vertex for debugging the GJK algorithm. More...
 
struct  b2Simplex
 Simplex from the GJK algorithm. More...
 
struct  b2ShapeCastPairInput
 Input parameters for b2ShapeCast. More...
 
struct  b2Sweep
 This describes the motion of a body/shape for TOI computation. More...
 
struct  b2TOIInput
 Input parameters for b2TimeOfImpact. More...
 
struct  b2TOIOutput
 Output parameters for b2TimeOfImpact. More...
 

Enumerations

enum  b2TOIState {
  b2_toiStateUnknown , b2_toiStateFailed , b2_toiStateOverlapped , b2_toiStateHit ,
  b2_toiStateSeparated
}
 Describes the TOI output.
 

Functions

b2SegmentDistanceResult b2SegmentDistance (b2Vec2 p1, b2Vec2 q1, b2Vec2 p2, b2Vec2 q2)
 Compute the distance between two line segments, clamping at the end points if needed.
 
b2DistanceOutput b2ShapeDistance (b2DistanceCache *cache, const b2DistanceInput *input, b2Simplex *simplexes, int simplexCapacity)
 Compute the closest points between two shapes represented as point clouds.
 
b2CastOutput b2ShapeCast (const b2ShapeCastPairInput *input)
 Perform a linear shape cast of shape B moving and shape A fixed. Determines the hit point, normal, and translation fraction.
 
b2DistanceProxy b2MakeProxy (const b2Vec2 *vertices, int32_t count, float radius)
 Make a proxy for use in GJK and related functions.
 
b2Transform b2GetSweepTransform (const b2Sweep *sweep, float time)
 Evaluate the transform sweep at a specific time.
 
b2TOIOutput b2TimeOfImpact (const b2TOIInput *input)
 Compute the upper bound on time before two shapes penetrate.
 

Detailed Description

Functions for computing the distance between shapes.

These are advanced functions you can use to perform distance calculations. There are functions for computing the closest points between shapes, doing linear shape casts, and doing rotational shape casts. The latter is called time of impact (TOI).


Data Structure Documentation

◆ b2SegmentDistanceResult

struct b2SegmentDistanceResult

Result of computing the distance between two line segments.

Collaboration diagram for b2SegmentDistanceResult:
Data Fields
b2Vec2 closest1 The closest point on the first segment.
b2Vec2 closest2 The closest point on the second segment.
float distanceSquared The squared distance between the closest points.
float fraction1 The barycentric coordinate on the first segment.
float fraction2 The barycentric coordinate on the second segment.

◆ b2DistanceProxy

struct b2DistanceProxy

A distance proxy is used by the GJK algorithm. It encapsulates any shape.

Collaboration diagram for b2DistanceProxy:
Data Fields
int32_t count The number of points.
b2Vec2 points[b2_maxPolygonVertices] The point cloud.
float radius The external radius of the point cloud.

◆ b2DistanceCache

struct b2DistanceCache

Used to warm start b2Distance.

Set count to zero on first call or use zero initialization.

Data Fields
uint16_t count The number of stored simplex points.
uint8_t indexA[3] The cached simplex indices on shape A.
uint8_t indexB[3] The cached simplex indices on shape B.

◆ b2DistanceInput

struct b2DistanceInput

Input for b2ShapeDistance.

Collaboration diagram for b2DistanceInput:
Data Fields
b2DistanceProxy proxyA The proxy for shape A.
b2DistanceProxy proxyB The proxy for shape B.
b2Transform transformA The world transform for shape A.
b2Transform transformB The world transform for shape B.
bool useRadii Should the proxy radius be considered?

◆ b2DistanceOutput

struct b2DistanceOutput

Output for b2ShapeDistance.

Collaboration diagram for b2DistanceOutput:
Data Fields
float distance The final distance, zero if overlapped.
int32_t iterations Number of GJK iterations used.
b2Vec2 pointA Closest point on shapeA.
b2Vec2 pointB Closest point on shapeB.
int32_t simplexCount The number of simplexes stored in the simplex array.

◆ b2SimplexVertex

struct b2SimplexVertex

Simplex vertex for debugging the GJK algorithm.

Collaboration diagram for b2SimplexVertex:
Data Fields
float a barycentric coordinate for closest point
int32_t indexA wA index
int32_t indexB wB index
b2Vec2 w wB - wA
b2Vec2 wA support point in proxyA
b2Vec2 wB support point in proxyB

◆ b2Simplex

struct b2Simplex

Simplex from the GJK algorithm.

Collaboration diagram for b2Simplex:
Data Fields
int32_t count number of valid vertices
b2SimplexVertex v1
b2SimplexVertex v2
b2SimplexVertex v3 vertices

◆ b2ShapeCastPairInput

struct b2ShapeCastPairInput

Input parameters for b2ShapeCast.

Collaboration diagram for b2ShapeCastPairInput:
Data Fields
float maxFraction The fraction of the translation to consider, typically 1.
b2DistanceProxy proxyA The proxy for shape A.
b2DistanceProxy proxyB The proxy for shape B.
b2Transform transformA The world transform for shape A.
b2Transform transformB The world transform for shape B.
b2Vec2 translationB The translation of shape B.

◆ b2Sweep

struct b2Sweep

This describes the motion of a body/shape for TOI computation.

Shapes are defined with respect to the body origin, which may not coincide with the center of mass. However, to support dynamics we must interpolate the center of mass position.

Collaboration diagram for b2Sweep:
Data Fields
b2Vec2 c1 Starting center of mass world position.
b2Vec2 c2 Ending center of mass world position.
b2Vec2 localCenter Local center of mass position.
b2Rot q1 Starting world rotation.
b2Rot q2 Ending world rotation.

◆ b2TOIInput

struct b2TOIInput

Input parameters for b2TimeOfImpact.

Collaboration diagram for b2TOIInput:
Data Fields
b2DistanceProxy proxyA The proxy for shape A.
b2DistanceProxy proxyB The proxy for shape B.
b2Sweep sweepA The movement of shape A.
b2Sweep sweepB The movement of shape B.
float tMax Defines the sweep interval [0, tMax].

◆ b2TOIOutput

struct b2TOIOutput

Output parameters for b2TimeOfImpact.

Data Fields
b2TOIState state The type of result.
float t The time of the collision.

Function Documentation

◆ b2ShapeDistance()

b2DistanceOutput b2ShapeDistance ( b2DistanceCache * cache,
const b2DistanceInput * input,
b2Simplex * simplexes,
int simplexCapacity )

Compute the closest points between two shapes represented as point clouds.

b2DistanceCache cache is input/output. On the first call set b2DistanceCache.count to zero. The underlying GJK algorithm may be debugged by passing in debug simplexes and capacity. You may pass in NULL and 0 for these.

◆ b2TimeOfImpact()

b2TOIOutput b2TimeOfImpact ( const b2TOIInput * input)

Compute the upper bound on time before two shapes penetrate.

Time is represented as a fraction between [0,tMax]. This uses a swept separating axis and may miss some intermediate, non-tunneling collisions. If you change the time interval, you should call this function again.