1
- use crate :: utils:: Point2D ;
1
+ use crate :: utils:: { Point2D , min_of_f32_vec , max_of_f32_vec } ;
2
2
use amethyst:: core:: ecs:: { Component , DenseVecStorage } ;
3
3
use geo:: { Polygon , LineString } ;
4
4
use geo:: intersects:: Intersects ;
5
+ use std:: cmp:: Ordering ;
5
6
6
7
pub struct ButtonPlatform ;
7
8
@@ -29,23 +30,42 @@ impl Component for Transparent {
29
30
30
31
#[ derive( Debug ) ]
31
32
pub struct Colliders {
33
+ pub min_x : f32 ,
34
+ pub min_y : f32 ,
35
+ pub max_x : f32 ,
36
+ pub max_y : f32 ,
32
37
colliders : Vec < Collider > ,
33
- polygons : Vec < Polygon < f32 > >
38
+ polygons : Vec < Polygon < f32 > > ,
34
39
}
35
40
36
41
impl Colliders {
37
42
pub fn from_vec ( colliders : Vec < Collider > ) -> Colliders {
38
- Colliders { polygons : colliders. iter ( ) . map ( |collider| collider. to_polygon ( ) ) . collect ( ) , colliders }
39
- }
40
- pub fn from_points ( a : Point2D , b : Point2D , c : Point2D , d : Point2D ) -> Colliders {
41
- Colliders { polygons : vec ! [
42
- Polygon :: new(
43
- LineString :: from(
44
- vec![ ( a. x, a. y) , ( b. x, b. y) , ( c. x, c. y) , ( d. x, d. y) , ( a. x, a. y) ]
45
- ) ,
46
- vec![ ] ,
43
+ let ( min_x, min_y, max_x, max_y) = {
44
+ (
45
+ colliders. iter ( ) . map ( |col| min_of_f32_vec ( [ col. a . x , col. b . x , col. c . x , col. d . x ] ) ) . min_by ( |a, b| a. partial_cmp ( b) . unwrap_or ( Ordering :: Equal ) ) . unwrap ( ) ,
46
+ colliders. iter ( ) . map ( |col| min_of_f32_vec ( [ col. a . y , col. b . y , col. c . y , col. d . y ] ) ) . min_by ( |a, b| a. partial_cmp ( b) . unwrap_or ( Ordering :: Equal ) ) . unwrap ( ) ,
47
+ colliders. iter ( ) . map ( |col| max_of_f32_vec ( [ col. a . x , col. b . x , col. c . x , col. d . x ] ) ) . max_by ( |a, b| a. partial_cmp ( b) . unwrap_or ( Ordering :: Equal ) ) . unwrap ( ) ,
48
+ colliders. iter ( ) . map ( |col| max_of_f32_vec ( [ col. a . y , col. b . y , col. c . y , col. d . y ] ) ) . max_by ( |a, b| a. partial_cmp ( b) . unwrap_or ( Ordering :: Equal ) ) . unwrap ( )
47
49
)
48
- ] , colliders : Vec :: new ( ) }
50
+ } ;
51
+ Colliders { polygons : colliders. iter ( ) . map ( |collider| collider. to_polygon ( ) ) . collect ( ) , colliders, min_x, min_y, max_x, max_y }
52
+ }
53
+ pub fn from_points ( a : Point2D , b : Point2D , c : Point2D , d : Point2D ) -> Colliders {
54
+ Colliders {
55
+ polygons : vec ! [
56
+ Polygon :: new(
57
+ LineString :: from(
58
+ vec![ ( a. x, a. y) , ( b. x, b. y) , ( c. x, c. y) , ( d. x, d. y) , ( a. x, a. y) ]
59
+ ) ,
60
+ vec![ ] ,
61
+ )
62
+ ] ,
63
+ colliders : Vec :: new ( ) ,
64
+ min_x : min_of_f32_vec ( [ a. x , b. x , c. x , d. x ] ) ,
65
+ min_y : min_of_f32_vec ( [ a. y , b. y , c. y , d. y ] ) ,
66
+ max_x : max_of_f32_vec ( [ a. x , b. x , c. x , d. x ] ) ,
67
+ max_y : max_of_f32_vec ( [ a. y , b. y , c. y , d. y ] ) ,
68
+ }
49
69
}
50
70
51
71
pub fn polygons ( & self ) -> & Vec < Polygon < f32 > > {
@@ -63,10 +83,10 @@ impl Component for Colliders {
63
83
64
84
#[ derive( Debug ) ]
65
85
pub struct Collider {
66
- a : Point2D ,
67
- b : Point2D ,
68
- c : Point2D ,
69
- d : Point2D ,
86
+ pub a : Point2D ,
87
+ pub b : Point2D ,
88
+ pub c : Point2D ,
89
+ pub d : Point2D ,
70
90
}
71
91
72
92
impl Collider {
@@ -97,7 +117,6 @@ impl Collider {
97
117
}
98
118
99
119
pub fn are_colliding ( ship_polygon : & Vec < Polygon < f32 > > , struct_polygons : & Vec < Polygon < f32 > > ) -> bool {
100
-
101
120
for polygon in ship_polygon. iter ( ) {
102
121
for struct_polygon in struct_polygons. iter ( ) {
103
122
if polygon. intersects ( struct_polygon) {
@@ -106,4 +125,11 @@ pub fn are_colliding(ship_polygon: &Vec<Polygon<f32>>, struct_polygons: &Vec<Pol
106
125
}
107
126
}
108
127
false
128
+ }
129
+
130
+ pub fn compute_is_eligible_for_collision ( col1 : & Colliders , col2 : & Colliders ) -> bool {
131
+ !( col1. min_x < col2. min_x && col1. max_x < col2. min_x && col1. min_x < col2. max_x && col1. max_x < col2. max_x )
132
+ && !( col1. min_x > col2. min_x && col1. max_x > col2. min_x && col1. min_x > col2. max_x && col1. max_x > col2. max_x )
133
+ && !( col1. min_y < col2. min_y && col1. max_y < col2. min_y && col1. min_y < col2. max_y && col1. max_y < col2. max_y )
134
+ && !( col1. min_y > col2. min_y && col1. max_y > col2. min_y && col1. min_y > col2. max_y && col1. max_y > col2. max_y )
109
135
}
0 commit comments