338 lines
7.8 KiB
Odin

package collision
import "core:math"
import lg "core:math/linalg"
import "game:halfedge"
import rl "vendor:raylib"
import "vendor:raylib/rlgl"
_ :: math
Convex :: distinct halfedge.Half_Edge_Mesh
BOX_CORNERS_NORM :: [8]Vec3 {
{-1, 1, 1},
{-1, -1, 1},
{-1, 1, -1},
{-1, -1, -1},
{1, 1, 1},
{1, -1, 1},
{1, 1, -1},
{1, -1, -1},
}
box_indices := [6 * 4]u16{0, 4, 6, 2, 3, 2, 6, 7, 7, 6, 4, 5, 5, 1, 3, 7, 1, 0, 2, 3, 5, 4, 0, 1}
box_to_convex :: proc(box: Box, allocator := context.allocator) -> (convex: Convex) {
vertices := make([]Vec3, 8, context.temp_allocator)
for corner, i in BOX_CORNERS_NORM {
vertices[i] = box.pos + corner * box.rad
}
convex = Convex(halfedge.mesh_from_vertex_index_list(vertices, box_indices[:], 4, allocator))
return
}
Contact_Manifold :: struct {
points: []Vec3,
normal: Vec3,
}
convex_vs_convex_sat :: proc(a, b: Convex) -> (manifold: Contact_Manifold, collision: bool) {
face_query_a := query_separation_face_directions(a, b)
if face_query_a.separation > 0 {
return
}
face_query_b := query_separation_face_directions(b, a)
if face_query_b.separation > 0 {
return
}
edge_separation, edge_a, edge_b := query_separation_edges(a, b)
_, _ = edge_a, edge_b
if edge_separation > 0 {
return
}
edge_separation = -1
is_face_a_contact := face_query_a.separation > edge_separation
is_face_b_contact := face_query_b.separation > edge_separation
collision = true
if is_face_a_contact && is_face_b_contact {
manifold = create_face_contact_manifold(face_query_a, a, face_query_b, b)
} else {
}
return
}
Face_Query :: struct {
separation: f32,
face: halfedge.Face_Index,
}
query_separation_face_directions :: proc(a, b: Convex) -> (result: Face_Query) {
result.separation = min(f32)
for face, f in a.faces {
index := a.edges[face.edge].origin
pos := a.vertices[index].pos
normal := face.normal
support_point := find_support_point(b, -normal)
plane := plane_from_point_normal(pos, normal)
distance := signed_distance_plane(support_point, plane)
if distance > result.separation {
result.face = halfedge.Face_Index(f)
result.separation = distance
}
}
return
}
find_support_point :: proc(convex: Convex, normal: Vec3) -> Vec3 {
p: Vec3
max_proj := min(f32)
for vert in convex.vertices {
proj := lg.dot(vert.pos, normal)
if proj > max_proj {
max_proj = proj
p = vert.pos
}
}
return p
}
query_separation_edges :: proc(
a, b: Convex,
) -> (
separation: f32,
a_edge: halfedge.Edge_Index,
b_edge: halfedge.Edge_Index,
) {
separation = min(f32)
a_edge = -1
b_edge = -1
for edge_a, edge_a_idx in a.edges {
for edge_b, edge_b_idx in b.edges {
edge_a_dir := halfedge.get_edge_direction_normalized(
halfedge.Half_Edge_Mesh(a),
edge_a,
)
edge_b_dir := halfedge.get_edge_direction_normalized(
halfedge.Half_Edge_Mesh(b),
edge_b,
)
axis := lg.cross(edge_a_dir, edge_b_dir)
edge_a_origin := a.vertices[edge_a.origin].pos
if lg.dot(axis, edge_a_origin - a.center) < 0 {
axis = -axis
}
plane_a := plane_from_point_normal(edge_a_origin, axis)
vert_b := find_support_point(b, -plane_a.normal)
distance := signed_distance_plane(vert_b, plane_a)
if distance > separation {
separation = distance
a_edge = halfedge.Edge_Index(edge_a_idx)
b_edge = halfedge.Edge_Index(edge_b_idx)
}
}
}
return
}
create_face_contact_manifold :: proc(
face_query_a: Face_Query,
a: Convex,
face_query_b: Face_Query,
b: Convex,
) -> (
manifold: Contact_Manifold,
) {
is_ref_a := face_query_a.separation > face_query_b.separation
ref_face_query := is_ref_a ? face_query_a : face_query_b
ref_convex := is_ref_a ? a : b
inc_convex := is_ref_a ? b : a
ref_face := a.faces[ref_face_query.face]
// incident face
inc_face: halfedge.Face
inc_face_idx: halfedge.Face_Index
// Find the most anti parallel face
{
min_dot := f32(1.0)
for face, face_idx in inc_convex.faces {
dot := lg.dot(ref_face.normal, face.normal)
if dot < min_dot {
min_dot = dot
inc_face = face
inc_face_idx = halfedge.Face_Index(face_idx)
}
}
}
inc_polygon: []Vec3
// Get incident face vertices
{
it := halfedge.iterator_face_edges(halfedge.Half_Edge_Mesh(inc_convex), inc_face_idx)
vert_count := 0
for _ in halfedge.iterate_next_edge(&it) {
vert_count += 1
}
inc_polygon = make([]Vec3, vert_count, context.temp_allocator)
halfedge.iterator_reset_edges(&it)
i := 0
for edge in halfedge.iterate_next_edge(&it) {
inc_polygon[i] = inc_convex.vertices[edge.origin].pos
i += 1
}
}
assert(len(inc_polygon) > 0)
// Set up ping pong buffers
inc_polygon2 := make([]Vec3, len(inc_polygon), context.temp_allocator)
// Buffer 0 is the original incident polygon, start with index 1
clip_buf_idx := 1
clip_bufs := [2][]Vec3{inc_polygon, inc_polygon2}
get_other_clip_buf :: proc(idx: int) -> int {
return (idx + 1) % 2
}
step := 0
{
it := halfedge.iterator_face_edges(
halfedge.Half_Edge_Mesh(ref_convex),
ref_face_query.face,
)
for edge in halfedge.iterate_next_edge(&it) {
src_polygon := clip_bufs[get_other_clip_buf(clip_buf_idx)]
clipped_polygon := clip_bufs[clip_buf_idx]
clipping_face, clipping_face_idx, _ := halfedge.get_adjacent_face(
halfedge.Half_Edge_Mesh(ref_convex),
edge,
)
clipping_face_vert :=
ref_convex.vertices[ref_convex.edges[clipping_face.edge].origin].pos
clipping_plane_center: Vec3
{
clipping_plane_it := halfedge.iterator_face_edges(
halfedge.Half_Edge_Mesh(ref_convex),
clipping_face_idx,
)
num := 0
for clip_edge in halfedge.iterate_next_edge(&clipping_plane_it) {
vert := ref_convex.vertices[clip_edge.origin].pos
clipping_plane_center += vert
num += 1
}
clipping_plane_center /= f32(num)
}
clipping_plane := plane_from_point_normal(clipping_face_vert, clipping_face.normal)
// Actual clipping
{
j := 0
for i in 0 ..< len(src_polygon) {
k := (i + 1) % len(src_polygon)
d1 := signed_distance_plane(src_polygon[i], clipping_plane)
d2 := signed_distance_plane(src_polygon[k], clipping_plane)
if d1 < 0 && d2 < 0 {
// Both points inside
clipped_polygon[j] = src_polygon[k]
j += 1
} else if d1 >= 0 && d2 < 0 {
// First point is outside
_, clipped_polygon[j], _ = intersect_segment_plane(
{src_polygon[i], src_polygon[k]},
clipping_plane,
)
j += 1
clipped_polygon[j] = src_polygon[k]
j += 1
} else if d1 < 0 && d2 >= 0 {
// Second point outside
_, clipped_polygon[j], _ = intersect_segment_plane(
{src_polygon[i], src_polygon[k]},
clipping_plane,
)
j += 1
}
}
clipped_polygon = clipped_polygon[:j]
if step == 3 {
for p in clipped_polygon {
rl.DrawSphereWires(p, 0.05, 4, 4, rl.GREEN)
}
{
rl.BeginBlendMode(.ALPHA)
defer rl.EndBlendMode()
rlgl.PushMatrix()
defer rlgl.PopMatrix()
f := clipping_face.normal
r := lg.normalize0(lg.cross(f, rl.Vector3{0, 1, 0}))
u := lg.cross(r, f)
// ps: [4]Vec3 = {{-1, -1, 0}, {1, -1, 0}, {1, 1, 0}, {-1, 1, 0}}
rl.DrawLine3D(clipping_plane_center, clipping_plane_center + f, rl.BLUE)
rl.DrawLine3D(clipping_plane_center, clipping_plane_center + r, rl.RED)
rl.DrawLine3D(clipping_plane_center, clipping_plane_center + u, rl.GREEN)
// mat: rlgl.Matrix = auto_cast lg.matrix4_look_at_from_fru(0, f, r, u, false)
// rlgl.LoadIdentity()
// rlgl.MultMatrixf(cast([^]f32)&mat)
// col := rl.Color{0, 228, 48, 50}
// rl.DrawTriangle3D(ps[0], ps[1], ps[2], col)
// rl.DrawTriangle3D(ps[2], ps[3], ps[0], col)
}
}
}
clip_bufs[clip_buf_idx] = clipped_polygon
clip_buf_idx = get_other_clip_buf(clip_buf_idx)
step += 1
}
}
manifold.normal = ref_face.normal
manifold.points = clip_bufs[get_other_clip_buf(clip_buf_idx)]
return
}