aboutsummaryrefslogtreecommitdiff
path: root/src/phys/body.odin
blob: 4793e421ec65ff25e09aeee799f99c21204aa61a (plain)
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package phys

import "core:math"
import "core:math/linalg"
import "core:log"

Vec2 :: [2]f32

Rect :: struct {
  start: Vec2,
  size: Vec2,
}

Layer :: enum (u16) {
  Hard, // hard collisions; don't let bodies intersect at all. default
  Soft, // soft collisions; push away other bodies with a force
  Enemy, // enemy hitboxes
  Player, // player hitboxes
  Enemy_Projectile,
  Player_Projectile,
}

Layer_Set :: bit_set[Layer;u16]

Collision_Type :: enum (u8) {
  Up,
  Down,
  Right,
  Left,
  Horizontal,
  Vertical,
}

Raycast :: struct {
  start: Vec2,
  dir: Vec2,
  mask: Layer_Set,
}

Body :: struct {
  handle: Body_Handle,
  bin_idx: i32,
  rect: Rect,
  active: bool,
  pos: Vec2,
  vel: Vec2,
  collisions: bit_set[Collision_Type;u8],
  layers: Layer_Set,
  mask: Layer_Set,
}

make_body :: proc(
  rect: Rect,
  layers := Layer_Set{.Hard},
  mask := Layer_Set{.Hard},
) -> Body_Handle {
  b := Body {
    rect   = rect,
    layers = layers,
    mask   = mask,
    active = true,
  }
  return add_body(b)
}

@(require_results)
make_raycast :: #force_inline proc(
  start: Vec2,
  end: Vec2,
  mask := Layer_Set{.Hard},
) -> Raycast {
  return Raycast{
    start,
    end,
    mask,
  }
}

@(require_results)
aabb_hori :: proc(a: Rect, b: Rect) -> bool {
  return a.start.x < b.start.x + b.size.x && b.start.x < a.start.x + a.size.x
}

@(require_results)
aabb_vert :: proc(a: Rect, b: Rect) -> bool {
  return a.start.y < b.start.y + b.size.y && b.start.y < a.start.y + a.size.y
}

@(require_results)
aabb :: proc(a: Rect, b: Rect) -> bool {
  return aabb_hori(a, b) && aabb_vert(a, b)
}

@(require_results)
point_aabb_hori :: proc(r: Rect, p: Vec2) -> bool {
  return r.start.x < p.x && r.start.x + r.size.x > p.x
}

@(require_results)
point_aabb_vert :: proc(r: Rect, p: Vec2) -> bool {
  return r.start.y < p.y && r.start.y + r.size.y > p.y
}

@(require_results)
point_aabb :: proc(r: Rect, p: Vec2) -> bool {
  return point_aabb_hori(r, p) && point_aabb_vert(r, p)
}

@(require_results)
raycast_to_aabb :: proc(rc: Raycast, body: Body) -> bool {
  body_min := body.pos + body.rect.start
  body_max := body_min + body.rect.size

  rc_dir_to_body := linalg.normalize0((body_min + body_max) * 0.5 - rc.start)

  // Don't consider bodies behind the ray
  if linalg.dot(rc_dir_to_body, rc.dir) < 0 {
    return false;
  }

  tmin := -math.INF_F32
  tmax := math.INF_F32

  dir_inv := 1.0 / rc.dir

  tx_min := (body_min.x - rc.start.x) * dir_inv.x
  tx_max := (body_max.x - rc.start.x) * dir_inv.x
  
  tmin = max(tmin, min(tx_max, tx_min))
  tmax = min(tmax, max(tx_max, tx_min))

  ty_min := (body_min.y - rc.start.y) * dir_inv.y
  ty_max := (body_max.y - rc.start.y) * dir_inv.y
  
  tmin = max(tmin, min(ty_max, ty_min))
  tmax = min(tmax, max(ty_max, ty_min))

  return tmax >= tmin
}