2023-09-11 18:52:11 +00:00
|
|
|
//! This example demonstrates how to use the `Camera::viewport_to_world_2d` method.
|
|
|
|
|
Implement `From` translation and rotation for isometries (#15733)
# Objective
Several of our APIs (namely gizmos and bounding) use isometries on
current Bevy main. This is nicer than separate properties in a lot of
cases, but users have still expressed usability concerns.
One problem is that in a lot of cases, you only care about e.g.
translation, so you end up with this:
```rust
gizmos.cross_2d(
Isometry2d::from_translation(Vec2::new(-160.0, 120.0)),
12.0,
FUCHSIA,
);
```
The isometry adds quite a lot of length and verbosity, and isn't really
that relevant since only the translation is important here.
It would be nice if you could use the translation directly, and only
supply an isometry if both translation and rotation are needed. This
would make the following possible:
```rust
gizmos.cross_2d(Vec2::new(-160.0, 120.0), 12.0, FUCHSIA);
```
removing a lot of verbosity.
## Solution
Implement `From<Vec2>` and `From<Rot2>` for `Isometry2d`, and
`From<Vec3>`, `From<Vec3A>`, and `From<Quat>` for `Isometry3d`. These
are lossless conversions that fit the semantics of `From`.
This makes the proposed API possible! The methods must now simply take
an `impl Into<IsometryNd>`, and this works:
```rust
gizmos.cross_2d(Vec2::new(-160.0, 120.0), 12.0, FUCHSIA);
```
2024-10-08 16:09:28 +00:00
|
|
|
use bevy::{color::palettes::basic::WHITE, 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(
|
2024-10-13 20:32:06 +00:00
|
|
|
camera_query: Single<(&Camera, &GlobalTransform)>,
|
2024-11-01 18:25:42 +00:00
|
|
|
window: Single<&Window>,
|
2023-09-11 18:52:11 +00:00
|
|
|
mut gizmos: Gizmos,
|
|
|
|
) {
|
2024-10-13 20:32:06 +00:00
|
|
|
let (camera, camera_transform) = *camera_query;
|
2023-09-11 18:52:11 +00:00
|
|
|
|
2024-08-19 21:48:32 +00:00
|
|
|
let Some(cursor_position) = window.cursor_position() else {
|
2023-09-11 18:52:11 +00:00
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Calculate a world position based on the cursor's position.
|
2024-09-03 19:45:15 +00:00
|
|
|
let Ok(point) = camera.viewport_to_world_2d(camera_transform, cursor_position) else {
|
2023-09-11 18:52:11 +00:00
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
Implement `From` translation and rotation for isometries (#15733)
# Objective
Several of our APIs (namely gizmos and bounding) use isometries on
current Bevy main. This is nicer than separate properties in a lot of
cases, but users have still expressed usability concerns.
One problem is that in a lot of cases, you only care about e.g.
translation, so you end up with this:
```rust
gizmos.cross_2d(
Isometry2d::from_translation(Vec2::new(-160.0, 120.0)),
12.0,
FUCHSIA,
);
```
The isometry adds quite a lot of length and verbosity, and isn't really
that relevant since only the translation is important here.
It would be nice if you could use the translation directly, and only
supply an isometry if both translation and rotation are needed. This
would make the following possible:
```rust
gizmos.cross_2d(Vec2::new(-160.0, 120.0), 12.0, FUCHSIA);
```
removing a lot of verbosity.
## Solution
Implement `From<Vec2>` and `From<Rot2>` for `Isometry2d`, and
`From<Vec3>`, `From<Vec3A>`, and `From<Quat>` for `Isometry3d`. These
are lossless conversions that fit the semantics of `From`.
This makes the proposed API possible! The methods must now simply take
an `impl Into<IsometryNd>`, and this works:
```rust
gizmos.cross_2d(Vec2::new(-160.0, 120.0), 12.0, FUCHSIA);
```
2024-10-08 16:09:28 +00:00
|
|
|
gizmos.circle_2d(point, 10., WHITE);
|
2023-09-11 18:52:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn setup(mut commands: Commands) {
|
2024-10-05 01:59:52 +00:00
|
|
|
commands.spawn(Camera2d);
|
2023-09-11 18:52:11 +00:00
|
|
|
}
|