pub struct Collider {
pub geometry_type: GeometryType,
pub position: Vector2<f32>,
pub scale: Vector2<f32>,
}Expand description
Struct to represent the real collider of the object.
Fields§
§geometry_type: GeometryType§position: Vector2<f32>§scale: Vector2<f32>Implementations§
Source§impl Collider
impl Collider
Sourcepub fn new(
geometry_type: GeometryType,
position: Vector2<f32>,
scale: Vector2<f32>,
) -> Self
pub fn new( geometry_type: GeometryType, position: Vector2<f32>, scale: Vector2<f32>, ) -> Self
Create a new collider with parameters.
Sourcepub fn new_simple(geometry_type: GeometryType) -> Self
pub fn new_simple(geometry_type: GeometryType) -> Self
Create a new collider only with the geometry type as a parameter.
Examples found in repository?
examples/gravity_simulation.rs (line 25)
14fn setup(context: &mut Context) {
15 let table: Shape = Shape::new(Orientation::Horizontal, GeometryType::Rectangle, Color::by_option(ColorOption::Black));
16 let object: Shape = Shape::new(Orientation::Horizontal, GeometryType::Circle(Circle::default()), Color::by_option(ColorOption::Blue));
17
18 context.commands.spawn(vec![
19 Box::new(table),
20 Box::new(Transform::new(
21 Position::new(Vector2::new(0.0, -0.70), Strategy::Normalized),
22 0.0,
23 Vector2::new(0.90, 0.10)
24 )),
25 Box::new(Collision::new(Collider::new_simple(GeometryType::Rectangle)))
26 ]);
27
28 context.commands.spawn(vec![
29 Box::new(object),
30 Box::new(Transform::new(
31 Position::new(Vector2::new(400.0, 100.0), Strategy::Pixelated),
32 0.0,
33 Vector2::new(0.50, 0.50)
34 )),
35 Box::new(Collision::new(Collider::new_simple(GeometryType::Square))),
36 Box::new(Gravity::new(0.0)),
37 Box::new(Velocity::new(Vector2::new(0.2, 0.2))),
38 Box::new(RigidBody::new(BodyType::Dynamic, 1.0, 0.9, 1.0))
39 ]);
40}More examples
examples/physics_simulation.rs (line 35)
20fn setup(context: &mut Context) {
21 let circle: Circle = Circle::new(64, 0.5);
22 let red_object: Shape = Shape::new(Orientation::Horizontal, GeometryType::Circle(circle.clone()), Color::by_option(ColorOption::Red));
23 let blue_object: Shape = Shape::new(Orientation::Horizontal, GeometryType::Circle(circle.clone()), Color::by_option(ColorOption::Blue));
24
25 context.commands.spawn(
26 vec![
27 Box::new(red_object),
28 Box::new(Object()),
29 Box::new(Transform::new(
30 Position::new(Vector2::new(0.0, 0.0), Strategy::Normalized),
31 0.0,
32 Vector2::new(0.30, 0.30)
33 )),
34 Box::new(Velocity::new(Vector2::new(0.45, 0.45))),
35 Box::new(Collision::new(Collider::new_simple(GeometryType::Square)))
36 ]
37 );
38
39 context.commands.spawn(
40 vec![
41 Box::new(blue_object),
42 Box::new(Object()),
43 Box::new(Transform::new(
44 Position::new(Vector2::new(-0.45, 0.0), Strategy::Normalized),
45 0.0,
46 Vector2::new(0.30, 0.30)
47 )),
48 Box::new(Velocity::new(Vector2::new(0.45, 0.45))),
49 Box::new(Collision::new(Collider::new_simple(GeometryType::Square)))
50 ]
51 );
52
53 spawn_border(context, Orientation::Horizontal, Vector2::new(0.0, -1.), Vector2::new(context.window_configuration.width as f32, 0.01));
54 spawn_border(context, Orientation::Horizontal, Vector2::new(0.0, 1.), Vector2::new(context.window_configuration.width as f32, 0.01));
55 spawn_border(context, Orientation::Vertical, Vector2::new(1.35, 0.), Vector2::new(0.01, context.window_configuration.height as f32));
56 spawn_border(context, Orientation::Vertical, Vector2::new(-1.35, 0.), Vector2::new(0.01, context.window_configuration.height as f32));
57}
58
59fn update(context: &mut Context) {
60 let mut query: Query = Query::new(&context.world).with::<Object>();
61 let entities: Vec<Entity> = query.entities_with_components().unwrap();
62
63 check_border_collision(context, &entities);
64 check_object_collision(context, &entities);
65 move_objects(context, &entities);
66}
67
68fn spawn_border(context: &mut Context, orientation: Orientation, position: Vector2<f32>, scale: Vector2<f32>) {
69 let border: Shape = Shape::new(orientation, GeometryType::Rectangle, Color::by_option(ColorOption::White));
70
71 context.commands.spawn(
72 vec![
73 Box::new(border),
74 Box::new(Border()),
75 Box::new(Transform::new(
76 Position::new(position, Strategy::Normalized),
77 0.0,
78 scale
79 )),
80 Box::new(Collision::new(Collider::new_simple(GeometryType::Rectangle)))
81 ]
82 );
83}examples/performance.rs (line 43)
16fn setup(context: &mut Context) {
17 let circle: Circle = Circle::new(64, 0.5);
18 let mut thread_rng: ThreadRng = rng();
19
20 let left_border_perimeter: f64 = -1.3;
21 let right_border_perimiter: f64 = 1.3;
22 let bottom_border_perimeter: f64 = -0.9;
23 let top_border_perimeter: f64 = 0.9;
24
25 // Update the number of spawns for performance benchmarking.
26 for _ in 1..=10 {
27 let randomic_position_x: f32 = thread_rng.random_range(left_border_perimeter..right_border_perimiter) as f32;
28 let randomic_position_y: f32 = thread_rng.random_range(bottom_border_perimeter..top_border_perimeter) as f32;
29
30 let randomic_velocity_x: f32 = thread_rng.random_range(-0.5..0.5) as f32;
31 let randomic_velocity_y: f32 = thread_rng.random_range(-0.5..0.5) as f32;
32
33 context.commands.spawn(
34 vec![
35 Box::new(Shape::new(Orientation::Horizontal, GeometryType::Circle(circle.clone()), Color::by_option(ColorOption::Blue))),
36 Box::new(Object()),
37 Box::new(Transform::new(
38 Position::new(Vector2::new(randomic_position_x, randomic_position_y), Strategy::Normalized),
39 0.0,
40 Vector2::new(0.15, 0.15)
41 )),
42 Box::new(Velocity::new(Vector2::new(randomic_velocity_x, randomic_velocity_y))),
43 Box::new(Collision::new(Collider::new_simple(GeometryType::Square)))
44 ]
45 );
46 }
47
48 spawn_border(context, Orientation::Horizontal, Vector2::new(0.0, -1.), Vector2::new(context.window_configuration.width as f32, 0.01));
49 spawn_border(context, Orientation::Horizontal, Vector2::new(0.0, 1.), Vector2::new(context.window_configuration.width as f32, 0.01));
50 spawn_border(context, Orientation::Vertical, Vector2::new(1.35, 0.), Vector2::new(0.01, context.window_configuration.height as f32));
51 spawn_border(context, Orientation::Vertical, Vector2::new(-1.35, 0.), Vector2::new(0.01, context.window_configuration.height as f32));
52}
53
54fn update(context: &mut Context) {
55 context.commands.show_fps(context.game_loop_listener.current_fps, Color::by_option(ColorOption::Black));
56
57 let mut query: Query = Query::new(&context.world).with::<Object>();
58 let entities: Vec<Entity> = query.entities_with_components().unwrap();
59
60 check_border_collision(context, &entities);
61 move_objects(context, &entities);
62}
63
64fn spawn_border(context: &mut Context, orientation: Orientation, position: Vector2<f32>, scale: Vector2<f32>) {
65 let border: Shape = Shape::new(orientation, GeometryType::Rectangle, Color::by_option(ColorOption::White));
66
67 context.commands.spawn(
68 vec![
69 Box::new(border),
70 Box::new(Border()),
71 Box::new(Transform::new(
72 Position::new(position, Strategy::Normalized),
73 0.0,
74 scale
75 )),
76 Box::new(Collision::new(Collider::new_simple(GeometryType::Rectangle)))
77 ]
78 );
79}examples/breakout.rs (line 107)
67fn setup(context: &mut Context) {
68 let player: Shape = Shape::new(Orientation::Horizontal, GeometryType::Rectangle, Color::by_option(ColorOption::Purple));
69 let little_ball: Shape = Shape::new(
70 Orientation::Horizontal,
71 GeometryType::Circle(Circle::new(64, 0.2)),
72 Color::by_option(ColorOption::Black)
73 );
74 let start_text: Text = Text::new(
75 &mut context.render_state,
76 Font::new(Fonts::RobotoMonoItalic.get_path(), 40.0),
77 Position::new(Vector2::new(298.0, 380.0), Strategy::Pixelated),
78 Color::by_option(ColorOption::Black),
79 "> enter <".to_string()
80 );
81
82 let mut thread_rng: ThreadRng = rand::rng();
83 let random_direction: bool = thread_rng.random_bool(1.0 / 3.0);
84
85 let velocity_x: f32 = if random_direction {
86 1.2
87 } else {
88 -1.2
89 };
90
91 context.commands.add_resources(vec![
92 Box::new(LittleBallRespawnTimer::new()),
93 Box::new(NextState::default())
94 ]);
95 context.commands.spawn(vec![Box::new(start_text)]);
96
97 context.commands.spawn(
98 vec![
99 Box::new(player),
100 Box::new(Player()),
101 Box::new(Transform::new(
102 Position::new(Vector2::new(0.0, -0.85), Strategy::Normalized),
103 0.0,
104 Vector2::new(0.15, 0.10)
105 )),
106 Box::new(Velocity::new(Vector2::new(2.0, 2.0))),
107 Box::new(Collision::new(Collider::new_simple(GeometryType::Rectangle)))
108 ]
109 );
110
111 context.commands.spawn(
112 vec![
113 Box::new(little_ball),
114 Box::new(LittleBall()),
115 Box::new(Transform::new(
116 Position::new(Vector2::new(0.0, -0.5), Strategy::Normalized),
117 0.0,
118 Vector2::new(0.10, 0.10)
119 )),
120 Box::new(Velocity::new(Vector2::new(velocity_x, -0.5))),
121 Box::new(Collision::new(Collider::new_simple(GeometryType::Square)))
122 ]
123 );
124
125 spawn_border(context, Vector2::new(1.05, 0.0));
126 spawn_border(context, Vector2::new(-1.05, 0.0));
127 spawn_targets(context);
128}
129
130fn update(context: &mut Context) {
131 let keyboard_input: KeyboardInput = context.world.get_resource_cloned::<KeyboardInput>().unwrap();
132 let mouse_input: MouseInput = context.world.get_resource_cloned::<MouseInput>().unwrap();
133 let is_hover: bool = mouse_input.mouse_position.x >= 298.0 && (mouse_input.mouse_position.y > 380.0 && mouse_input.mouse_position.y < 416.0);
134
135 if
136 keyboard_input.is_key_released(KeyboardKey::Enter) ||
137 (mouse_input.is_mouse_button_released(MouseButton::Left) && is_hover)
138 {
139 let mut next_state: ResourceRefMut<'_, NextState> = context.world.get_resource_mut::<NextState>().unwrap();
140 next_state.0 = GameState::Running;
141
142 let mut query: Query = Query::new(&context.world).with::<Text>();
143 if let Some(entity) = query.entities_with_components().unwrap().first() {
144 context.commands.despawn(entity.clone());
145 }
146 }
147
148 if context.world.get_resource::<NextState>().unwrap().0 == GameState::Running {
149 let mut player_query: Query = Query::new(&context.world).with::<Player>();
150 let player_entity: Entity = player_query.entities_with_components().unwrap().first().unwrap().clone();
151
152 let mut little_ball_query: Query = Query::new(&context.world).with::<LittleBall>();
153 let little_ball_entity: Entity = little_ball_query.entities_with_components().unwrap().first().unwrap().clone();
154
155 let mut thread_rng: ThreadRng = rand::rng();
156 let random_factor: f32 = thread_rng.random_range(-0.5..0.5);
157
158 move_player(context, keyboard_input, player_entity);
159 move_little_ball(context, little_ball_entity);
160 check_player_little_ball_collision(context, player_entity, little_ball_entity, random_factor);
161 check_little_ball_borders_collision(context, little_ball_entity, random_factor);
162 check_litte_ball_targets_collision(context, little_ball_entity, random_factor);
163 respawn_little_ball_after_outbounds(context, little_ball_entity);
164 }
165}
166
167fn spawn_border(context: &mut Context, position: Vector2<f32>) {
168 let border: Shape = Shape::new(Orientation::Vertical, GeometryType::Rectangle, Color::by_option(ColorOption::Cyan));
169
170 context.commands.spawn(
171 vec![
172 Box::new(border),
173 Box::new(Border()),
174 Box::new(Transform::new(
175 Position::new(position, Strategy::Normalized),
176 0.0,
177 Vector2::new(0.01, context.window_configuration.height as f32)
178 )),
179 Box::new(Collision::new(Collider::new_simple(GeometryType::Rectangle)))
180 ]
181 );
182}
183
184fn spawn_targets(context: &mut Context) {
185 let width: f32 = 0.15;
186 let height: f32 = 0.10;
187
188 let rows: i32 = 8;
189 let columns: i32 = 10;
190 let spacing_x: f32 = 0.09;
191 let spacing_y: f32 = 0.02;
192
193 let start_x: f32 = -(columns as f32 * (width + spacing_x)) / 2.0;
194 let start_y: f32 = 1.0 - 0.1;
195
196 for row in 0..rows {
197 for column in 0..columns {
198 let x: f32 = start_x + column as f32 * (width + spacing_x);
199 let y: f32 = start_y - row as f32 * (height + spacing_y);
200
201 let mut color: Color = Color::by_option(ColorOption::Red);
202
203 if row == 2 || row == 3 {
204 color = Color::by_option(ColorOption::Orange);
205 } else if row == 4 || row == 5 {
206 color = Color::by_option(ColorOption::Green);
207 } else if row == 6 || row == 7 {
208 color = Color::by_option(ColorOption::Yellow);
209 }
210
211 context.commands.spawn(
212 vec![
213 Box::new(Shape::new(Orientation::Horizontal, GeometryType::Rectangle, color)),
214 Box::new(Target()),
215 Box::new(Transform::new(
216 Position::new(Vector2::new(x, y), Strategy::Normalized),
217 0.0,
218 Vector2::new(width, height)
219 )),
220 Box::new(Collision::new(Collider::new_simple(GeometryType::Rectangle))),
221 ]
222 );
223 }
224 }
225}examples/pong.rs (line 104)
66fn setup(context: &mut Context) {
67 let gray_racket_sprite: Sprite = Sprite::new("textures/pong/gray_racket_256x256.png".to_string());
68 let pink_racket_sprite: Sprite = Sprite::new("textures/pong/pink_racket_256x256.png".to_string());
69 let pong_ball_sprite: Sprite = Sprite::new("textures/pong/pong_ball_left_256x256.png".to_string());
70
71 let mut game_audio: GameAudio = GameAudio::default();
72 game_audio.0.load_streaming_sound(
73 "game_music",
74 "audio/pong/soundtrack/arcade_music.ogg",
75 AudioSettings::default().loop_region(..).volume(Value::Fixed(Decibels(-10.0)))
76 ).ok();
77 game_audio.0.play_streaming_sound("game_music".to_string()).ok();
78
79 game_audio.0.load_static_sound(
80 "racket_hit",
81 "audio/pong/effect/pong_hit.wav",
82 AudioSettings::default()
83 ).ok();
84
85 context.commands.add_resources(vec![
86 Box::new(PongBallRespawnTimer::default()),
87 Box::new(game_audio)
88 ]);
89
90 spawn_border(context, Vector2::new(0.0, -1.0));
91 spawn_border(context, Vector2::new(0.0, 1.0));
92
93 context.commands.spawn(
94 vec![
95 Box::new(gray_racket_sprite),
96 Box::new(Transform::new(
97 Position::new(Vector2::new(-1.0, 0.23), Strategy::Normalized),
98 0.0,
99 Vector2::new(0.55, 0.55)
100 )),
101 Box::new(Racket()),
102 Box::new(GrayRacket()),
103 Box::new(Velocity::new(Vector2::new(1.5, 1.5))),
104 Box::new(Collision::new(Collider::new_simple(GeometryType::Square)))
105 ]
106 );
107
108 context.commands.spawn(
109 vec![
110 Box::new(pink_racket_sprite),
111 Box::new(Transform::new(
112 Position::new(Vector2::new(1.0, 0.25), Strategy::Normalized),
113 0.0,
114 Vector2::new(0.55, 0.55)
115 )),
116 Box::new(Racket()),
117 Box::new(PinkRacket()),
118 Box::new(Velocity::new(Vector2::new(1.5, 1.5))),
119 Box::new(Collision::new(Collider::new_simple(GeometryType::Square)))
120 ]
121 );
122
123 context.commands.spawn(
124 vec![
125 Box::new(pong_ball_sprite),
126 Box::new(Transform::new(
127 Position::new(Vector2::new(0.0, 0.0), Strategy::Normalized),
128 0.0,
129 Vector2::new(0.55, 0.55)
130 )),
131 Box::new(PongBall()),
132 Box::new(Velocity::new(Vector2::new(1.0, 1.0))),
133 Box::new(Collision::new(Collider::new_simple(GeometryType::Square)))
134 ]
135 );
136}
137
138fn update(context: &mut Context) {
139 let keyboard_input: KeyboardInput = context.world.get_resource_cloned::<KeyboardInput>().unwrap();
140
141 let mut pong_ball_query: Query = Query::new(&context.world).with::<PongBall>();
142 let pong_ball_entities: Vec<Entity> = pong_ball_query.entities_with_components().unwrap();
143 let pong_ball: &Entity = pong_ball_entities.first().unwrap();
144 let mut thread_rng: ThreadRng = rand::rng();
145 let random_factor: f32 = thread_rng.random_range(-0.5..0.5);
146
147 move_gray_racket(context, keyboard_input.clone());
148 move_pink_racket(context, keyboard_input.clone());
149 move_pong_ball(context, pong_ball);
150 check_rackets_ball_collision(context, pong_ball, random_factor);
151 check_borders_ball_collision(context, pong_ball, random_factor);
152 respawn_pong_ball_after_outbounds(context, pong_ball);
153}
154
155fn spawn_border(context: &mut Context, position: Vector2<f32>) {
156 let border: Shape = Shape::new(Orientation::Horizontal, GeometryType::Rectangle, Color::by_option(ColorOption::Black));
157
158 context.commands.spawn(
159 vec![
160 Box::new(border),
161 Box::new(Border()),
162 Box::new(Transform::new(
163 Position::new(position, Strategy::Normalized),
164 0.0,
165 Vector2::new(context.window_configuration.width as f32, 0.01)
166 )),
167 Box::new(Collision::new(Collider::new_simple(GeometryType::Rectangle)))
168 ]
169 );
170}Trait Implementations§
Auto Trait Implementations§
impl Freeze for Collider
impl RefUnwindSafe for Collider
impl Send for Collider
impl Sync for Collider
impl Unpin for Collider
impl UnsafeUnpin for Collider
impl UnwindSafe for Collider
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Convert
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can
then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Convert
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
Convert
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
Convert
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.Source§impl<T> DowncastSync for T
impl<T> DowncastSync for T
Source§impl<S> FromSample<S> for S
impl<S> FromSample<S> for S
fn from_sample_(s: S) -> S
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more