box3d-rust 0.1.0

Pure Rust port of the Box3D 3D physics engine
Documentation
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
//! Height field ray and shape casts.
//!
//! SPDX-FileCopyrightText: 2026 Erin Catto
//! SPDX-License-Identifier: MIT

use super::triangle::get_height_field_cell_corners;
use super::types::{get_height_field_material_indices, HeightFieldData, HEIGHT_FIELD_HOLE};
use crate::aabb::ray_cast_aabb;
use crate::constants::max_aabb_margin;
use crate::distance::{make_proxy, shape_cast, CastOutput, ShapeCastPairInput};
use crate::geometry::{RayCastInput, ShapeCastInput};
use crate::math_functions::{
    aabb_center, aabb_extents, aabb_overlaps, abs, add, cross, intersect_ray_triangle, make_aabb,
    max, min, mul_add, mul_sv, normalize, sub, Aabb, Transform, Vec3, TRANSFORM_IDENTITY,
    VEC3_ZERO,
};

/// Compute the AABB of a height field. (b3ComputeHeightFieldAABB)
pub fn compute_height_field_aabb(shape: &HeightFieldData, transform: Transform) -> Aabb {
    crate::math_functions::aabb_transform(transform, shape.aabb)
}

/// Ray cast versus a height field in local space. (b3RayCastHeightField)
pub fn ray_cast_height_field(height_field: &HeightFieldData, input: &RayCastInput) -> CastOutput {
    let shape_cast_input = ShapeCastInput {
        proxy: make_proxy(&[input.origin], 0.0),
        translation: input.translation,
        max_fraction: input.max_fraction,
        can_encroach: false,
    };
    shape_cast_height_field(height_field, &shape_cast_input)
}

/// Shape cast versus a height field in local space. (b3ShapeCastHeightField)
pub fn shape_cast_height_field(
    height_field: &HeightFieldData,
    input: &ShapeCastInput,
) -> CastOutput {
    let shape_bounds = make_aabb(
        &input.proxy.points[..input.proxy.count as usize],
        input.proxy.radius,
    );
    let shape_translation = input.translation;
    let scale = height_field.scale;

    let shape_start = aabb_center(shape_bounds);
    let shape_delta = mul_sv(input.max_fraction, shape_translation);
    let shape_end = add(shape_start, shape_delta);

    let mut result = CastOutput::default();

    let shape_extents = aabb_extents(shape_bounds);
    let margin = max_aabb_margin();
    let margin_v = Vec3 {
        x: margin,
        y: margin,
        z: margin,
    };
    let combined_bounds = Aabb {
        lower_bound: sub(sub(height_field.aabb.lower_bound, shape_extents), margin_v),
        upper_bound: add(add(height_field.aabb.upper_bound, shape_extents), margin_v),
    };

    let mut min_fraction = 0.0;
    let mut max_fraction = 0.0;
    let intersects = ray_cast_aabb(
        combined_bounds,
        shape_start,
        shape_end,
        &mut min_fraction,
        &mut max_fraction,
    );
    if !intersects {
        return result;
    }

    // These are for walking the grid, not the triangle cast.
    let mut clamped_start = mul_add(shape_start, min_fraction, shape_delta);
    let clamped_delta = mul_sv(max_fraction - min_fraction, shape_delta);
    let mut clamped_end = add(clamped_start, clamped_delta);

    // Preserve the un-shifted center sweep for the swept-volume AABB cull.
    let center_start = clamped_start;
    let center_end = clamped_end;

    // The grid traversal starts from the leading shape bounds corner
    let sign_x;
    if shape_translation.x >= 0.0 {
        clamped_start.x += shape_extents.x;
        sign_x = 1.0;
    } else {
        clamped_start.x -= shape_extents.x;
        sign_x = -1.0;
    }

    let sign_z;
    if shape_translation.z >= 0.0 {
        clamped_start.z += shape_extents.z;
        sign_z = 1.0;
    } else {
        clamped_start.z -= shape_extents.z;
        sign_z = -1.0;
    }

    clamped_end = add(clamped_start, clamped_delta);

    let column_start = (clamped_start.x / scale.x).floor() as i32;
    let column_end = (clamped_end.x / scale.x).floor() as i32;
    let row_start = (clamped_start.z / scale.z).floor() as i32;
    let row_end = (clamped_end.z / scale.z).floor() as i32;

    let abs_clamped_delta = abs(clamped_delta);

    let delta_alpha_x;
    let mut next_fraction_x;
    let delta_column;

    if column_start < column_end {
        debug_assert!(abs_clamped_delta.x > 0.0);
        delta_alpha_x = scale.x / abs_clamped_delta.x;
        next_fraction_x =
            (scale.x * ((column_start + 1) as f32) - clamped_start.x) / abs_clamped_delta.x;
        delta_column = 1;
    } else if column_end < column_start {
        debug_assert!(abs_clamped_delta.x > 0.0);
        delta_alpha_x = scale.x / abs_clamped_delta.x;
        next_fraction_x = (clamped_start.x - scale.x * (column_start as f32)) / abs_clamped_delta.x;
        delta_column = -1;
    } else {
        delta_alpha_x = 0.0;
        next_fraction_x = f32::MAX;
        delta_column = 0;
    }

    let delta_alpha_z;
    let mut next_fraction_z;
    let delta_row;

    if row_start < row_end {
        debug_assert!(abs_clamped_delta.z > 0.0);
        delta_alpha_z = scale.z / abs_clamped_delta.z;
        next_fraction_z =
            (scale.z * ((row_start + 1) as f32) - clamped_start.z) / abs_clamped_delta.z;
        delta_row = 1;
    } else if row_end < row_start {
        debug_assert!(abs_clamped_delta.z > 0.0);
        delta_alpha_z = scale.z / abs_clamped_delta.z;
        next_fraction_z = (clamped_start.z - scale.z * (row_start as f32)) / abs_clamped_delta.z;
        delta_row = -1;
    } else {
        delta_alpha_z = 0.0;
        next_fraction_z = f32::MAX;
        delta_row = 0;
    }

    let mut box_column_head = column_start;
    let mut box_row_head = row_start;

    let mut box_column_tail =
        ((clamped_start.x - 2.0 * sign_x * shape_extents.x) / scale.x).floor() as i32;
    let mut box_row_tail =
        ((clamped_start.z - 2.0 * sign_z * shape_extents.z) / scale.z).floor() as i32;

    let mut best_fraction = input.max_fraction;

    let grid_fraction_scale = input.max_fraction * (max_fraction - min_fraction);
    let grid_fraction_offset = input.max_fraction * min_fraction;

    let row_count = height_field.row_count;
    let column_count = height_field.column_count;

    let mut pair_input = ShapeCastPairInput {
        proxy_a: Default::default(),
        proxy_b: input.proxy,
        transform: TRANSFORM_IDENTITY,
        translation_b: input.translation,
        max_fraction: 0.0,
        can_encroach: input.can_encroach,
    };

    let cast_bounds = Aabb {
        lower_bound: sub(min(center_start, center_end), shape_extents),
        upper_bound: add(max(center_start, center_end), shape_extents),
    };

    let ray_origin = shape_start;
    let ray_translation = shape_translation;

    loop {
        let (column1, column2) = if box_column_tail < box_column_head {
            (box_column_tail, box_column_head)
        } else {
            (box_column_head, box_column_tail)
        };

        let (row1, row2) = if box_row_tail < box_row_head {
            (box_row_tail, box_row_head)
        } else {
            (box_row_head, box_row_tail)
        };

        for row in row1..=row2 {
            if row < 0 || row_count - 1 <= row {
                continue;
            }

            for column in column1..=column2 {
                if column < 0 || column_count - 1 <= column {
                    continue;
                }

                let cell_index = (row * (column_count - 1) + column) as usize;
                let material_index = get_height_field_material_indices(height_field)[cell_index];
                if material_index == HEIGHT_FIELD_HOLE {
                    continue;
                }

                let corners = get_height_field_cell_corners(height_field, row, column);
                let point11 = corners[0];
                let point12 = corners[1];
                let point21 = corners[2];
                let point22 = corners[3];

                let bounds = Aabb {
                    lower_bound: min(min(point11, point12), min(point21, point22)),
                    upper_bound: max(max(point11, point12), max(point21, point22)),
                };

                if !aabb_overlaps(cast_bounds, bounds) {
                    continue;
                }

                let quad_index = row * (column_count - 1) + column;
                let triangle_index1 = 2 * quad_index;
                let triangle_index2 = triangle_index1 + 1;

                if input.proxy.count == 1 && input.proxy.radius == 0.0 {
                    // Ray cast
                    {
                        let vertex1 = point11;
                        let (vertex2, vertex3) = if height_field.clockwise {
                            (point12, point21)
                        } else {
                            (point21, point12)
                        };

                        let alpha = intersect_ray_triangle(
                            ray_origin,
                            ray_translation,
                            vertex1,
                            vertex2,
                            vertex3,
                        );
                        debug_assert!((0.0..=1.0).contains(&alpha));

                        if alpha < best_fraction {
                            let edge1 = sub(point21, point11);
                            let edge2 = sub(point12, point11);
                            let normal = if height_field.clockwise {
                                cross(edge2, edge1)
                            } else {
                                cross(edge1, edge2)
                            };

                            result.point = mul_add(shape_start, alpha, shape_translation);
                            result.normal = normalize(normal);
                            result.fraction = alpha;
                            result.triangle_index = triangle_index1;
                            result.material_index = material_index as i32;
                            result.hit = true;
                            best_fraction = alpha;
                        }
                    }

                    {
                        let vertex1 = point22;
                        let (vertex2, vertex3) = if height_field.clockwise {
                            (point21, point12)
                        } else {
                            (point12, point21)
                        };

                        let alpha = intersect_ray_triangle(
                            ray_origin,
                            ray_translation,
                            vertex1,
                            vertex2,
                            vertex3,
                        );
                        debug_assert!((0.0..=1.0).contains(&alpha));

                        if alpha < best_fraction {
                            let edge1 = sub(point22, point21);
                            let edge2 = sub(point12, point21);
                            let normal = if height_field.clockwise {
                                cross(edge2, edge1)
                            } else {
                                cross(edge1, edge2)
                            };

                            result.point = mul_add(shape_start, alpha, shape_translation);
                            result.normal = normalize(normal);
                            result.fraction = alpha;
                            result.triangle_index = triangle_index2;
                            result.material_index = material_index as i32;
                            result.hit = true;
                            best_fraction = alpha;
                        }
                    }
                } else {
                    // Shape cast
                    {
                        let origin = point11;
                        let triangle_vertices =
                            [VEC3_ZERO, sub(point21, origin), sub(point12, origin)];
                        pair_input.proxy_a = make_proxy(&triangle_vertices, 0.0);
                        pair_input.max_fraction = best_fraction;
                        pair_input.transform = Transform {
                            q: TRANSFORM_IDENTITY.q,
                            p: crate::math_functions::neg(origin),
                        };

                        let pair_output = shape_cast(&pair_input);
                        if pair_output.hit {
                            best_fraction = pair_output.fraction;
                            result = pair_output;
                            result.point = add(result.point, origin);
                            result.triangle_index = triangle_index1;
                            result.material_index = material_index as i32;
                        }
                    }

                    {
                        let origin = point21;
                        let triangle_vertices =
                            [VEC3_ZERO, sub(point22, origin), sub(point12, origin)];
                        pair_input.proxy_a = make_proxy(&triangle_vertices, 0.0);
                        pair_input.max_fraction = best_fraction;
                        pair_input.transform = Transform {
                            q: TRANSFORM_IDENTITY.q,
                            p: crate::math_functions::neg(origin),
                        };

                        let pair_output = shape_cast(&pair_input);
                        if pair_output.hit {
                            best_fraction = pair_output.fraction;
                            result = pair_output;
                            result.point = add(result.point, origin);
                            result.triangle_index = triangle_index2;
                            result.material_index = material_index as i32;
                        }
                    }
                }
            }
        }

        let input_fraction_x = if next_fraction_x == f32::MAX {
            f32::MAX
        } else {
            grid_fraction_offset + next_fraction_x * grid_fraction_scale
        };
        let input_fraction_z = if next_fraction_z == f32::MAX {
            f32::MAX
        } else {
            grid_fraction_offset + next_fraction_z * grid_fraction_scale
        };
        if input_fraction_x > best_fraction && input_fraction_z > best_fraction {
            break;
        }

        if next_fraction_x <= next_fraction_z {
            if box_column_head == column_end {
                break;
            }

            box_column_head += delta_column;
            box_column_tail = box_column_head;

            if shape_extents.z == 0.0 {
                box_row_tail = box_row_head;
            } else {
                let row_intercept = clamped_start.z + next_fraction_x * clamped_delta.z;
                box_row_tail =
                    ((row_intercept - 2.0 * sign_z * shape_extents.z) / scale.z).floor() as i32;
            }

            next_fraction_x += delta_alpha_x;
        } else {
            if box_row_head == row_end {
                break;
            }

            box_row_head += delta_row;
            box_row_tail = box_row_head;

            if shape_extents.x == 0.0 {
                box_column_tail = box_column_head;
            } else {
                let column_intercept = clamped_start.x + next_fraction_z * clamped_delta.x;
                box_column_tail =
                    ((column_intercept - 2.0 * sign_x * shape_extents.x) / scale.x).floor() as i32;
            }

            next_fraction_z += delta_alpha_z;
        }
    }

    result
}