Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions libs/cbullet/cbullet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions libs/cbullet/cbullet.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
43 changes: 43 additions & 0 deletions src/zbullet.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -285,6 +286,7 @@ pub const ShapeType = enum(c_int) {
cylinder = 13,
compound = 31,
trimesh = 21,
convex_hull = 4,
};

const ShapeImpl = opaque {
Expand All @@ -306,6 +308,7 @@ const ShapeImpl = opaque {
.capsule,
.cylinder,
.compound,
.convex_hull
=> cbtShapeDestroy(shape),
.trimesh => cbtShapeTriMeshDestroy(shape),
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;

Expand Down
Loading