Skip to main content

arcane_engine/physics/
integrate.rs

1use super::types::{BodyType, RigidBody};
2
3/// Semi-implicit Euler integration.
4/// Applies gravity, updates velocity from forces, updates position from velocity.
5pub fn integrate(body: &mut RigidBody, gravity_x: f32, gravity_y: f32, dt: f32) {
6    if body.body_type == BodyType::Static || body.sleeping {
7        body.fx = 0.0;
8        body.fy = 0.0;
9        body.torque = 0.0;
10        return;
11    }
12
13    // Apply gravity to dynamic bodies (not kinematic)
14    if body.body_type == BodyType::Dynamic {
15        body.fx += gravity_x * body.mass;
16        body.fy += gravity_y * body.mass;
17    }
18
19    // Update velocity from forces
20    body.vx += body.fx * body.inv_mass * dt;
21    body.vy += body.fy * body.inv_mass * dt;
22    body.angular_velocity += body.torque * body.inv_inertia * dt;
23
24    // Update position from velocity
25    body.x += body.vx * dt;
26    body.y += body.vy * dt;
27    body.angle += body.angular_velocity * dt;
28
29    // Clear accumulated forces
30    body.fx = 0.0;
31    body.fy = 0.0;
32    body.torque = 0.0;
33}