2023-09-11 18:52:11 +00:00
|
|
|
//! This example demonstrates how to use the `Camera::viewport_to_world` method.
|
|
|
|
|
2024-04-01 19:59:08 +00:00
|
|
|
use bevy::prelude::*;
|
2023-09-11 18:52:11 +00:00
|
|
|
|
|
|
|
fn main() {
|
|
|
|
App::new()
|
|
|
|
.add_plugins(DefaultPlugins)
|
|
|
|
.add_systems(Startup, setup)
|
|
|
|
.add_systems(Update, draw_cursor)
|
|
|
|
.run();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn draw_cursor(
|
|
|
|
camera_query: Query<(&Camera, &GlobalTransform)>,
|
|
|
|
ground_query: Query<&GlobalTransform, With<Ground>>,
|
|
|
|
windows: Query<&Window>,
|
|
|
|
mut gizmos: Gizmos,
|
|
|
|
) {
|
|
|
|
let (camera, camera_transform) = camera_query.single();
|
|
|
|
let ground = ground_query.single();
|
|
|
|
|
|
|
|
let Some(cursor_position) = windows.single().cursor_position() else {
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Calculate a ray pointing from the camera into the world based on the cursor's position.
|
|
|
|
let Some(ray) = camera.viewport_to_world(camera_transform, cursor_position) else {
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Calculate if and where the ray is hitting the ground plane.
|
2024-04-18 14:13:22 +00:00
|
|
|
let Some(distance) =
|
|
|
|
ray.intersect_plane(ground.translation(), InfinitePlane3d::new(ground.up()))
|
Split `Ray` into `Ray2d` and `Ray3d` and simplify plane construction (#10856)
# Objective
A better alternative version of #10843.
Currently, Bevy has a single `Ray` struct for 3D. To allow better
interoperability with Bevy's primitive shapes (#10572) and some third
party crates (that handle e.g. spatial queries), it would be very useful
to have separate versions for 2D and 3D respectively.
## Solution
Separate `Ray` into `Ray2d` and `Ray3d`. These new structs also take
advantage of the new primitives by using `Direction2d`/`Direction3d` for
the direction:
```rust
pub struct Ray2d {
pub origin: Vec2,
pub direction: Direction2d,
}
pub struct Ray3d {
pub origin: Vec3,
pub direction: Direction3d,
}
```
and by using `Plane2d`/`Plane3d` in `intersect_plane`:
```rust
impl Ray2d {
// ...
pub fn intersect_plane(&self, plane_origin: Vec2, plane: Plane2d) -> Option<f32> {
// ...
}
}
```
---
## Changelog
### Added
- `Ray2d` and `Ray3d`
- `Ray2d::new` and `Ray3d::new` constructors
- `Plane2d::new` and `Plane3d::new` constructors
### Removed
- Removed `Ray` in favor of `Ray3d`
### Changed
- `direction` is now a `Direction2d`/`Direction3d` instead of a vector,
which provides guaranteed normalization
- `intersect_plane` now takes a `Plane2d`/`Plane3d` instead of just a
vector for the plane normal
- `Direction2d` and `Direction3d` now derive `Serialize` and
`Deserialize` to preserve ray (de)serialization
## Migration Guide
`Ray` has been renamed to `Ray3d`.
### Ray creation
Before:
```rust
Ray {
origin: Vec3::ZERO,
direction: Vec3::new(0.5, 0.6, 0.2).normalize(),
}
```
After:
```rust
// Option 1:
Ray3d {
origin: Vec3::ZERO,
direction: Direction3d::new(Vec3::new(0.5, 0.6, 0.2)).unwrap(),
}
// Option 2:
Ray3d::new(Vec3::ZERO, Vec3::new(0.5, 0.6, 0.2))
```
### Plane intersections
Before:
```rust
let result = ray.intersect_plane(Vec2::X, Vec2::Y);
```
After:
```rust
let result = ray.intersect_plane(Vec2::X, Plane2d::new(Vec2::Y));
```
2023-12-06 14:09:04 +00:00
|
|
|
else {
|
2023-09-11 18:52:11 +00:00
|
|
|
return;
|
|
|
|
};
|
|
|
|
let point = ray.get_point(distance);
|
|
|
|
|
|
|
|
// Draw a circle just above the ground plane at that position.
|
2024-05-06 20:52:05 +00:00
|
|
|
gizmos.circle(point + ground.up() * 0.01, ground.up(), 0.2, Color::WHITE);
|
2023-09-11 18:52:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Component)]
|
|
|
|
struct Ground;
|
|
|
|
|
|
|
|
fn setup(
|
|
|
|
mut commands: Commands,
|
|
|
|
mut meshes: ResMut<Assets<Mesh>>,
|
|
|
|
mut materials: ResMut<Assets<StandardMaterial>>,
|
|
|
|
) {
|
|
|
|
// plane
|
|
|
|
commands.spawn((
|
|
|
|
PbrBundle {
|
2024-02-08 18:01:34 +00:00
|
|
|
mesh: meshes.add(Plane3d::default().mesh().size(20., 20.)),
|
2024-05-29 13:50:28 +00:00
|
|
|
material: materials.add(Color::srgb(0.3, 0.5, 0.3)),
|
2023-09-11 18:52:11 +00:00
|
|
|
..default()
|
|
|
|
},
|
|
|
|
Ground,
|
|
|
|
));
|
|
|
|
|
|
|
|
// light
|
|
|
|
commands.spawn(DirectionalLightBundle {
|
|
|
|
transform: Transform::from_translation(Vec3::ONE).looking_at(Vec3::ZERO, Vec3::Y),
|
|
|
|
..default()
|
|
|
|
});
|
|
|
|
|
|
|
|
// camera
|
|
|
|
commands.spawn(Camera3dBundle {
|
|
|
|
transform: Transform::from_xyz(15.0, 5.0, 15.0).looking_at(Vec3::ZERO, Vec3::Y),
|
|
|
|
..default()
|
|
|
|
});
|
|
|
|
}
|