Update shape from immediate_body

This commit is contained in:
sergeypdev 2025-02-09 19:33:19 +04:00
parent d816e96c3e
commit aa1dab6079
4 changed files with 27 additions and 7 deletions

View File

@ -1,6 +1,9 @@
package halfedge package halfedge
import lg "core:math/linalg" import lg "core:math/linalg"
import "core:hash/xxhash"
import "core:slice"
import "core:mem"
Vec3 :: [3]f32 Vec3 :: [3]f32
@ -290,3 +293,15 @@ iterate_next_vertex_edge :: proc(
return 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)
}

View File

@ -17,6 +17,7 @@ immediate_body :: proc(
) -> ( ) -> (
handle: Body_Handle, handle: Body_Handle,
) { ) {
sim_state := get_sim_state(scene)
if id in state.immedate_bodies { if id in state.immedate_bodies {
body := &state.immedate_bodies[id] body := &state.immedate_bodies[id]
if body.last_ref != state.simulation_frame { if body.last_ref != state.simulation_frame {
@ -24,10 +25,10 @@ immediate_body :: proc(
state.num_referenced_bodies += 1 state.num_referenced_bodies += 1
} }
handle = body.handle 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 { } else {
state.num_referenced_bodies += 1 state.num_referenced_bodies += 1
handle = add_body(get_sim_state(scene), config) handle = add_body(sim_state, config)
state.immedate_bodies[id] = { state.immedate_bodies[id] = {
handle = handle, handle = handle,
last_ref = state.simulation_frame, last_ref = state.simulation_frame,

View File

@ -157,7 +157,7 @@ get_body :: proc(sim_state: ^Sim_State, handle: Body_Handle) -> Body_Ptr {
return &sim_state.bodies_slice[index] 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 { switch &s in shape {
case Shape_Box: case Shape_Box:
case Internal_Shape_Convex: 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) body.inv_mass, body.inv_inertia_tensor = calculate_body_params_from_config(config)
} }
update_body_from_config :: proc(body: Body_Ptr, config: Body_Config) { update_body_from_config :: proc(sim_state: ^Sim_State, body: Body_Ptr, config: Body_Config) {
// TODO: Figure out how to update shape // Inefficient, but eh
// body.shape = config.shape 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) 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 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 body.next_plus_one = sim_state.first_free_body_plus_one
sim_state.first_free_body_plus_one = i32(handle) sim_state.first_free_body_plus_one = i32(handle)

View File

@ -48,6 +48,8 @@ prepare_next_sim_state :: proc(scene: ^Scene) {
current_state := get_sim_state(scene) current_state := get_sim_state(scene)
next_state := get_next_sim_state(scene) next_state := get_next_sim_state(scene)
convex_container_reconcile(&current_state.convex_container)
next_state.first_free_body_plus_one = current_state.first_free_body_plus_one next_state.first_free_body_plus_one = current_state.first_free_body_plus_one
next_state.first_free_suspension_constraint_plus_one = next_state.first_free_suspension_constraint_plus_one =
current_state.first_free_suspension_constraint_plus_one current_state.first_free_suspension_constraint_plus_one