From 660a535a7c890081bedb3c25c306969a167802db Mon Sep 17 00:00:00 2001 From: sergeypdev Date: Sat, 1 Feb 2025 01:07:18 +0400 Subject: [PATCH] Experiment with different com calculations --- game/assets/assets.odin | 16 +++++++++++++--- game/game.odin | 3 ++- game/physics/scene.odin | 2 ++ 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/game/assets/assets.odin b/game/assets/assets.odin index 4e072da..1deebb7 100644 --- a/game/assets/assets.odin +++ b/game/assets/assets.odin @@ -33,8 +33,9 @@ Loaded_BVH :: struct { } Loaded_Convex :: struct { - mesh: collision.Convex, - center_of_mass: rl.Vector3, + mesh: collision.Convex, + center_of_mass: rl.Vector3, + center_of_mass_tris: rl.Vector3, } 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_of_mass: rl.Vector3 + center_of_mass_tris: rl.Vector3 mesh := halfedge.Half_Edge_Mesh { vertices = vertices[:], @@ -373,6 +375,7 @@ get_convex :: proc(assetman: ^Asset_Manager, path: cstring) -> (result: Loaded_C // Center of mass calculation total_volume := f32(0.0) + total_area_tris := f32(0.0) { rlgl.Begin(rlgl.TRIANGLES) 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 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 } @@ -418,8 +423,13 @@ get_convex :: proc(assetman: ^Asset_Manager, path: cstring) -> (result: Loaded_C assert(total_volume > 0, "degenerate convex hull") 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) { diff --git a/game/game.odin b/game/game.odin index ba9ab70..a3cafc5 100644 --- a/game/game.odin +++ b/game/game.odin @@ -555,8 +555,9 @@ draw :: proc() { car_convex := assets.get_convex(&g_mem.assetman, "assets/car_convex.obj") 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_tris, 0.5, 8, 8, rl.GREEN) box1_convex := collision.box_to_convex(box1, context.temp_allocator) box2_convex := collision.box_to_convex(box2, context.temp_allocator) diff --git a/game/physics/scene.odin b/game/physics/scene.odin index 07f3991..f9e6db7 100644 --- a/game/physics/scene.odin +++ b/game/physics/scene.odin @@ -47,6 +47,8 @@ Shape_Box :: struct { size: rl.Vector3, } +Shape_Convex :: struct {} + Collision_Shape :: union { Shape_Sphere, Shape_Box,