forked from dimforge/bevy_rapier
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathray_casting3.rs
112 lines (97 loc) · 3.17 KB
/
ray_casting3.rs
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
use bevy::color::palettes::basic;
use bevy::prelude::*;
use bevy::window::PrimaryWindow;
use bevy_rapier3d::prelude::*;
fn main() {
App::new()
.insert_resource(ClearColor(Color::srgb(
0xF9 as f32 / 255.0,
0xF9 as f32 / 255.0,
0xFF as f32 / 255.0,
)))
.add_plugins((
DefaultPlugins,
RapierPhysicsPlugin::<NoUserData>::default(),
RapierDebugRenderPlugin::default(),
))
.add_systems(Startup, (setup_graphics, setup_physics))
.add_systems(Update, cast_ray)
.run();
}
pub fn setup_graphics(mut commands: Commands) {
commands.spawn(Camera3dBundle {
transform: Transform::from_xyz(-30.0, 30.0, 100.0)
.looking_at(Vec3::new(0.0, 10.0, 0.0), Vec3::Y),
..Default::default()
});
}
pub fn setup_physics(mut commands: Commands) {
/*
* Ground
*/
let ground_size = 200.1;
let ground_height = 0.1;
commands.spawn((
TransformBundle::from(Transform::from_xyz(0.0, -ground_height, 0.0)),
Collider::cuboid(ground_size, ground_height, ground_size),
));
/*
* Create the cubes
*/
let num = 8;
let rad = 1.0;
let shift = rad * 2.0 + rad;
let centerx = shift * (num / 2) as f32;
let centery = shift / 2.0;
let centerz = shift * (num / 2) as f32;
let mut offset = -(num as f32) * (rad * 2.0 + rad) * 0.5;
for j in 0usize..20 {
for i in 0..num {
for k in 0usize..num {
let x = i as f32 * shift - centerx + offset;
let y = j as f32 * shift + centery + 3.0;
let z = k as f32 * shift - centerz + offset;
// Build the rigid body.
commands.spawn((
TransformBundle::from(Transform::from_xyz(x, y, z)),
RigidBody::Dynamic,
Collider::cuboid(rad, rad, rad),
));
}
}
offset -= 0.05 * rad * (num as f32 - 1.0);
}
}
pub fn cast_ray(
mut commands: Commands,
windows: Query<&Window, With<PrimaryWindow>>,
rapier_context: DefaultRapierContextAccess,
cameras: Query<(&Camera, &GlobalTransform)>,
) {
let window = windows.single();
let Some(cursor_position) = window.cursor_position() else {
return;
};
// We will color in read the colliders hovered by the mouse.
for (camera, camera_transform) in &cameras {
// First, compute a ray from the mouse position.
let Some(ray) = camera.viewport_to_world(camera_transform, cursor_position) else {
return;
};
// Then cast the ray.
let hit = rapier_context.cast_ray(
ray.origin,
ray.direction.into(),
f32::MAX,
true,
QueryFilter::only_dynamic(),
);
if let Some((entity, _toi)) = hit {
// Color in blue the entity we just hit.
// Because of the query filter, only colliders attached to a dynamic body
// will get an event.
let color = basic::BLUE.into();
commands.entity(entity).insert(ColliderDebugColor(color));
}
}
}