box3d/shared/ghost_collisions.h

143 lines
3.7 KiB
C
Raw Normal View History

2026-10-31 00:07:57 -07:00
// SPDX-FileCopyrightText: 2026 Erin Catto
// SPDX-License-Identifier: MIT
#pragma once
#include "box3d/collision.h"
#include "box3d/id.h"
#include "box3d/math_functions.h"
#include <stdbool.h>
#ifdef __cplusplus
extern "C"
{
#endif
// Ghost collision scenes, shared by the Mesh / Ghost Collisions sample and the headless ghost scan.
// Each ground is a profile in the xy-plane extruded along z and finely tessellated, so a sliding
// body crosses many internal edges. A manifold that pushes on the body is a ghost when its normal
// points outside the span of face normals under the body. Those are the ones that block or launch
// a body.
2026-11-09 21:12:12 -08:00
// A conveyor side rail along x, the floor is on the -z side
#define GHOST_RAIL_HEIGHT 0.35f
#define GHOST_RAIL_THICKNESS 0.08f
2026-10-31 00:07:57 -07:00
typedef enum GhostScene
{
e_ghostFlat,
2026-10-31 11:43:25 -07:00
e_ghostConcave,
2026-10-31 00:07:57 -07:00
e_ghostCliff,
e_ghostCrest,
e_ghostBumps,
2026-10-31 11:43:25 -07:00
e_ghostInternal,
e_ghostChamfer,
e_ghostRail,
2026-10-31 00:07:57 -07:00
e_ghostSceneCount
} GhostScene;
typedef enum GhostBody
{
e_ghostBox,
2026-10-31 11:43:25 -07:00
e_ghostHCapsule,
e_ghostVCapsule,
2026-10-31 00:07:57 -07:00
e_ghostSphere,
e_ghostCylinder,
e_ghostBodyCount
} GhostBody;
// Mesh variants of the PEEL InternalEdges presets
#define GHOST_PEEL_PRESET_COUNT 4
// Chamfer has the longest profile
#define GHOST_MAX_PROFILE_POINTS 128
2026-10-31 00:07:57 -07:00
extern const char* g_ghostSceneNames[e_ghostSceneCount];
extern const char* g_ghostBodyNames[e_ghostBodyCount];
extern const char* g_ghostPeelPresetNames[GHOST_PEEL_PRESET_COUNT];
typedef struct GhostSettings
{
GhostScene scene;
GhostBody body;
int peelPreset;
bool mirror;
2026-11-01 22:48:05 -08:00
2026-11-06 21:41:52 -08:00
// Build the ground from height fields instead of meshes. Each height field is its own shape.
2026-11-01 22:48:05 -08:00
bool heightField;
2026-10-31 00:07:57 -07:00
float cellSize;
float speed;
// Degrees
float yaw;
float friction;
bool fixedRotation;
bool speculative;
} GhostSettings;
GhostSettings GetDefaultGhostSettings( void );
typedef struct GhostReport
{
b3Pos point;
b3Vec3 normal;
float separation;
float impulse;
} GhostReport;
typedef struct GhostCollisionsData
{
GhostSettings settings;
b3Vec2 profile[GHOST_MAX_PROFILE_POINTS];
int profileCount;
2026-10-31 14:45:17 -07:00
2026-11-09 23:14:54 -08:00
// The ground spans x in [-halfLength, halfLength] and z in [-halfWidth, halfWidth]
float halfLength;
2026-10-31 14:45:17 -07:00
float halfWidth;
2026-10-31 00:07:57 -07:00
b3MeshData* meshes[2];
int meshCount;
2026-11-01 22:48:05 -08:00
// A height field shape has no local transform, so each one gets its own static body
b3HeightFieldData* heightFields[2];
b3BodyId heightBodyIds[2];
int heightFieldCount;
2026-10-31 00:07:57 -07:00
b3HullData* cylinder;
b3HullData* peelCylinder;
b3BodyId groundId;
b3BodyId bodyId;
b3ShapeId shapeId;
2026-10-31 11:43:25 -07:00
// Body bounds at the start of the step, where the contacts were computed. A fast body can cross a fold
// within a step, so the bounds after the step can miss the face a contact came from.
b3AABB contactBounds;
2026-10-31 00:07:57 -07:00
// Steps in the current run
int runSteps;
// Completed runs, each ending when the body reaches the end or falls off
int runCount;
int runGhostCount;
int ghostCount;
} GhostCollisionsData;
// Creates the ground and launches the body
GhostCollisionsData CreateGhostCollisions( b3WorldId worldId, GhostSettings settings );
2026-11-01 22:48:05 -08:00
// Rebuild the ground after a scene, preset, mirror, height field or cell size change. Relaunch after this.
2026-10-31 00:07:57 -07:00
void CreateGhostGround( b3WorldId worldId, GhostCollisionsData* data );
// Launch a new body from the start, after a body, speed, yaw, friction or rotation change
void LaunchGhostBody( b3WorldId worldId, GhostCollisionsData* data );
// Call after each world step. Relaunches a body that finished its run, then counts the ghost
// manifolds on the body. Returns the ghost count for this step and writes up to capacity reports.
int UpdateGhostCollisions( b3WorldId worldId, GhostCollisionsData* data, GhostReport* reports, int capacity );
void DestroyGhostCollisions( GhostCollisionsData* data );
#ifdef __cplusplus
}
#endif