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
type t = Vec2.t -> Vec2.t -> Vec2.t
let none : t = fun _pos _vel -> (0., 0.)
let uniform (g : Vec2.t) : t = fun _pos _vel -> g
let gravitation ~(gm : float) ~(center : Vec2.t) : t =
fun pos _vel ->
let d = Vec2.sub center pos in
let r = Vec2.length d in
Vec2.scale (gm /. (r *. r *. r)) d
let spring ~(k_over_m : float) ~(anchor : Vec2.t) : t =
fun pos _vel -> Vec2.scale (-.k_over_m) (Vec2.sub pos anchor)
let drag ~(c : float) : t = fun _pos vel -> Vec2.scale (-.c) vel
let sum (forces : t list) : t =
fun pos vel -> List.fold_left (fun acc (f : t) -> Vec2.add acc (f pos vel)) (0., 0.) forces