diff --git a/libs/cbullet/cbullet.cpp b/libs/cbullet/cbullet.cpp index 1a2ee57..000b264 100644 --- a/libs/cbullet/cbullet.cpp +++ b/libs/cbullet/cbullet.cpp @@ -430,6 +430,7 @@ CbtShapeHandle cbtShapeAllocate(int shape_type) { case CBT_SHAPE_TYPE_CONE: size = sizeof(btConeShape); break; case CBT_SHAPE_TYPE_CYLINDER: size = sizeof(btCylinderShape); break; case CBT_SHAPE_TYPE_COMPOUND: size = sizeof(btCompoundShape); break; + case CBT_SHAPE_TYPE_CONVEX_HULL: size = sizeof(btConvexHullShape); break; case CBT_SHAPE_TYPE_TRIANGLE_MESH: size = sizeof(btBvhTriangleMeshShape) + sizeof(btTriangleIndexVertexArray); break; @@ -652,6 +653,26 @@ int cbtShapeConeGetUpAxis(CbtShapeHandle shape_handle) { return shape->getConeUpIndex(); } +void cbtShapeConvexHullCreate(CbtShapeHandle shape_handle, const float* points, int num_points, int stride){ + assert(shape_handle && !cbtShapeIsCreated(shape_handle)); + assert(cbtShapeGetType(shape_handle) == CBT_SHAPE_TYPE_CONVEX_HULL); + new (shape_handle) btConvexHullShape(points, num_points, stride); +} + +void cbtShapeConvexHullAddPoint(CbtShapeHandle shape_handle, const CbtVector3 point, bool recalculate_local_aabb){ + assert(shape_handle && cbtShapeIsCreated(shape_handle)); + assert(cbtShapeGetType(shape_handle) == CBT_SHAPE_TYPE_CONVEX_HULL); + auto shape = (btConvexHullShape*)shape_handle; + shape->addPoint(btVector3(point[0], point[1], point[2]), recalculate_local_aabb); +} + +void cbtShapeConvexHullRecalcLocalAabb(CbtShapeHandle shape_handle){ + assert(shape_handle && cbtShapeIsCreated(shape_handle)); + assert(cbtShapeGetType(shape_handle) == CBT_SHAPE_TYPE_CONVEX_HULL); + auto shape = (btConvexHullShape*)shape_handle; + shape->recalcLocalAabb(); +} + void cbtShapeCompoundCreate( CbtShapeHandle shape_handle, bool enable_dynamic_aabb_tree, diff --git a/libs/cbullet/cbullet.h b/libs/cbullet/cbullet.h index 1362997..42e327a 100644 --- a/libs/cbullet/cbullet.h +++ b/libs/cbullet/cbullet.h @@ -36,6 +36,7 @@ #define CBT_SHAPE_TYPE_CYLINDER 13 #define CBT_SHAPE_TYPE_COMPOUND 31 #define CBT_SHAPE_TYPE_TRIANGLE_MESH 21 +#define CBT_SHAPE_TYPE_CONVEX_HULL 4 // cbtConGetType, cbtConAllocate #define CBT_CONSTRAINT_TYPE_POINT2POINT 3 @@ -259,6 +260,10 @@ float cbtShapeConeGetRadius(CbtShapeHandle shape_handle); float cbtShapeConeGetHeight(CbtShapeHandle shape_handle); int cbtShapeConeGetUpAxis(CbtShapeHandle shape_handle); +void cbtShapeConvexHullCreate(CbtShapeHandle shape_handle, const float* points, int num_points, int stride); +void cbtShapeConvexHullAddPoint(CbtShapeHandle shape_handle, const CbtVector3 point, bool recalculate_local_aabb); +void cbtShapeConvexHullRecalcLocalAabb(CbtShapeHandle shape_handle); + void cbtShapeCompoundCreate( CbtShapeHandle shape_handle, bool enable_dynamic_aabb_tree, // true diff --git a/src/zbullet.zig b/src/zbullet.zig index 6a42321..a93b6af 100644 --- a/src/zbullet.zig +++ b/src/zbullet.zig @@ -12,6 +12,7 @@ pub const BoxShape = *align(@sizeOf(usize)) BoxShapeImpl; pub const SphereShape = *align(@sizeOf(usize)) SphereShapeImpl; pub const CapsuleShape = *align(@sizeOf(usize)) CapsuleShapeImpl; pub const CylinderShape = *align(@sizeOf(usize)) CylinderShapeImpl; +pub const ConvexHullShape = *align(@sizeOf(usize)) ConvexHullShapeImpl; pub const CompoundShape = *align(@sizeOf(usize)) CompoundShapeImpl; pub const TriangleMeshShape = *align(@sizeOf(usize)) TriangleMeshShapeImpl; pub const Body = *align(@sizeOf(usize)) BodyImpl; @@ -285,6 +286,7 @@ pub const ShapeType = enum(c_int) { cylinder = 13, compound = 31, trimesh = 21, + convex_hull = 4, }; const ShapeImpl = opaque { @@ -306,6 +308,7 @@ const ShapeImpl = opaque { .capsule, .cylinder, .compound, + .convex_hull => cbtShapeDestroy(shape), .trimesh => cbtShapeTriMeshDestroy(shape), } @@ -561,6 +564,40 @@ const CylinderShapeImpl = opaque { extern fn cbtShapeCylinderGetUpAxis(capsule: CylinderShape) Axis; }; +const ConvexHullShapeImpl = opaque { + pub usingnamespace ShapeFunctions(ConvexHullShape); + + fn alloc() ConvexHullShape { + return @as(ConvexHullShape, @ptrCast(ShapeImpl.alloc(.convex_hull))); + } + + pub const create = cbtShapeConvexHullCreate; + extern fn cbtShapeConvexHullCreate( + convex_hull: ConvexHullShape, + points: ?*f32, + num_points: i32, + stride: i32, + ) void; + + pub const recalcLocalAabb = cbtShapeConvexHullRecalcLocalAabb; + extern fn cbtShapeConvexHullRecalcLocalAabb(convex_hull: ConvexHullShape) void; + + pub const addPoint = cbtShapeConvexHullAddPoint; + extern fn cbtShapeConvexHullAddPoint(convex_hull: ConvexHullShape, point: *const [3]f32, recalculate_local_aabb: bool) void; +}; + +pub fn initConvexHullShape( + args: struct { + points: ?*f32 = null, + num_points: i32 = 0, + stride: i32 = @sizeOf([3]f32), + }, +) ConvexHullShape { + const cshape = ConvexHullShapeImpl.alloc(); + cshape.create(args.points, args.num_points, args.stride); + return cshape; +} + pub fn initCompoundShape( args: struct { enable_dynamic_aabb_tree: bool = true, @@ -787,6 +824,12 @@ const BodyImpl = opaque { pub const setDamping = cbtBodySetDamping; extern fn cbtBodySetDamping(body: Body, linear: f32, angular: f32) void; + pub const setLinearVelocity = cbtBodySetLinearVelocity; + extern fn cbtBodySetLinearVelocity(body: Body, velocity: *const [3]f32) void; + + pub const getLinearVelocity = cbtBodyGetLinearVelocity; + extern fn cbtBodyGetLinearVelocity(body: Body, velocity: *[3]f32) void; + pub const setAngularFactor = cbtBodySetAngularFactor; extern fn cbtBodySetAngularFactor(body: Body, factor: *const [3]f32) void;