Added triangle mesh colliders

This commit is contained in:
2025-07-24 16:14:29 +02:00
parent 0d51dc8236
commit e0f1dd5e70
2 changed files with 56 additions and 0 deletions

View File

@@ -55,6 +55,7 @@ MAX_CHILDREN :: 16;
SPHERE; SPHERE;
BOX; BOX;
CAPSULE; CAPSULE;
MESH;
} }
Physics_Lock :: enum_flags u8 { Physics_Lock :: enum_flags u8 {

View File

@@ -43,6 +43,8 @@ init_physx :: () {
tolerance_scale.speed = 10; tolerance_scale.speed = 10;
physics = PhysX.PxCreatePhysics(PhysX.PX_PHYSICS_VERSION, foundation, *tolerance_scale, true, pvd, null); physics = PhysX.PxCreatePhysics(PhysX.PX_PHYSICS_VERSION, foundation, *tolerance_scale, true, pvd, null);
cooking_params = PhysX.PxCookingParams_new(*tolerance_scale);
dispatcher = PhysX.PxDefaultCpuDispatcherCreate(2); dispatcher = PhysX.PxDefaultCpuDispatcherCreate(2);
material = PhysX.PxPhysics_createMaterial(physics, 0.0, 0.0, 0.6); material = PhysX.PxPhysics_createMaterial(physics, 0.0, 0.0, 0.6);
@@ -201,6 +203,58 @@ create_physx_actor :: (e: *Entity) {
case .CAPSULE; { case .CAPSULE; {
geo = PhysX.PxCapsuleGeometry_new(e.physics.capsule.radius, e.physics.capsule.half_height-e.physics.capsule.radius); geo = PhysX.PxCapsuleGeometry_new(e.physics.capsule.radius, e.physics.capsule.half_height-e.physics.capsule.radius);
} }
case .MESH; {
if e.flags & .RENDERABLE {
points : [..] Vector3;
points.allocator = temp;
indices : [..] u32;
indices.allocator = temp;
model := get_model_by_handle(e.renderable.model);
for node, node_index: model.nodes {
index_start : u32 = xx indices.count;
render_data := e.renderable.nodes[node_index];
success, inv_matrix := inverse(e.transform.model_matrix);
// We need to undo the local to world part of every world matrix
matrix := inv_matrix * render_data.transform.world_matrix;
if node.meshes.count > 0 {
for m, mi: node.meshes {
mesh := parray_get(*engine.renderer.meshes, m);
for v: mesh.positions {
array_add(*points, transform_position(v, matrix));
}
for i: mesh.indices {
array_add(*indices, index_start + i);
}
}
}
}
mesh_desc := PhysX.PxTriangleMeshDesc_new();
mesh_desc.points.count = xx points.count;
mesh_desc.points.stride = size_of(Vector3);
mesh_desc.points.data = points.data;
mesh_desc.triangles.count = cast(u32)(indices.count / 3);
mesh_desc.triangles.stride = 3 * size_of(u32);
mesh_desc.triangles.data = indices.data;
if PhysX.PxValidateTriangleMesh(*cooking_params, *mesh_desc) {
assert(false);
}
stream : PhysX.PxOutputStream;
callback := PhysX.PxGetStandaloneInsertionCallback();
//read_buffer : PhysX.PxDefaultMemoryInputData_new(;
cond : s32;
mesh := PhysX.PxCreateTriangleMesh(*cooking_params, *mesh_desc, callback, null);
scale := PhysX.PxMeshScale_new(*e.transform.scale);
geo = PhysX.PxTriangleMeshGeometry_new(mesh, *scale, 0);
}
}
} }
shape := PhysX.PxPhysics_createShape(physics, geo, material, false, ifx e.physics.trigger then PHYSX_DEFAULT_TRIGGER_SHAPE_FLAGS else PHYSX_DEFAULT_SIMULATION_SHAPE_FLAGS); shape := PhysX.PxPhysics_createShape(physics, geo, material, false, ifx e.physics.trigger then PHYSX_DEFAULT_TRIGGER_SHAPE_FLAGS else PHYSX_DEFAULT_SIMULATION_SHAPE_FLAGS);
@@ -246,6 +300,7 @@ PhysX :: #import "PhysX";
#scope_file #scope_file
physics : *PhysX.PxPhysics; physics : *PhysX.PxPhysics;
cooking_params: PhysX.PxCookingParams;
material : *PhysX.PxMaterial; material : *PhysX.PxMaterial;
default_allocator : PhysX.PxAllocatorCallback; default_allocator : PhysX.PxAllocatorCallback;
default_error_callback : PhysX.PxErrorCallback; default_error_callback : PhysX.PxErrorCallback;