Initial Commit
This commit is contained in:
64 files changed
+13419
No files matched your search
@@ -0,0 +1,68 @@
|
||||
#! Contains how a space steps forward in time, and what rays meet in it.
|
||||
|
||||
import std::containers;
|
||||
import std::math;
|
||||
import std::time;
|
||||
import malunal::geometry;
|
||||
import malunal::linear;
|
||||
module malunal::physics3d;
|
||||
|
||||
#! Steps the space forward in time: finds what's newly near, where it meets,
|
||||
#! and moves its bodies, pushed apart where they meet. Events of what began
|
||||
#! and stopped touching are those of this step alone after.
|
||||
pub def space::step(&mut this, dt: f32): void {
|
||||
this.noted.clear();
|
||||
return if dt <= 0;
|
||||
val began := instant::now();
|
||||
this.find_pairs();
|
||||
val paired := instant::now();
|
||||
this.update_contacts();
|
||||
val met := instant::now();
|
||||
this.solve(dt);
|
||||
val solved := instant::now();
|
||||
if this.sleeping {
|
||||
this.sleep_islands(dt);
|
||||
}
|
||||
val slept := instant::now();
|
||||
this.profile = step_profile{
|
||||
pairs = milliseconds(paired - began), contacts = milliseconds(met - paired),
|
||||
solve = milliseconds(solved - met), sleep = milliseconds(slept - solved), step = milliseconds(slept - began),
|
||||
};
|
||||
}
|
||||
|
||||
def milliseconds(d: duration): f64 {
|
||||
return d.in_nanoseconds as f64 / 1e6;
|
||||
}
|
||||
|
||||
#! Where a ray met a space's collider: which, of which body, where, and of a
|
||||
#! mesh's, which triangle.
|
||||
pub struct space_hit {
|
||||
pub var collider: collider_id;
|
||||
pub var body: body_id;
|
||||
pub var distance: f32;
|
||||
pub var point: vec3;
|
||||
pub var normal: vec3;
|
||||
pub var triangle: u32 = 0xFFFFFFFF;
|
||||
}
|
||||
|
||||
#! Where a ray first meets a collider on a layer of a mask, sensors passed
|
||||
#! over: from a place, along a direction of length 1, as far as `reach`.
|
||||
pub def space::ray_cast(&this, origin: vec3, direction: vec3, reach: f32, mask: u32 = 0xFFFFFFFF): maybe<space_hit> {
|
||||
var best: maybe<space_hit> = nothing{};
|
||||
this.tree.ray_cast(origin, direction, reach, def(leaf: u32, data: u64, so_far: f32): f32 {
|
||||
val c := &this.colliders[data];
|
||||
return so_far if (c.made.layers & mask) == 0 || c.made.sensor;
|
||||
val at := this.bodies[c.body as u64].at;
|
||||
val named := collider_id{ index = data as u32, generation = c.generation };
|
||||
val of := body_id{ index = c.body, generation = this.bodies[c.body as u64].generation };
|
||||
if c.is_mesh() {
|
||||
val met := this.meshes[c.mesh as u64].ray_cast(at, origin, direction, so_far) else { return so_far; };
|
||||
best = space_hit{ collider = named, body = of, distance = met.distance, point = met.point, normal = met.normal, triangle = met.triangle };
|
||||
return met.distance;
|
||||
}
|
||||
val met := c.made.shape.ray_cast(at, origin, direction, so_far) else { return so_far; };
|
||||
best = space_hit{ collider = named, body = of, distance = met.distance, point = met.point, normal = met.normal };
|
||||
return met.distance;
|
||||
});
|
||||
return best;
|
||||
}
|
||||
Reference in new issue
Block a user