117 lines
5.1 KiB
Plaintext
117 lines
5.1 KiB
Plaintext
#! Contains what a space is asked of the shapes in it: which colliders a shape
|
|||
|
|
#! overlaps, and where a shape cast along a motion first meets one, as a
|
||
|
|
#! character's feet and a thrown stone ask.
|
||
|
|
|
||
|
|
import std::containers;
|
||
|
|
import std::math;
|
||
|
|
import malunal::geometry;
|
||
|
|
import malunal::linear;
|
||
|
|
module malunal::physics3d;
|
||
|
|
|
||
|
|
#! Where a shape cast through a space first met one of its colliders: which,
|
||
|
|
#! of which body, how far along the motion, as a fraction of it, where on what
|
||
|
|
#! it met, which way that faces, toward the shape cast, and of a mesh's or a
|
||
|
|
#! heightfield's, which triangle.
|
||
|
|
pub struct space_cast {
|
||
|
|
pub var collider: collider_id;
|
||
|
|
pub var body: body_id;
|
||
|
|
pub var fraction: f32;
|
||
|
|
pub var point: vec3;
|
||
|
|
pub var normal: vec3;
|
||
|
|
pub var triangle: u32 = 0xFFFFFFFF;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! What a query of a space passes over: colliders on no layer of its `mask`;
|
||
|
|
#! sensors, unless `sensors`; and those whose data is `ignore`, where it isn't
|
||
|
|
#! 0, as the asker's own, a body and its colliders given an entity's bits.
|
||
|
|
pub struct query_filter {
|
||
|
|
pub var mask: u32 = 0xFFFFFFFF;
|
||
|
|
pub var sensors := false;
|
||
|
|
pub var ignore: u64 = 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
# Whether a query of a filter asks of a collider.
|
||
|
|
def space::passes(&this, c: u32, filter: &query_filter): bool {
|
||
|
|
val held := &this.colliders[c as u64].made;
|
||
|
|
return (held.layers & filter.mask) != 0 && (!held.sensor || filter.sensors) && (filter.ignore == 0 || held.data != filter.ignore);
|
||
|
|
}
|
||
|
|
|
||
|
|
def space::named(&this, c: u32): collider_id {
|
||
|
|
return collider_id{ index = c, generation = this.colliders[c as u64].generation };
|
||
|
|
}
|
||
|
|
|
||
|
|
def space::body_named(&this, b: u32): body_id {
|
||
|
|
return body_id{ index = b, generation = this.bodies[b as u64].generation };
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Visits each collider on a layer of a mask that a shape overlaps, where a
|
||
|
|
#! pose places it, sensors passed over, as `visit(collider, body)`, until it
|
||
|
|
#! gives false.
|
||
|
|
pub def space::overlap<F>(&this, s: &shape, at: pose, mask: u32, visit: F): void where F: invokable<def(collider_id, body_id): bool> {
|
||
|
|
this.overlap(s, at, query_filter{ mask = mask }, visit);
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Visits each collider a shape overlaps, where a pose places it, that a
|
||
|
|
#! filter asks of, as `visit(collider, body)`, until it gives false.
|
||
|
|
pub def space::overlap<F>(&this, s: &shape, at: pose, filter: query_filter, visit: F): void where F: invokable<def(collider_id, body_id): bool> {
|
||
|
|
this.tree.query(s.bounds(at), def(leaf: u32, data: u64): bool {
|
||
|
|
val c := data as u32;
|
||
|
|
return true if !this.passes(c, &filter);
|
||
|
|
val held := &this.colliders[data];
|
||
|
|
val placed := this.bodies[held.body as u64].at;
|
||
|
|
var met := false;
|
||
|
|
if held.is_mesh() {
|
||
|
|
met = this.meshes[held.mesh as u64].overlaps(placed, s, at);
|
||
|
|
} else {
|
||
|
|
met = overlaps(&held.made.shape, placed, s, at);
|
||
|
|
}
|
||
|
|
return true if !met;
|
||
|
|
return visit(this.named(c), this.body_named(held.body));
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Where a shape, cast along a motion of space from where a pose places it,
|
||
|
|
#! first meets a collider on a layer of a mask, sensors passed over: within a
|
||
|
|
#! quarter of the slop of what it meets, a little short of touching. A
|
||
|
|
#! fraction of 0 where it overlaps something as it begins. Colliders of
|
||
|
|
#! `except`, a body of its own, as a character's, are passed over.
|
||
|
|
pub def space::shape_cast(&this, s: &shape, at: pose, motion: vec3, mask: u32 = 0xFFFFFFFF, except: body_id = body_id{}): maybe<space_cast> {
|
||
|
|
return this.shape_cast(s, at, motion, query_filter{ mask = mask }, except);
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Where a shape, cast along a motion as `shape_cast` casts it, first meets a
|
||
|
|
#! collider a filter asks of.
|
||
|
|
pub def space::shape_cast(&this, s: &shape, at: pose, motion: vec3, filter: query_filter, except: body_id = body_id{}): maybe<space_cast> {
|
||
|
|
var best: maybe<space_cast> = nothing{};
|
||
|
|
var first: f32 = 2;
|
||
|
|
val tolerance := 0.25 * this.slop;
|
||
|
|
val moved := pose{ at = at.at + motion, turn = at.turn };
|
||
|
|
val box := s.bounds(at).merged(s.bounds(moved));
|
||
|
|
this.tree.query(box, def(leaf: u32, data: u64): bool {
|
||
|
|
val c := data as u32;
|
||
|
|
return true if !this.passes(c, &filter) || this.colliders[data].body == except.index;
|
||
|
|
val held := &this.colliders[data];
|
||
|
|
val placed := this.bodies[held.body as u64].at;
|
||
|
|
if held.is_mesh() {
|
||
|
|
val surface := &this.meshes[held.mesh as u64];
|
||
|
|
surface.triangles_in(local_box(box, placed), def(t: u32): bool {
|
||
|
|
val corners := surface.triangle(t);
|
||
|
|
val face := shape::of_hull(hull::triangle(corners[0], corners[1], corners[2]));
|
||
|
|
val hit := shape_cast(&face, placed, s, at, motion, tolerance) else { return true; };
|
||
|
|
if hit.fraction < first {
|
||
|
|
first = hit.fraction;
|
||
|
|
best = space_cast{ collider = this.named(c), body = this.body_named(held.body), fraction = hit.fraction, point = hit.point, normal = hit.normal, triangle = t };
|
||
|
|
}
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
val hit := shape_cast(&held.made.shape, placed, s, at, motion, tolerance) else { return true; };
|
||
|
|
if hit.fraction < first {
|
||
|
|
first = hit.fraction;
|
||
|
|
best = space_cast{ collider = this.named(c), body = this.body_named(held.body), fraction = hit.fraction, point = hit.point, normal = hit.normal };
|
||
|
|
}
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
return best;
|
||
|
|
}
|