Skip to content

Commit

Permalink
Consistently use PI to specify angles in examples. (bevyengine#5825)
Browse files Browse the repository at this point in the history
Examples inconsistently use either `TAU`, `PI`, `FRAC_PI_2` or `FRAC_PI_4`.
Often in odd ways and without `use`ing the constants, making it difficult to parse.

 * Use `PI` to specify angles.
 * General code-quality improvements.
 * Fix borked `hierarchy` example.


Co-authored-by: devil-ira <[email protected]>
  • Loading branch information
tim-blackbird and tim-blackbird committed Aug 30, 2022
1 parent fe6246d commit 65252bb
Show file tree
Hide file tree
Showing 24 changed files with 140 additions and 171 deletions.
12 changes: 7 additions & 5 deletions examples/2d/mesh2d_manual.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
//! It doesn't use the [`Material2d`] abstraction, but changes the vertex buffer to include vertex color.
//! Check out the "mesh2d" example for simpler / higher level 2d meshes.

use std::f32::consts::PI;

use bevy::{
core_pipeline::core_2d::Transparent2d,
prelude::*,
Expand Down Expand Up @@ -62,12 +64,12 @@ fn star(
// These vertices are specified in 3D space.
let mut v_pos = vec![[0.0, 0.0, 0.0]];
for i in 0..10 {
// Angle of each vertex is 1/10 of TAU, plus PI/2 for positioning vertex 0
let a = std::f32::consts::FRAC_PI_2 - i as f32 * std::f32::consts::TAU / 10.0;
// Radius of internal vertices (2, 4, 6, 8, 10) is 100, it's 200 for external
// The angle between each vertex is 1/10 of a full rotation.
let a = i as f32 * PI / 5.0;
// The radius of inner vertices (even indices) is 100. For outer vertices (odd indices) it's 200.
let r = (1 - i % 2) as f32 * 100.0 + 100.0;
// Add the vertex coordinates
v_pos.push([r * a.cos(), r * a.sin(), 0.0]);
// Add the vertex position.
v_pos.push([r * a.sin(), r * a.cos(), 0.0]);
}
// Set the position attribute
star.insert_attribute(Mesh::ATTRIBUTE_POSITION, v_pos);
Expand Down
12 changes: 6 additions & 6 deletions examples/3d/lighting.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//! Illustrates different lights of various types and colors, some static, some moving over
//! a simple scene.

use std::f32::consts::PI;

use bevy::prelude::*;

fn main() {
Expand Down Expand Up @@ -34,7 +36,7 @@ fn setup(

// left wall
let mut transform = Transform::from_xyz(2.5, 2.5, 0.0);
transform.rotate_z(std::f32::consts::FRAC_PI_2);
transform.rotate_z(PI / 2.);
commands.spawn_bundle(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Box::new(5.0, 0.15, 5.0))),
transform,
Expand All @@ -47,7 +49,7 @@ fn setup(
});
// back (right) wall
let mut transform = Transform::from_xyz(0.0, 2.5, -2.5);
transform.rotate_x(std::f32::consts::FRAC_PI_2);
transform.rotate_x(PI / 2.);
commands.spawn_bundle(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Box::new(5.0, 0.15, 5.0))),
transform,
Expand Down Expand Up @@ -138,9 +140,7 @@ fn setup(
})
.with_children(|builder| {
builder.spawn_bundle(PbrBundle {
transform: Transform::from_rotation(Quat::from_rotation_x(
std::f32::consts::PI / 2.0,
)),
transform: Transform::from_rotation(Quat::from_rotation_x(PI / 2.0)),
mesh: meshes.add(Mesh::from(shape::Capsule {
depth: 0.125,
radius: 0.1,
Expand Down Expand Up @@ -202,7 +202,7 @@ fn setup(
},
transform: Transform {
translation: Vec3::new(0.0, 2.0, 0.0),
rotation: Quat::from_rotation_x(-std::f32::consts::FRAC_PI_4),
rotation: Quat::from_rotation_x(-PI / 4.),
..default()
},
..default()
Expand Down
6 changes: 4 additions & 2 deletions examples/3d/load_gltf.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Loads and renders a glTF file as a scene.

use std::f32::consts::PI;

use bevy::prelude::*;

fn main() {
Expand Down Expand Up @@ -50,8 +52,8 @@ fn animate_light_direction(
transform.rotation = Quat::from_euler(
EulerRot::ZYX,
0.0,
time.seconds_since_startup() as f32 * std::f32::consts::TAU / 10.0,
-std::f32::consts::FRAC_PI_4,
time.seconds_since_startup() as f32 * PI / 5.0,
-PI / 4.,
);
}
}
14 changes: 6 additions & 8 deletions examples/3d/render_to_texture.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Shows how to render to a texture. Useful for mirrors, UI, or exporting images.

use std::f32::consts::PI;

use bevy::{
core_pipeline::clear_color::ClearColorConfig,
prelude::*,
Expand Down Expand Up @@ -104,7 +106,7 @@ fn setup(
..default()
},
transform: Transform::from_translation(Vec3::new(0.0, 0.0, 15.0))
.looking_at(Vec3::default(), Vec3::Y),
.looking_at(Vec3::ZERO, Vec3::Y),
..default()
})
.insert(first_pass_layer);
Expand All @@ -125,19 +127,15 @@ fn setup(
.spawn_bundle(PbrBundle {
mesh: cube_handle,
material: material_handle,
transform: Transform {
translation: Vec3::new(0.0, 0.0, 1.5),
rotation: Quat::from_rotation_x(-std::f32::consts::PI / 5.0),
..default()
},
transform: Transform::from_xyz(0.0, 0.0, 1.5)
.with_rotation(Quat::from_rotation_x(-PI / 5.0)),
..default()
})
.insert(MainPassCube);

// The main pass camera.
commands.spawn_bundle(Camera3dBundle {
transform: Transform::from_translation(Vec3::new(0.0, 0.0, 15.0))
.looking_at(Vec3::default(), Vec3::Y),
transform: Transform::from_xyz(0.0, 0.0, 15.0).looking_at(Vec3::ZERO, Vec3::Y),
..default()
});
}
Expand Down
25 changes: 12 additions & 13 deletions examples/3d/shadow_biases.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Demonstrates how shadow biases affect shadows in a 3d scene.

use std::f32::consts::PI;

use bevy::{input::mouse::MouseMotion, prelude::*};

fn main() {
Expand Down Expand Up @@ -61,8 +63,6 @@ fn setup(
..default()
});

let theta = std::f32::consts::FRAC_PI_4;
let light_transform = Mat4::from_euler(EulerRot::ZYX, 0.0, std::f32::consts::FRAC_PI_2, -theta);
commands.spawn_bundle(DirectionalLightBundle {
directional_light: DirectionalLight {
illuminance: 100000.0,
Expand All @@ -80,7 +80,12 @@ fn setup(
shadows_enabled: true,
..default()
},
transform: Transform::from_matrix(light_transform),
transform: Transform::from_rotation(Quat::from_euler(
EulerRot::ZYX,
0.0,
PI / 2.,
-PI / 4.,
)),
..default()
});

Expand Down Expand Up @@ -308,16 +313,10 @@ fn camera_controller(

if mouse_delta != Vec2::ZERO {
// Apply look update
let (pitch, yaw) = (
(options.pitch - mouse_delta.y * 0.5 * options.sensitivity * dt).clamp(
-0.99 * std::f32::consts::FRAC_PI_2,
0.99 * std::f32::consts::FRAC_PI_2,
),
options.yaw - mouse_delta.x * options.sensitivity * dt,
);
transform.rotation = Quat::from_euler(EulerRot::ZYX, 0.0, yaw, pitch);
options.pitch = pitch;
options.yaw = yaw;
options.pitch = (options.pitch - mouse_delta.y * 0.5 * options.sensitivity * dt)
.clamp(-PI / 2., PI / 2.);
options.yaw -= mouse_delta.x * options.sensitivity * dt;
transform.rotation = Quat::from_euler(EulerRot::ZYX, 0.0, options.yaw, options.pitch);
}
}
}
11 changes: 8 additions & 3 deletions examples/3d/shadow_caster_receiver.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Demonstrates how to prevent meshes from casting/receiving shadows in a 3d scene.

use std::f32::consts::PI;

use bevy::{
pbr::{NotShadowCaster, NotShadowReceiver},
prelude::*,
Expand Down Expand Up @@ -89,8 +91,6 @@ fn setup(
..default()
});

let theta = std::f32::consts::FRAC_PI_4;
let light_transform = Mat4::from_euler(EulerRot::ZYX, 0.0, std::f32::consts::FRAC_PI_2, -theta);
commands.spawn_bundle(DirectionalLightBundle {
directional_light: DirectionalLight {
illuminance: 100000.0,
Expand All @@ -106,7 +106,12 @@ fn setup(
shadows_enabled: true,
..default()
},
transform: Transform::from_matrix(light_transform),
transform: Transform::from_rotation(Quat::from_euler(
EulerRot::ZYX,
0.0,
PI / 2.,
-PI / 4.,
)),
..default()
});

Expand Down
17 changes: 8 additions & 9 deletions examples/3d/shapes.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//! This example demonstrates the built-in 3d shapes in Bevy.
//! The scene includes a patterned texture and a rotation for visualizing the normals and UVs.

use std::f32::consts::PI;

use bevy::{
prelude::*,
render::render_resource::{Extent3d, TextureDimension, TextureFormat},
Expand Down Expand Up @@ -48,15 +50,12 @@ fn setup(
.spawn_bundle(PbrBundle {
mesh: shape,
material: debug_material.clone(),
transform: Transform {
translation: Vec3::new(
-X_EXTENT / 2. + i as f32 / (num_shapes - 1) as f32 * X_EXTENT,
2.0,
0.0,
),
rotation: Quat::from_rotation_x(-std::f32::consts::PI / 4.),
..default()
},
transform: Transform::from_xyz(
-X_EXTENT / 2. + i as f32 / (num_shapes - 1) as f32 * X_EXTENT,
2.0,
0.0,
)
.with_rotation(Quat::from_rotation_x(-PI / 4.)),
..default()
})
.insert(Shape);
Expand Down
25 changes: 9 additions & 16 deletions examples/3d/skybox.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Load a cubemap texture onto a cube like a skybox and cycle through different compressed texture formats

use std::f32::consts::PI;

use bevy::{
asset::LoadState,
input::mouse::MouseMotion,
Expand Down Expand Up @@ -66,19 +68,16 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
illuminance: 32000.0,
..default()
},
transform: Transform {
translation: Vec3::new(0.0, 2.0, 0.0),
rotation: Quat::from_rotation_x(-std::f32::consts::FRAC_PI_4),
..default()
},
transform: Transform::from_xyz(0.0, 2.0, 0.0)
.with_rotation(Quat::from_rotation_x(-PI / 4.)),
..default()
});

let skybox_handle = asset_server.load(CUBEMAPS[0].0);
// camera
commands
.spawn_bundle(Camera3dBundle {
transform: Transform::from_xyz(0.0, 0.0, 8.0).looking_at(Vec3::default(), Vec3::Y),
transform: Transform::from_xyz(0.0, 0.0, 8.0).looking_at(Vec3::ZERO, Vec3::Y),
..default()
})
.insert(CameraController::default());
Expand Down Expand Up @@ -410,16 +409,10 @@ pub fn camera_controller(

if mouse_delta != Vec2::ZERO {
// Apply look update
let (pitch, yaw) = (
(options.pitch - mouse_delta.y * 0.5 * options.sensitivity * dt).clamp(
-0.99 * std::f32::consts::FRAC_PI_2,
0.99 * std::f32::consts::FRAC_PI_2,
),
options.yaw - mouse_delta.x * options.sensitivity * dt,
);
transform.rotation = Quat::from_euler(EulerRot::ZYX, 0.0, yaw, pitch);
options.pitch = pitch;
options.yaw = yaw;
options.pitch = (options.pitch - mouse_delta.y * 0.5 * options.sensitivity * dt)
.clamp(-PI / 2., PI / 2.);
options.yaw -= mouse_delta.x * options.sensitivity * dt;
transform.rotation = Quat::from_euler(EulerRot::ZYX, 0.0, options.yaw, options.pitch);
}
}
}
9 changes: 3 additions & 6 deletions examples/3d/split_screen.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Renders two cameras to the same window to accomplish "split screen".

use std::f32::consts::PI;

use bevy::{
core_pipeline::clear_color::ClearColorConfig,
prelude::*,
Expand Down Expand Up @@ -36,12 +38,7 @@ fn setup(

// Light
commands.spawn_bundle(DirectionalLightBundle {
transform: Transform::from_rotation(Quat::from_euler(
EulerRot::ZYX,
0.0,
1.0,
-std::f32::consts::FRAC_PI_4,
)),
transform: Transform::from_rotation(Quat::from_euler(EulerRot::ZYX, 0.0, 1.0, -PI / 4.)),
directional_light: DirectionalLight {
shadows_enabled: true,
..default()
Expand Down
14 changes: 7 additions & 7 deletions examples/3d/spotlight.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::f32::consts::PI;

use bevy::{
diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
pbr::NotShadowCaster,
Expand Down Expand Up @@ -74,8 +76,8 @@ fn setup(
intensity: 200.0, // lumens
color: Color::WHITE,
shadows_enabled: true,
inner_angle: std::f32::consts::PI / 4.0 * 0.85,
outer_angle: std::f32::consts::PI / 4.0,
inner_angle: PI / 4.0 * 0.85,
outer_angle: PI / 4.0,
..default()
},
..default()
Expand Down Expand Up @@ -123,13 +125,11 @@ fn light_sway(time: Res<Time>, mut query: Query<(&mut Transform, &mut SpotLight)
for (mut transform, mut angles) in query.iter_mut() {
transform.rotation = Quat::from_euler(
EulerRot::XYZ,
-std::f32::consts::FRAC_PI_2
+ (time.seconds_since_startup() * 0.67 * 3.0).sin() as f32 * 0.5,
-PI / 2. + (time.seconds_since_startup() * 0.67 * 3.0).sin() as f32 * 0.5,
(time.seconds_since_startup() * 3.0).sin() as f32 * 0.5,
0.0,
);
let angle = ((time.seconds_since_startup() * 1.2).sin() as f32 + 1.0)
* (std::f32::consts::FRAC_PI_4 - 0.1);
let angle = ((time.seconds_since_startup() * 1.2).sin() as f32 + 1.0) * (PI / 4. - 0.1);
angles.inner_angle = angle * 0.8;
angles.outer_angle = angle;
}
Expand All @@ -140,7 +140,7 @@ fn movement(
time: Res<Time>,
mut query: Query<&mut Transform, With<Movable>>,
) {
for mut transform in query.iter_mut() {
for mut transform in &mut query {
let mut direction = Vec3::ZERO;
if input.pressed(KeyCode::Up) {
direction.z -= 1.0;
Expand Down
Loading

0 comments on commit 65252bb

Please sign in to comment.