From 08cb8d6a585dd552b69a488d9db1be123f01467a Mon Sep 17 00:00:00 2001 From: tech kaczynski Date: Tue, 22 Sep 2026 22:38:57 +1000 Subject: initial commit. --- physics.h | 156 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 156 insertions(+) create mode 100644 physics.h (limited to 'physics.h') diff --git a/physics.h b/physics.h new file mode 100644 index 0000000..0a92118 --- /dev/null +++ b/physics.h @@ -0,0 +1,156 @@ +#ifndef PHYSICS_H +#define PHYSICS_H + +#include "common.h" + +typedef enum { + PHYS_SHAPE_BOX, + PHYS_SHAPE_SPHERE, + PHYS_SHAPE_CAPSULE, + PHYS_SHAPE_CYLINDER, + PHYS_SHAPE_CONVEX_HULL, + PHYS_SHAPE_STATIC_TRIMESH, +} PhysicsShapeTag; + +typedef enum { + PHYS_JOINT_SPHERICAL, + PHYS_JOINT_REVOLUTE, + PHYS_JOINT_PRISMATIC, + PHYS_JOINT_WELD, + PHYS_JOINT_WHEEL, + PHYS_JOINT_DISTANCE, +} PhysicsJointTag; + +typedef enum { + PHYS_JOINT_LIMIT, + PHYS_JOINT_SPRING, + PHYS_JOINT_MOTOR, +} PhysicsJointFlags; + +typedef enum { + PHYS_MODE_STATIC, + PHYS_MODE_KINEMATIC, + PHYS_MODE_DYNAMIC, +} PhysicsMode; + +typedef enum { + PHYS_MASK_DEFAULT, + PHYS_MASK_WORLD, + PHYS_MASK_PROP, + PHYS_MASK_PLAYER, + PHYS_MASK_PARTICLE, + + PHYS_MASK_EDITOR_SELECTABLE, + PHYS_MASK_EDITOR_RAYCAST, +} PhysicsMask; + +typedef struct { + PhysicsShapeTag tag; + union { + struct { + Vec3 sz; + } Box; + + struct { + f32 radius; + } Sphere; + + struct { + f32 height, radius; + } Capsule, Cylinder; + + struct { + Vec3 *points; + u32 point_cnt; + } ConvexHull; + + struct { + Vec3 *points; + u32 *indices; + u32 point_cnt, idx_cnt; + } TriMesh; + }; + + /* local to body */ + Vec3 pos; + Quaternion rot; + + f32 density, friction, restitution; + Flags32 category_bits, mask_bits; +} PhysicsShape; + +typedef struct { + PhysicsShape *shapes; + u32 shape_cnt; + + /* local to entity */ + Vec3 pos; + Quaternion rot; + + f32 linear_damping, angular_damping; + f32 gravity_scale; + PhysicsMode mode; + bool enable_sleep; +} PhysicsBody; + +/* + physics joint fields meaning changes depending on their context. + + anchor_a/axis_a are local to body_a. + anchor_b/axis_b are local to body_b. + axes should be normalized. + + lower/upper are generic limits + - revolute: radians + - prismatic: metres + - distance: metres + - wheel: suspension travel + + springs: + - spring_hz: frequency of the spring response; higher = stiffer/faster + - spring_damping: damping ratio; controls oscillation + + motors: + revolute: + motor_speed = angular velocity + motor_force = maximum torque + + prismatic: + motor_speed = linear velocity + motor_force = maximum force + + wheel: + motor_speed = wheel angular velocity + motor_force = maximum drive/braking torque +*/ +typedef struct { + PhysicsJointTag tag; + u32 body_a, body_b; + Flags32 flags; + + Vec3 anchor_a, anchor_b; + Vec3 axis_a, axis_b; + + f32 lower, upper; + f32 spring_hz, spring_damping; + f32 motor_speed, motor_force; +} PhysicsJoint; + +typedef struct { + b3BodyId id; + b3ShapeId *shapes; // count is stored in PhysicsCmp +} PhysicsBodyRuntime; + +typedef struct { + b3WorldDef world_def; + b3WorldId world_id; + bool simulate; +} PhysicsInfo; + +extern PhysicsInfo physics; + +void physics_init(void); +void physics_exit(void); +void physics_update(void); + +#endif -- cgit v1.2.3