@@ -9,14 +9,13 @@ pub fn setup_coliders_system(mut commands: Commands) {
9
9
for x in collider_range. clone ( ) {
10
10
for y in collider_range. clone ( ) {
11
11
for z in collider_range. clone ( ) {
12
- let key = x * COLLIDER_GRID_SIZE * COLLIDER_GRID_SIZE + y * COLLIDER_GRID_SIZE + z;
13
12
commands
14
13
. spawn ( Collider :: cuboid ( 0.5 , 0.5 , 0.5 ) )
15
14
. insert ( TransformBundle :: from ( Transform :: from_xyz (
16
- x as f32 , y as f32 , z as f32 ,
15
+ x as f32 , y as f32 , z as f32 ,
17
16
) ) )
18
- . insert ( collider_components:: MyCollider { key } ) ;
19
- }
17
+ . insert ( collider_components:: MyCollider { relative_position : Vec3 { x : x as f32 , y : y as f32 , z : z as f32 } } ) ;
18
+ }
20
19
}
21
20
}
22
21
}
@@ -30,7 +29,7 @@ pub fn handle_collider_update_events_system(
30
29
let event_position =
31
30
Vec3 :: new ( event. grid_center_position [ 0 ] , event. grid_center_position [ 1 ] , event. grid_center_position [ 2 ] ) . floor ( ) ;
32
31
for ( mut transform, collider) in query. iter_mut ( ) {
33
- let relative_position = relative_colider_position ( collider. key ) ;
32
+ let relative_position = collider. relative_position ;
34
33
let collider_position = ( event_position + relative_position) . floor ( ) ;
35
34
let block = chunk_manager. get_block ( collider_position) ;
36
35
@@ -50,18 +49,6 @@ pub fn handle_collider_update_events_system(
50
49
}
51
50
}
52
51
53
- fn relative_colider_position ( key : u32 ) -> Vec3 {
54
- let x = key / ( COLLIDER_GRID_SIZE * COLLIDER_GRID_SIZE ) ;
55
- let y = ( key % ( COLLIDER_GRID_SIZE * COLLIDER_GRID_SIZE ) ) / COLLIDER_GRID_SIZE ;
56
- let z = key % COLLIDER_GRID_SIZE ;
57
-
58
- Vec3 {
59
- x : x as f32 - ( COLLIDER_GRID_SIZE / 2 ) as f32 ,
60
- y : y as f32 - ( COLLIDER_GRID_SIZE / 2 ) as f32 ,
61
- z : z as f32 - ( COLLIDER_GRID_SIZE / 2 ) as f32 ,
62
- }
63
- }
64
-
65
52
#[ cfg( test) ]
66
53
mod tests {
67
54
use collider_events:: ColliderUpdateEvent ;
@@ -95,15 +82,15 @@ mod tests {
95
82
app. insert_resource ( terrain_resources:: ChunkManager :: new ( ) ) ;
96
83
97
84
app. world . spawn ( (
98
- Transform {
99
- translation : Vec3 {
100
- x : 0.0 ,
101
- y : 0.0 ,
102
- z : 0.0 ,
85
+ Transform {
86
+ translation : Vec3 {
87
+ x : 0.0 ,
88
+ y : 0.0 ,
89
+ z : 0.0 ,
90
+ } ,
91
+ ..Default :: default ( )
103
92
} ,
104
- ..Default :: default ( )
105
- } ,
106
- collider_components:: MyCollider { key : 0 } ,
93
+ collider_components:: MyCollider { relative_position : Vec3 { x : 1.0 , y : 2.0 , z : 3.0 } } ,
107
94
) ) ;
108
95
109
96
let block = BlockId :: Dirt ;
@@ -122,15 +109,15 @@ mod tests {
122
109
resource. insert_chunks ( chunks) ;
123
110
resource. set_block (
124
111
Vec3 {
125
- x : 9 .0,
126
- y : 9 .0,
127
- z : 9 .0,
112
+ x : 6 .0,
113
+ y : 7 .0,
114
+ z : 8 .0,
128
115
} ,
129
116
block,
130
117
) ;
131
118
132
119
app. world . send_event ( ColliderUpdateEvent {
133
- grid_center_position : [ 10 .0, 10 .0, 10 .0] ,
120
+ grid_center_position : [ 5 .0, 5 .0, 5 .0] ,
134
121
} ) ;
135
122
136
123
app. update ( ) ;
@@ -141,9 +128,9 @@ mod tests {
141
128
let ( collider_transform, _) = collider_query. single ( & app. world ) ;
142
129
assert_eq ! (
143
130
Vec3 {
144
- x: 9 .5,
145
- y: 9 .5,
146
- z: 9 .5
131
+ x: 6 .5,
132
+ y: 7 .5,
133
+ z: 8 .5
147
134
} ,
148
135
collider_transform. translation
149
136
) ;
0 commit comments