Apply force at point

main
Cameron Murphy Reikes 2 years ago
parent d021005a9c
commit 7a03b89b99

@ -38,6 +38,12 @@ MASS :: 1.0;
moment_of_inertia :: (using r: Rect) -> float { moment_of_inertia :: (using r: Rect) -> float {
return MASS*(halfsize.y*halfsize.y + halfsize.x*halfsize.x)/12.0; return MASS*(halfsize.y*halfsize.y + halfsize.x*halfsize.x)/12.0;
} }
apply_force_at_point :: (r: *Rect, force: Vector2, point_world_space: Vector2) {
r.force += force;
offset_from_center_of_mass := point_world_space - r.pos;
r.torque += offset_from_center_of_mass.x * force.y - offset_from_center_of_mass.y * force.x;
}
draw_rect :: (using r: Rect) { draw_rect :: (using r: Rect) {
facing_to_right := rotate(xy(halfsize.x,0.0), angle); facing_to_right := rotate(xy(halfsize.x,0.0), angle);
facing_to_up := rotate(xy(0.0,halfsize.y), angle); facing_to_up := rotate(xy(0.0,halfsize.y), angle);
@ -93,7 +99,7 @@ main :: () {
rects: [..]Rect; rects: [..]Rect;
array_add(*rects, .{pos = #run xy(0.0, 0.0), halfsize = #run xy(0.3)}); array_add(*rects, .{pos = #run xy(0.0, 0.0), halfsize = #run xy(0.3)});
array_add(*rects, .{pos = #run xy(1.5, 0.0), halfsize = #run xy(0.3)}); array_add(*rects, .{pos = #run xy(2.5, 0.0), halfsize = #run xy(0.3)});
quit := false; quit := false;
@ -139,9 +145,19 @@ main :: () {
defer unprocessed_time -= TIMESTEP; defer unprocessed_time -= TIMESTEP;
if get_time() < 0.5
{
apply_force_at_point(*rects[0], xy(3, 0), rects[0].pos + xy(-0.1, 0.03));
}
for * rects
{
defer { it.force = .{}; it.torque = 0.0; };
it.vel += (it.force/MASS) * TIMESTEP;
it.pos += it.vel * TIMESTEP;
it.angle_vel += (it.torque / moment_of_inertia(it)) * TIMESTEP;
rects[0].angle += TIMESTEP * 1.0; it.angle += it.angle_vel * TIMESTEP;
}
} }
} }

Loading…
Cancel
Save