package tiny_libs

  1. Overview
  2. Docs
From-scratch libraries for teaching: graphics, audio, compression, crypto, networking and more

Install

dune-project
 Dependency

Authors

Maintainers

Sources

0.3.6.tar.gz
md5=7c636383d146d30ac6f2fa234a6253c8
sha512=c79f3823c5f8f57e5038eb640d487c61168b84aa07c61999d6622ef9fd0c890e2b03b4c6a7cdbbe9352a49e25dda00ac7bb14693cee8e3d7beeed251351a2af0

doc/src/tiny_libs.physics_2d/Springs.ml.html

Source file Springs.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
(* Claude Code
 *
 * Copyright (C) 2026 Yoann Padioleau
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public License
 * (LGPL) as published by the Free Software Foundation; either version
 * 2 of the License, or (at your option) any later version.
 *)

(* See Springs.mli *)

type spring = { a : int; b : int; rest : float; k : float; damping : float }

let accelerations (bodies : Body.t array) (springs : spring list) : Vec2.t array =
  let acc = Array.make (Array.length bodies) (0., 0.) in
  springs
  |> List.iter (fun s ->
         let a = bodies.(s.a) and b = bodies.(s.b) in
         let d = Vec2.sub b.pos a.pos in
         let length = Vec2.length d in
         if length > 0. then (
           let u = Vec2.scale (1. /. length) d in
           (* the pull on a towards b (on b towards a, the opposite):
            * Hooke's, and the damping of their speed apart *)
           let f = (s.k *. (length -. s.rest)) +. (s.damping *. Vec2.dot (Vec2.sub b.vel a.vel) u) in
           acc.(s.a) <- Vec2.add acc.(s.a) (Vec2.scale (f /. a.mass) u);
           acc.(s.b) <- Vec2.sub acc.(s.b) (Vec2.scale (f /. b.mass) u)));
  acc

let step ~(gravity : Vec2.t) ~(dt : float) (bodies : Body.t array) (springs : spring list) : Body.t array =
  let acc = accelerations bodies springs in
  bodies
  |> Array.mapi (fun i (b : Body.t) ->
         if b.mass = infinity then b
         else
           let vel = Vec2.add b.vel (Vec2.scale dt (Vec2.add acc.(i) gravity)) in
           { b with vel; pos = Vec2.add b.pos (Vec2.scale dt vel) })

let chain ~(from : Vec2.t) ~(towards : Vec2.t) (n : int) ~(k : float) ~(damping : float) : Body.t array * spring list =
  let at i = Vec2.add from (Vec2.scale (float_of_int i /. float_of_int (n - 1)) (Vec2.sub towards from)) in
  let rest = Vec2.length (Vec2.sub towards from) /. float_of_int (n - 1) in
  ( Array.init n (fun i -> if i = 0 then Body.make ~mass:infinity (at i) else Body.make (at i)),
    List.init (n - 1) (fun i -> { a = i; b = i + 1; rest; k; damping }) )