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
45
46
47
48
49
50
51
52
53
54
55
56
let gap (p : Vec3.t) (h : Hitbox3d.placed) : float =
match h.shape with
| Hitbox3d.Sphere r -> Float.max 0. (Vec3.length (Vec3.sub p h.pos) -. r)
| Hitbox3d.Box _ -> Vec3.length (Vec3.sub p (Collide3d.closest_on_box h p))
| Hitbox3d.Capsule (_, r) -> Float.max 0. (Vec3.length (Vec3.sub p (Collide3d.closest_on_segment (Hitbox3d.segment h) p)) -. r)
| Hitbox3d.Plane (n, d) -> Float.max 0. (Vec3.dot n p -. d)
let reach (h : Hitbox3d.placed) : float =
match h.shape with
| Hitbox3d.Sphere r -> r
| Hitbox3d.Box half -> Vec3.length half
| Hitbox3d.Capsule (half, r) -> half +. r
| Hitbox3d.Plane _ -> infinity
let touching = 1e-4
let overlap = 1e-3
let sphere ~(radius : float) ~(from : Vec3.t) ~(motion : Vec3.t) ?(moving = ((0., 0., 0.), (0., 0., 0.)))
(h : Hitbox3d.placed) : float option =
let shift, turn = moving in
let at (t : float) : Hitbox3d.placed =
{ h with pos = Vec3.add h.pos (Vec3.scale t shift); orientation = Quat.turned_by ~spin:turn ~dt:t h.orientation }
in
let clearance t = gap (Vec3.add from (Vec3.scale t motion)) (at t) -. radius in
let closing =
Vec3.length (Vec3.sub motion shift) +. if Vec3.length turn = 0. then 0. else Vec3.length turn *. reach h
in
if clearance 0. <= touching || closing = 0. || Float.is_nan closing || closing = infinity then None
else
let rec advance t n =
let c = clearance t in
if c <= -.overlap +. 1e-9 then Some t
else if n = 0 then Some t
else
let t = t +. ((c +. overlap) /. closing) in
if t > 1. then None else advance t (n - 1)
in
advance 0. 64