box3d/src/shape.h

209 lines
7.5 KiB
C
Raw Normal View History

2026-05-10 13:36:52 -07:00
// SPDX-FileCopyrightText: 2025 Erin Catto
// SPDX-License-Identifier: MIT
#pragma once
#include "math_internal.h"
#include "box3d/collision.h"
2026-05-10 13:36:52 -07:00
#include "box3d/types.h"
#include <stdbool.h>
typedef struct b3BroadPhase b3BroadPhase;
typedef struct b3World b3World;
typedef enum b3ShapeFlags
{
b3_enableSensorEvents = 0x01,
b3_enableContactEvents = 0x02,
b3_enableCustomFiltering = 0x04,
b3_enableHitEvents = 0x08,
b3_enablePreSolveEvents = 0x10,
// 0x20 is free, it was the enlarged AABB flag before the broad-phase marked the tree
b3_enableSpeculative = 0x40,
} b3ShapeFlags;
2026-05-10 13:36:52 -07:00
typedef struct b3Shape
{
int id;
int bodyId;
int prevShapeId;
int nextShapeId;
int sensorIndex;
int proxyKey;
b3ShapeType type;
float density;
float explosionScale;
float aabbMargin;
2026-05-10 13:36:52 -07:00
b3AABB aabb;
b3Vec3 localCentroid;
int materialCount;
b3SurfaceMaterial material;
2026-05-10 13:36:52 -07:00
b3SurfaceMaterial* materials;
b3Filter filter;
void* userData;
void* userShape;
uint32_t nameId;
uint16_t generation;
// b3ShapeFlags
uint8_t flags;
2026-05-10 13:36:52 -07:00
union
{
b3Capsule capsule;
b3Sphere sphere;
const b3HullData* hull;
2026-05-10 13:36:52 -07:00
b3Mesh mesh;
const b3HeightFieldData* heightField;
const b3CompoundData* compound;
2026-05-10 13:36:52 -07:00
};
} b3Shape;
// A single material shape keeps its material inline. Multi material meshes and compounds own a heap
// array. Reach the materials the same way for both: a single material shape presents its inline
// material as a one element array. Do not cache the pointer, the shapes array can move.
static inline b3SurfaceMaterial* b3GetShapeMaterials( const b3Shape* shape )
{
return shape->materials != NULL ? shape->materials : (b3SurfaceMaterial*)&shape->material;
}
void b3CreateShapeProxy( b3World* world, b3Shape* shape, b3BodyType type, b3WorldTransform transform, bool forcePairCreation );
2026-05-10 13:36:52 -07:00
void b3DestroyShapeProxy( b3Shape* shape, b3BroadPhase* bp );
void b3DestroyShapeAllocations( b3World* world, b3Shape* shape );
b3MassData b3ComputeShapeMass( const b3Shape* shape );
b3ShapeExtent b3ComputeShapeExtent( const b3Shape* shape, b3Vec3 localCenter );
float b3ComputeShapeMinExtent( const b3Shape* shape, b3Vec3 localCenter );
2026-05-10 13:36:52 -07:00
b3AABB b3ComputeSweptSphereAABB( const b3Sphere* shape, b3Transform xf1, b3Transform xf2 );
b3AABB b3ComputeSweptCapsuleAABB( const b3Capsule* shape, b3Transform xf1, b3Transform xf2 );
b3AABB b3ComputeShapeAABB( const b3Shape* shape, b3Transform transform );
// Conservative world AABB for a shape inflated by extra margin. In double precision mode the
// box is built in the body local frame, translated by the double origin, and rounded outward.
b3AABB b3ComputeFatShapeAABB( const b3Shape* shape, b3WorldTransform transform, float extra );
2026-05-10 13:36:52 -07:00
b3AABB b3ComputeSweptShapeAABB( const b3Shape* shape, const b3Sweep* sweep, float time );
b3Vec3 b3GetShapeCentroid( const b3Shape* shape );
float b3GetShapeArea( const b3Shape* shape );
float b3GetShapeProjectedArea( const b3Shape* shape, b3Vec3 planeNormal );
uint64_t b3GetShapeUserMaterialId( const b3Shape* shape, int childIndex, int triangleIndex );
b3ShapeProxy b3MakeShapeProxy( const b3Shape* shape );
b3ShapeProxy b3MakeLocalProxy( const b3ShapeProxy* proxy, b3Transform transform, b3Vec3* buffer );
b3AABB b3ComputeProxyAABB( const b3ShapeProxy* proxy );
b3CastOutput b3RayCastShape( const b3Shape* shape, b3Transform transform, const b3RayCastInput* input );
b3CastOutput b3ShapeCastShape( const b3Shape* shape, b3Transform transform, const b3ShapeCastInput* input );
bool b3OverlapShape( const b3Shape* shape, b3Transform transform, const b3ShapeProxy* proxy );
float b3GetShapeArea( const b3Shape* shape );
float b3GetShapeProjectedArea( const b3Shape* shape, b3Vec3 planeNormal );
b3TOIOutput b3ShapeTimeOfImpact( b3Shape* shapeA, b3Shape* shapeB, b3Sweep* sweepA, b3Sweep* sweepB, float maxFraction );
int b3CollideMoverAndSphere( b3PlaneResult* result, const b3Sphere* shape, const b3Capsule* mover );
int b3CollideMoverAndCapsule( b3PlaneResult* result, const b3Capsule* shape, const b3Capsule* mover );
int b3CollideMoverAndHull( b3PlaneResult* result, const b3HullData* shape, const b3Capsule* mover );
2026-05-10 13:36:52 -07:00
int b3CollideMoverAndMesh( b3PlaneResult* planes, int capacity, const b3Mesh* shape, const b3Capsule* mover );
int b3CollideMoverAndHeightField( b3PlaneResult* results, int capacity, const b3HeightFieldData* shape, const b3Capsule* mover );
2026-05-10 13:36:52 -07:00
int b3CollideMover( b3PlaneResult* planes, int planeCapacity, const b3Shape* shape, b3Transform transform,
const b3Capsule* mover );
// Hull
int b3FindHullSupportVertex( const b3HullData* hull, b3Vec3 direction );
int b3FindHullSupportFace( const b3HullData* hull, b3Vec3 direction );
bool b3IsValidHull( const b3HullData* hull );
b3AABB b3ComputeSweptHullAABB( const b3HullData* shape, b3Transform xf1, b3Transform xf2 );
b3ShapeExtent b3ComputeHullExtent( const b3HullData* hull, b3Vec3 origin );
float b3ComputeHullProjectedArea( const b3HullData* hull, b3Vec3 direction );
2026-05-10 13:36:52 -07:00
// Height field
b3Triangle b3GetHeightFieldTriangle( const b3HeightFieldData* heightField, int triangleIndex );
int b3GetHeightFieldMaterial( const b3HeightFieldData* heightField, int triangleIndex );
2026-05-10 13:36:52 -07:00
static inline int b3GetHeightFieldTriangleCount( const b3HeightFieldData* heightField )
2026-05-10 13:36:52 -07:00
{
int cellCount = ( heightField->rowCount - 1 ) * ( heightField->columnCount - 1 );
return 2 * cellCount;
}
// Mesh
// Making this inline made the tree benchmarks faster.
static inline b3Triangle b3GetMeshTriangle( const b3Mesh* mesh, int triangleIndex )
{
B3_ASSERT( 0 <= triangleIndex && triangleIndex < mesh->data->triangleCount );
const b3MeshTriangle* triangles = b3GetMeshTriangles( mesh->data );
const uint8_t* flags = b3GetMeshFlags( mesh->data );
const b3Vec3* vertices = b3GetMeshVertices( mesh->data );
b3Triangle result;
b3MeshTriangle triangle = triangles[triangleIndex];
uint8_t triangleFlags = flags[triangleIndex];
b3Vec3 scale = mesh->scale;
result.vertices[0] = b3Mul( scale, vertices[triangle.index1] );
result.i1 = triangle.index1;
if ( scale.x * scale.y * scale.z < 0.0f )
{
result.vertices[1] = b3Mul( scale, vertices[triangle.index3] );
result.vertices[2] = b3Mul( scale, vertices[triangle.index2] );
result.i2 = triangle.index3;
result.i3 = triangle.index2;
// Hand mesh inversion.
int edge1Bits = triangleFlags & ( b3_concaveEdge1 | b3_inverseConcaveEdge1 );
int edge3Bits = triangleFlags & ( b3_concaveEdge3 | b3_inverseConcaveEdge3 );
result.flags = triangleFlags & ~( b3_concaveEdge1 | b3_concaveEdge3 | b3_inverseConcaveEdge1 | b3_inverseConcaveEdge3 );
result.flags |= edge1Bits << 2;
result.flags |= edge3Bits >> 2;
}
else
{
result.vertices[1] = b3Mul( scale, vertices[triangle.index2] );
result.vertices[2] = b3Mul( scale, vertices[triangle.index3] );
result.i2 = triangle.index2;
result.i3 = triangle.index3;
result.flags = triangleFlags;
}
return result;
}
2026-10-30 15:37:39 -07:00
bool b3GetMeshNeighborNormal( const b3Mesh* mesh, int triangleIndex, int edgeIndex, b3Vec3* normal );
2026-05-10 13:36:52 -07:00
bool b3IsValidMesh( const b3MeshData* meshData );
static inline bool b3ShouldShapesCollide( b3Filter filterA, b3Filter filterB )
2026-05-10 13:36:52 -07:00
{
if ( filterA.groupIndex == filterB.groupIndex && filterA.groupIndex != 0 )
{
return filterA.groupIndex > 0;
}
return ( filterA.maskBits & filterB.categoryBits ) != 0 && ( filterA.categoryBits & filterB.maskBits ) != 0;
}
static inline bool b3ShouldQueryCollide( const b3Filter* shapeFilter, const b3QueryFilter* queryFilter )
2026-05-10 13:36:52 -07:00
{
return ( shapeFilter->categoryBits & queryFilter->maskBits ) != 0 &&
( shapeFilter->maskBits & queryFilter->categoryBits ) != 0;
}
static inline bool b3IsConvex( b3ShapeType type )
{
return type == b3_sphereShape || type == b3_capsuleShape || type == b3_hullShape;
}