Experiment with different com calculations

This commit is contained in:
sergeypdev 2025-02-01 01:07:18 +04:00
parent ba686422a8
commit 660a535a7c
3 changed files with 17 additions and 4 deletions

View File

@ -33,8 +33,9 @@ Loaded_BVH :: struct {
} }
Loaded_Convex :: struct { Loaded_Convex :: struct {
mesh: collision.Convex, mesh: collision.Convex,
center_of_mass: rl.Vector3, center_of_mass: rl.Vector3,
center_of_mass_tris: rl.Vector3,
} }
destroy_loaded_bvh :: proc(loaded_bvh: Loaded_BVH) { destroy_loaded_bvh :: proc(loaded_bvh: Loaded_BVH) {
@ -363,6 +364,7 @@ get_convex :: proc(assetman: ^Asset_Manager, path: cstring) -> (result: Loaded_C
center /= f32(len(vertices)) center /= f32(len(vertices))
center_of_mass: rl.Vector3 center_of_mass: rl.Vector3
center_of_mass_tris: rl.Vector3
mesh := halfedge.Half_Edge_Mesh { mesh := halfedge.Half_Edge_Mesh {
vertices = vertices[:], vertices = vertices[:],
@ -373,6 +375,7 @@ get_convex :: proc(assetman: ^Asset_Manager, path: cstring) -> (result: Loaded_C
// Center of mass calculation // Center of mass calculation
total_volume := f32(0.0) total_volume := f32(0.0)
total_area_tris := f32(0.0)
{ {
rlgl.Begin(rlgl.TRIANGLES) rlgl.Begin(rlgl.TRIANGLES)
rlgl.End() rlgl.End()
@ -407,6 +410,8 @@ get_convex :: proc(assetman: ^Asset_Manager, path: cstring) -> (result: Loaded_C
tetra_centroid := (tri[0] + tri[1] + tri[2] + center) * 0.25 tetra_centroid := (tri[0] + tri[1] + tri[2] + center) * 0.25
center_of_mass += tetra_volume * tetra_centroid center_of_mass += tetra_volume * tetra_centroid
center_of_mass_tris += 1.0 / 3.0 * (tri[0] + tri[1] + tri[2]) * tri_area
total_area_tris += tri_area
tri_idx += 1 tri_idx += 1
} }
@ -418,8 +423,13 @@ get_convex :: proc(assetman: ^Asset_Manager, path: cstring) -> (result: Loaded_C
assert(total_volume > 0, "degenerate convex hull") assert(total_volume > 0, "degenerate convex hull")
center_of_mass /= total_volume center_of_mass /= total_volume
center_of_mass_tris /= total_area_tris
return {mesh = mesh, center_of_mass = center_of_mass} return {
mesh = mesh,
center_of_mass = center_of_mass,
center_of_mass_tris = center_of_mass_tris,
}
} }
debug_draw_tetrahedron_wires :: proc(tri: [3]rl.Vector3, p: rl.Vector3, color: rl.Color) { debug_draw_tetrahedron_wires :: proc(tri: [3]rl.Vector3, p: rl.Vector3, color: rl.Color) {

View File

@ -555,8 +555,9 @@ draw :: proc() {
car_convex := assets.get_convex(&g_mem.assetman, "assets/car_convex.obj") car_convex := assets.get_convex(&g_mem.assetman, "assets/car_convex.obj")
halfedge.debug_draw_mesh_wires(car_convex.mesh, rl.RED) halfedge.debug_draw_mesh_wires(car_convex.mesh, rl.RED)
rl.DrawSphereWires(car_convex.mesh.center, 0.5, 8, 8, rl.BLUE) rl.DrawSphereWires(car_convex.mesh.center, 0.0, 8, 8, rl.BLUE)
rl.DrawSphereWires(car_convex.center_of_mass, 0.5, 8, 8, rl.RED) rl.DrawSphereWires(car_convex.center_of_mass, 0.5, 8, 8, rl.RED)
rl.DrawSphereWires(car_convex.center_of_mass_tris, 0.5, 8, 8, rl.GREEN)
box1_convex := collision.box_to_convex(box1, context.temp_allocator) box1_convex := collision.box_to_convex(box1, context.temp_allocator)
box2_convex := collision.box_to_convex(box2, context.temp_allocator) box2_convex := collision.box_to_convex(box2, context.temp_allocator)

View File

@ -47,6 +47,8 @@ Shape_Box :: struct {
size: rl.Vector3, size: rl.Vector3,
} }
Shape_Convex :: struct {}
Collision_Shape :: union { Collision_Shape :: union {
Shape_Sphere, Shape_Sphere,
Shape_Box, Shape_Box,