2023-09-11 18:52:11 +00:00
|
|
|
//! This example demonstrates how to use the `Camera::viewport_to_world` method.
|
|
|
|
|
2024-01-21 18:03:26 +00:00
|
|
|
use bevy::math::primitives::Direction3d;
|
2023-09-11 18:52:11 +00:00
|
|
|
use bevy::prelude::*;
|
|
|
|
|
|
|
|
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.
|
Add geometric primitives to `bevy_math::prelude` (#11432)
# Objective
Currently, the `primitives` module is inside of the prelude for
`bevy_math`, but the actual primitives are not. This requires either
importing the shapes everywhere that uses them, or adding the
`primitives::` prefix:
```rust
let rectangle = meshes.add(primitives::Rectangle::new(5.0, 2.5));
```
(Note: meshing isn't actually implemented yet, but it's in #11431)
The primitives are meant to be used for a variety of tasks across
several crates, like for meshing, bounding volumes, gizmos, colliders,
and so on, so I think having them in the prelude is justified. It would
make several common tasks a lot more ergonomic.
```rust
let rectangle = meshes.add(Rectangle::new(5.0, 2.5));
```
## Solution
Add `primitives::*` to `bevy_math::prelude`.
---------
Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
2024-01-20 20:15:38 +00:00
|
|
|
let Some(distance) = ray.intersect_plane(ground.translation(), Plane3d::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-01-21 18:03:26 +00:00
|
|
|
gizmos.circle(
|
|
|
|
point + ground.up() * 0.01,
|
|
|
|
Direction3d::new_unchecked(ground.up()), // Up vector is already normalized.
|
|
|
|
0.2,
|
2024-02-24 21:35:32 +00:00
|
|
|
LegacyColor::WHITE,
|
2024-01-21 18:03:26 +00:00
|
|
|
);
|
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-02-24 21:35:32 +00:00
|
|
|
material: materials.add(LegacyColor::rgb(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()
|
|
|
|
});
|
|
|
|
}
|