rg3d-physics 0.8.0

Physics library for rg3d-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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
use rg3d_core::{
    define_is_as,
    math::{
        vec3::Vec3,
        self,
        aabb::AxisAlignedBoundingBox
    },
    visitor::{Visit, VisitResult, Visitor, VisitError},
};

#[derive(Clone, Debug)]
pub struct SphereShape {
    pub radius: f32
}

pub trait CircumRadius {
    fn circumradius(&self) -> f32;
}

impl Visit for SphereShape {
    fn visit(&mut self, name: &str, visitor: &mut Visitor) -> VisitResult {
        visitor.enter_region(name)?;

        self.radius.visit("Radius", visitor)?;

        visitor.leave_region()
    }
}

impl CircumRadius for SphereShape {
    fn circumradius(&self) -> f32 {
        self.radius
    }
}

impl Default for SphereShape {
    fn default() -> Self {
        Self {
            radius: 0.5,
        }
    }
}

impl SphereShape {
    pub fn new(radius: f32) -> Self {
        Self {
            radius
        }
    }

    pub fn set_radius(&mut self, radius: f32) {
        self.radius = radius;
    }

    pub fn get_radius(&self) -> f32 {
        self.radius
    }

    pub fn get_farthest_point(&self, direction: Vec3) -> Vec3 {
        let norm_dir = direction.normalized().unwrap_or_else(|| Vec3::new(1.0, 0.0, 0.0));
        norm_dir.scale(self.radius)
    }
}

#[derive(Clone, Debug)]
pub struct TriangleShape {
    pub vertices: [Vec3; 3]
}

impl CircumRadius for TriangleShape {
    fn circumradius(&self) -> f32 {
        AxisAlignedBoundingBox::from_points(&self.vertices).half_extents().max_value()
    }
}

impl Visit for TriangleShape {
    fn visit(&mut self, name: &str, visitor: &mut Visitor) -> VisitResult {
        visitor.enter_region(name)?;

        self.vertices[0].visit("A", visitor)?;
        self.vertices[1].visit("B", visitor)?;
        self.vertices[2].visit("C", visitor)?;

        visitor.leave_region()
    }
}

impl Default for TriangleShape {
    fn default() -> Self {
        Self {
            vertices: [
                Vec3::new(0.0, 0.0, 0.0),
                Vec3::new(1.0, 0.0, 0.0),
                Vec3::new(0.5, 1.0, 0.0)]
        }
    }
}

impl TriangleShape {
    pub fn new(vertices: [Vec3; 3]) -> Self {
        Self {
            vertices
        }
    }

    pub fn get_normal(&self) -> Option<Vec3> {
        (self.vertices[2] - self.vertices[0]).cross(&(self.vertices[1] - self.vertices[0])).normalized()
    }

    pub fn get_farthest_point(&self, direction: Vec3) -> Vec3 {
        math::get_farthest_point(&self.vertices, direction)
    }
}

#[derive(Clone, Debug)]
pub struct BoxShape {
    half_extents: Vec3,
}

impl CircumRadius for BoxShape {
    fn circumradius(&self) -> f32 {
        self.half_extents.max_value()
    }
}

impl Visit for BoxShape {
    fn visit(&mut self, name: &str, visitor: &mut Visitor) -> VisitResult {
        visitor.enter_region(name)?;

        self.half_extents.visit("HalfExtents", visitor)?;

        visitor.leave_region()
    }
}

impl Default for BoxShape {
    fn default() -> Self {
        Self {
            half_extents: Vec3::new(0.5, 0.5, 0.5)
        }
    }
}

impl BoxShape {
    pub fn new(half_extents: Vec3) -> Self {
        Self {
            half_extents
        }
    }

    pub fn get_farthest_point(&self, direction: Vec3) -> Vec3 {
        Vec3 {
            x: if direction.x >= 0.0 { self.half_extents.x } else { -self.half_extents.x },
            y: if direction.y >= 0.0 { self.half_extents.y } else { -self.half_extents.y },
            z: if direction.z >= 0.0 { self.half_extents.z } else { -self.half_extents.z },
        }
    }

    pub fn get_min(&self) -> Vec3 {
        Vec3 {
            x: -self.half_extents.x,
            y: -self.half_extents.y,
            z: -self.half_extents.z,
        }
    }

    pub fn get_max(&self) -> Vec3 {
        Vec3 {
            x: self.half_extents.x,
            y: self.half_extents.y,
            z: self.half_extents.z,
        }
    }
}

#[derive(Clone, Debug)]
pub struct PointCloudShape {
    points: Vec<Vec3>
}

impl CircumRadius for PointCloudShape {
    fn circumradius(&self) -> f32 {
        // TODO: Unoptimal, value should be cached.
        AxisAlignedBoundingBox::from_points(&self.points).half_extents().max_value()
    }
}

impl Visit for PointCloudShape {
    fn visit(&mut self, name: &str, visitor: &mut Visitor) -> VisitResult {
        visitor.enter_region(name)?;

        self.points.visit("Points", visitor)?;

        visitor.leave_region()
    }
}

impl Default for PointCloudShape {
    fn default() -> Self {
        Self {
            points: Vec::new(),
        }
    }
}

impl PointCloudShape {
    pub fn new(points: Vec<Vec3>) -> Self {
        Self {
            points
        }
    }

    pub fn get_farthest_point(&self, direction: Vec3) -> Vec3 {
        math::get_farthest_point(&self.points, direction)
    }
}

#[derive(Copy, Clone, Debug)]
pub enum Axis {
    X = 0,
    Y = 1,
    Z = 2,
}

impl Visit for Axis {
    fn visit(&mut self, name: &str, visitor: &mut Visitor) -> VisitResult {
        let mut id = *self as u8;
        id.visit(name, visitor)?;
        if visitor.is_reading() {
            *self = match id {
                0 => Self::X,
                1 => Self::Y,
                2 => Self::Z,
                _ => return Err(VisitError::User("Invalid axis".to_owned()))
            }
        }
        Ok(())
    }
}

#[derive(Clone, Debug)]
pub struct CapsuleShape {
    axis: Axis,
    radius: f32,
    height: f32,
}

impl CircumRadius for CapsuleShape {
    fn circumradius(&self) -> f32 {
        self.radius.max(self.height)
    }
}

impl Default for CapsuleShape {
    fn default() -> Self {
        Self {
            axis: Axis::X,
            radius: 0.0,
            height: 0.0,
        }
    }
}

impl Visit for CapsuleShape {
    fn visit(&mut self, name: &str, visitor: &mut Visitor) -> VisitResult {
        visitor.enter_region(name)?;

        self.radius.visit("Radius", visitor)?;
        self.axis.visit("Axis", visitor)?;
        self.height.visit("Height", visitor)?;

        visitor.leave_region()
    }
}

impl CapsuleShape {
    pub fn new(radius: f32, height: f32, axis: Axis) -> Self {
        Self {
            axis,
            radius,
            height
        }
    }

    pub fn set_radius(&mut self, radius: f32) {
        self.radius = radius.abs()
    }

    pub fn get_radius(&self) -> f32 {
        self.radius
    }

    pub fn set_height(&mut self, height: f32) {
        self.height = height.abs()
    }

    pub fn get_height(&self) -> f32 {
        self.height
    }

    pub fn set_axis(&mut self, axis: Axis) {
        self.axis = axis;
    }

    pub fn get_axis(&self) -> Axis {
        self.axis
    }

    pub fn get_cap_centers(&self) -> (Vec3, Vec3) {
        let half_height = self.height * 0.5;

        match self.axis {
            Axis::X => {
                (Vec3::new(half_height, 0.0, 0.0),
                 Vec3::new(-half_height, 0.0, 0.0))
            }
            Axis::Y => {
                (Vec3::new(0.0, half_height, 0.0),
                 Vec3::new(0.0, -half_height, 0.0))
            }
            Axis::Z => {
                (Vec3::new(0.0, 0.0, half_height),
                 Vec3::new(0.0, 0.0, -half_height))
            }
        }
    }

    pub fn get_farthest_point(&self, direction: Vec3) -> Vec3 {
        let norm_dir = direction.normalized().unwrap_or_else(|| Vec3::new(1.0, 0.0, 0.0));
        let half_height = self.height * 0.5;

        let positive_cap_position = match self.axis {
            Axis::X => Vec3::new(half_height, 0.0, 0.0),
            Axis::Y => Vec3::new(0.0, half_height, 0.0),
            Axis::Z => Vec3::new(0.0, 0.0, half_height),
        };

        let mut max = -std::f32::MAX;
        let mut farthest = Vec3::ZERO;
        for cap_center in [positive_cap_position, -positive_cap_position].iter() {
            let vertex = *cap_center + norm_dir.scale(self.radius);
            let dot = norm_dir.dot(&vertex);
            if dot > max {
                max = dot;
                farthest = vertex;
            }
        }

        farthest
    }
}

#[derive(Clone, Debug)]
pub enum ConvexShape {
    Dummy,
    Box(BoxShape),
    Sphere(SphereShape),
    Capsule(CapsuleShape),
    Triangle(TriangleShape),
    PointCloud(PointCloudShape),
}

impl CircumRadius for ConvexShape {
    fn circumradius(&self) -> f32 {
        match self {
            Self::Dummy => 0.0,
            Self::Box(box_shape) => box_shape.circumradius(),
            Self::Sphere(sphere) => sphere.circumradius(),
            Self::Capsule(capsule) => capsule.circumradius(),
            Self::Triangle(triangle) => triangle.circumradius(),
            Self::PointCloud(point_cloud) => point_cloud.circumradius(),
        }
    }
}

impl ConvexShape {
    pub fn get_farthest_point(&self, position: Vec3, direction: Vec3) -> Vec3 {
        position + match self {
            Self::Dummy => Vec3::ZERO,
            Self::Box(box_shape) => box_shape.get_farthest_point(direction),
            Self::Sphere(sphere) => sphere.get_farthest_point(direction),
            Self::Capsule(capsule) => capsule.get_farthest_point(direction),
            Self::Triangle(triangle) => triangle.get_farthest_point(direction),
            Self::PointCloud(point_cloud) => point_cloud.get_farthest_point(direction),
        }
    }

    pub fn id(&self) -> i32 {
        match self {
            Self::Dummy => 0,
            Self::Box(_) => 1,
            Self::Sphere(_) => 2,
            Self::Capsule(_) => 3,
            Self::Triangle(_) => 4,
            Self::PointCloud(_) => 5,
        }
    }

    pub fn new(id: i32) -> Result<Self, String> {
        match id {
            0 => Ok(Self::Dummy),
            1 => Ok(Self::Box(Default::default())),
            2 => Ok(Self::Sphere(Default::default())),
            3 => Ok(Self::Capsule(Default::default())),
            4 => Ok(Self::Triangle(Default::default())),
            5 => Ok(Self::PointCloud(Default::default())),
            _ => Err("Invalid shape id!".to_owned())
        }
    }

    define_is_as!(ConvexShape : Box -> ref BoxShape => fn is_box, fn as_box, fn as_box_mut);
    define_is_as!(ConvexShape : Capsule -> ref CapsuleShape => fn is_capsule, fn as_capsule, fn as_capsule_mut);
    define_is_as!(ConvexShape : Sphere -> ref SphereShape => fn is_sphere, fn as_sphere, fn as_sphere_mut);
    define_is_as!(ConvexShape : Triangle -> ref TriangleShape => fn is_triangle, fn as_triangle, fn as_triangle_mut);
    define_is_as!(ConvexShape : PointCloud -> ref PointCloudShape => fn is_point_cloud, fn as_point_cloud, fn as_point_cloud_mut);
}

impl Visit for ConvexShape {
    fn visit(&mut self, name: &str, visitor: &mut Visitor) -> VisitResult {
        match self {
            Self::Dummy => Ok(()),
            Self::Box(box_shape) => box_shape.visit(name, visitor),
            Self::Sphere(sphere) => sphere.visit(name, visitor),
            Self::Capsule(capsule) => capsule.visit(name, visitor),
            Self::Triangle(triangle) => triangle.visit(name, visitor),
            Self::PointCloud(point_cloud) => point_cloud.visit(name, visitor),
        }
    }
}