divide forces by mass only once during physics sim

This commit is contained in:
jacob 2024-08-22 13:05:20 -05:00
parent 687ebb4eb9
commit bd237e211b

View File

@ -192,8 +192,6 @@ INTERNAL void spawn_test_entities(void)
f32 width = (f32)DEFAULT_CAMERA_WIDTH;
f32 height = (f32)DEFAULT_CAMERA_HEIGHT;
e->camera_quad_xform = XFORM_TRS(.s = V2(width, height));
activate_now(e);
}
}
@ -600,23 +598,21 @@ INTERNAL void game_update(struct game_cmd_array game_cmds)
struct xform xf = entity_get_xform(ent);
f32 mass = ent->mass_unscaled * xform_get_determinant(xf);
struct v2 acceleration = V2(0, 0);
/* Apply forces and impulses */
struct v2 acceleration = V2(0, 0);
for (struct entity *child = entity_from_handle(store, ent->first); child->valid; child = entity_from_handle(store, child->next)) {
b32 is_force = entity_has_prop(child, ENTITY_PROP_FORCE);
b32 is_impulse = entity_has_prop(child, ENTITY_PROP_IMPULSE);
if (is_force || is_impulse) {
struct v2 force_accel = v2_div(child->force, mass);
if (!is_impulse) {
force_accel = v2_mul(force_accel, dt);
if (is_impulse) {
acceleration = v2_add(acceleration, child->force);
} else {
acceleration = v2_add(acceleration, v2_mul(child->force, dt));
}
acceleration = v2_add(acceleration, force_accel);
entity_enable_prop(child, ENTITY_PROP_RELEASE);
}
}
acceleration = v2_div(acceleration, mass);
/* Verlet integration */
struct v2 tick_velocity = v2_sub(xf.og, ent->verlet_xform.og);