rust_3d/
sat_collider.rs

1/*
2Copyright 2019 Martin Buck
3
4Permission is hereby granted, free of charge, to any person obtaining a copy
5of this software and associated documentation files (the "Software"),
6to deal in the Software without restriction, including without limitation the
7rights to use, copy, modify, merge, publish, distribute, sublicense,
8and/or sell copies of the Software, and to permit persons to whom the Software
9is furnished to do so, subject to the following conditions:
10
11The above copyright notice and this permission notice shall
12be included all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
20OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21*/
22
23//! Helper to check for collisions between IsSATObject
24
25use crate::*;
26
27//------------------------------------------------------------------------------
28
29/// Helper to check for collisions between IsSATObject
30pub struct SATCollider {}
31
32//------------------------------------------------------------------------------
33
34impl SATCollider {
35    pub fn collide<A, B>(a: &A, b: &B) -> bool
36    where
37        A: IsSATObject,
38        B: IsSATObject,
39    {
40        let mut all_overlap = true;
41
42        let mut f = |axis: &Norm3D| {
43            if all_overlap {
44                let (mut min_a, mut max_a, mut min_b, mut max_b) = (None, None, None, None);
45                a.for_each_point(&mut |p: &Point3D| {
46                    let x = p.dot(axis);
47                    match min_a {
48                        None => min_a = Some(x),
49                        Some(min) => {
50                            if x < min {
51                                min_a = Some(x)
52                            }
53                        }
54                    }
55                    match max_a {
56                        None => max_a = Some(x),
57                        Some(max) => {
58                            if x > max {
59                                max_a = Some(x)
60                            }
61                        }
62                    }
63                });
64                b.for_each_point(&mut |p: &Point3D| {
65                    let x = p.dot(axis);
66                    match min_b {
67                        None => min_b = Some(x),
68                        Some(min) => {
69                            if x < min {
70                                min_b = Some(x)
71                            }
72                        }
73                    }
74                    match max_b {
75                        None => max_b = Some(x),
76                        Some(max) => {
77                            if x > max {
78                                max_b = Some(x)
79                            }
80                        }
81                    }
82                });
83
84                if let (Some(mina), Some(maxa), Some(minb), Some(maxb)) =
85                    (min_a, max_a, min_b, max_b)
86                {
87                    let not_overlap = maxa < minb || maxb < mina;
88                    if not_overlap {
89                        all_overlap = false
90                    }
91                }
92            }
93        };
94
95        a.for_each_axis(&mut f);
96        b.for_each_axis(&mut f);
97        all_overlap
98    }
99}