From aa1dab6079b18b015fb0d8315a7a51d21d410f3d Mon Sep 17 00:00:00 2001 From: sergeypdev Date: Sun, 9 Feb 2025 19:33:19 +0400 Subject: [PATCH] Update shape from immediate_body --- game/halfedge/halfedge.odin | 15 +++++++++++++++ game/physics/immediate.odin | 5 +++-- game/physics/scene.odin | 12 +++++++----- game/physics/simulation.odin | 2 ++ 4 files changed, 27 insertions(+), 7 deletions(-) diff --git a/game/halfedge/halfedge.odin b/game/halfedge/halfedge.odin index 8ff0e21..ca71fb3 100644 --- a/game/halfedge/halfedge.odin +++ b/game/halfedge/halfedge.odin @@ -1,6 +1,9 @@ package halfedge import lg "core:math/linalg" +import "core:hash/xxhash" +import "core:slice" +import "core:mem" Vec3 :: [3]f32 @@ -290,3 +293,15 @@ iterate_next_vertex_edge :: proc( return } + +hash :: proc(mesh: Half_Edge_Mesh) -> u32 { + state: xxhash.XXH32_state + xxhash.XXH32_reset_state(&state) + + _ = xxhash.XXH32_update(&state, mem.any_to_bytes(mesh.center)) + _ = xxhash.XXH32_update(&state, slice.to_bytes(mesh.vertices)) + _ = xxhash.XXH32_update(&state, slice.to_bytes(mesh.faces)) + _ = xxhash.XXH32_update(&state, slice.to_bytes(mesh.edges)) + + return xxhash.XXH32_digest(&state) +} diff --git a/game/physics/immediate.odin b/game/physics/immediate.odin index 6c17958..d77924d 100644 --- a/game/physics/immediate.odin +++ b/game/physics/immediate.odin @@ -17,6 +17,7 @@ immediate_body :: proc( ) -> ( handle: Body_Handle, ) { + sim_state := get_sim_state(scene) if id in state.immedate_bodies { body := &state.immedate_bodies[id] if body.last_ref != state.simulation_frame { @@ -24,10 +25,10 @@ immediate_body :: proc( state.num_referenced_bodies += 1 } handle = body.handle - update_body_from_config(get_body(get_sim_state(scene), handle), config) + update_body_from_config(sim_state, get_body(sim_state, handle), config) } else { state.num_referenced_bodies += 1 - handle = add_body(get_sim_state(scene), config) + handle = add_body(sim_state, config) state.immedate_bodies[id] = { handle = handle, last_ref = state.simulation_frame, diff --git a/game/physics/scene.odin b/game/physics/scene.odin index 5b2590e..5fdebaf 100644 --- a/game/physics/scene.odin +++ b/game/physics/scene.odin @@ -157,7 +157,7 @@ get_body :: proc(sim_state: ^Sim_State, handle: Body_Handle) -> Body_Ptr { return &sim_state.bodies_slice[index] } -remove_shape :: proc(sim_state: ^Sim_State, shape: ^Collision_Shape) { +remove_shape :: proc(sim_state: ^Sim_State, shape: Collision_Shape) { switch &s in shape { case Shape_Box: case Internal_Shape_Convex: @@ -240,9 +240,11 @@ initialize_body_from_config :: proc(sim_state: ^Sim_State, body: ^Body, config: body.inv_mass, body.inv_inertia_tensor = calculate_body_params_from_config(config) } -update_body_from_config :: proc(body: Body_Ptr, config: Body_Config) { - // TODO: Figure out how to update shape - // body.shape = config.shape +update_body_from_config :: proc(sim_state: ^Sim_State, body: Body_Ptr, config: Body_Config) { + // Inefficient, but eh + remove_shape(sim_state, body.shape) + body.shape = add_shape(sim_state, config.shape) + body.inv_mass, body.inv_inertia_tensor = calculate_body_params_from_config(config) } @@ -291,7 +293,7 @@ remove_body :: proc(sim_state: ^Sim_State, handle: Body_Handle) { body.alive = false - remove_shape(sim_state, &body.shape) + remove_shape(sim_state, body.shape) body.next_plus_one = sim_state.first_free_body_plus_one sim_state.first_free_body_plus_one = i32(handle) diff --git a/game/physics/simulation.odin b/game/physics/simulation.odin index c410977..d88337f 100644 --- a/game/physics/simulation.odin +++ b/game/physics/simulation.odin @@ -48,6 +48,8 @@ prepare_next_sim_state :: proc(scene: ^Scene) { current_state := get_sim_state(scene) next_state := get_next_sim_state(scene) + convex_container_reconcile(¤t_state.convex_container) + next_state.first_free_body_plus_one = current_state.first_free_body_plus_one next_state.first_free_suspension_constraint_plus_one = current_state.first_free_suspension_constraint_plus_one