Skip to content

Commit

Permalink
Add from_xyz to Transform (bevyengine#1212)
Browse files Browse the repository at this point in the history
* Add the from_xyz helper method to Transform

* Use `from_xyz` where possible
  • Loading branch information
DJMcNab authored and rparrett committed Jan 27, 2021
1 parent b9dcdd0 commit 61b58c4
Show file tree
Hide file tree
Showing 27 changed files with 86 additions and 83 deletions.
3 changes: 1 addition & 2 deletions crates/bevy_render/src/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use crate::{
use base::MainPass;
use bevy_asset::Handle;
use bevy_ecs::Bundle;
use bevy_math::Vec3;
use bevy_transform::components::{GlobalTransform, Transform};

/// A component bundle for "mesh" entities
Expand Down Expand Up @@ -73,7 +72,7 @@ impl Default for Camera2dBundle {
..Default::default()
},
visible_entities: Default::default(),
transform: Transform::from_translation(Vec3::new(0.0, 0.0, far - 0.1)),
transform: Transform::from_xyz(0.0, 0.0, far - 0.1),
global_transform: Default::default(),
}
}
Expand Down
6 changes: 6 additions & 0 deletions crates/bevy_transform/src/components/global_transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ pub struct GlobalTransform {
}

impl GlobalTransform {
/// Create a new [`GlobalTransform`] at the position `(x, y, z)`
#[inline]
pub fn from_xyz(x: f32, y: f32, z: f32) -> Self {
Self::from_translation(Vec3::new(x, y, z))
}

#[inline]
pub fn identity() -> Self {
GlobalTransform {
Expand Down
6 changes: 6 additions & 0 deletions crates/bevy_transform/src/components/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ pub struct Transform {
}

impl Transform {
/// Create a new [`Transform`] at the position `(x, y, z)`
#[inline]
pub fn from_xyz(x: f32, y: f32, z: f32) -> Self {
Self::from_translation(Vec3::new(x, y, z))
}

#[inline]
pub fn identity() -> Self {
Transform {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ mod test {
use super::*;
use crate::{hierarchy::BuildChildren, transform_propagate_system::transform_propagate_system};
use bevy_ecs::{IntoSystem, Resources, Schedule, SystemStage, World};
use bevy_math::Vec3;

#[test]
fn correct_children() {
Expand All @@ -92,13 +91,13 @@ mod test {
let mut parent = None;
let mut children = Vec::new();
commands
.spawn((Transform::from_translation(Vec3::new(1.0, 0.0, 0.0)),))
.spawn((Transform::from_xyz(1.0, 0.0, 0.0),))
.for_current_entity(|entity| parent = Some(entity))
.with_children(|parent| {
parent
.spawn((Transform::from_translation(Vec3::new(0.0, 2.0, 0.0)),))
.spawn((Transform::from_xyz(0.0, 2.0, 0.0),))
.for_current_entity(|entity| children.push(entity))
.spawn((Transform::from_translation(Vec3::new(0.0, 0.0, 3.0)),))
.spawn((Transform::from_xyz(0.0, 0.0, 3.0),))
.for_current_entity(|entity| children.push(entity));
});
let parent = parent.unwrap();
Expand Down
27 changes: 11 additions & 16 deletions crates/bevy_transform/src/transform_propagate_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ mod test {
use super::*;
use crate::hierarchy::{parent_update_system, BuildChildren, BuildWorldChildren};
use bevy_ecs::{Resources, Schedule, SystemStage, World};
use bevy_math::Vec3;

#[test]
fn did_propagate() {
Expand All @@ -88,26 +87,26 @@ mod test {

// Root entity
world.spawn((
Transform::from_translation(Vec3::new(1.0, 0.0, 0.0)),
Transform::from_xyz(1.0, 0.0, 0.0),
GlobalTransform::identity(),
));

let mut children = Vec::new();
world
.build()
.spawn((
Transform::from_translation(Vec3::new(1.0, 0.0, 0.0)),
Transform::from_xyz(1.0, 0.0, 0.0),
GlobalTransform::identity(),
))
.with_children(|parent| {
parent
.spawn((
Transform::from_translation(Vec3::new(0.0, 2.0, 0.)),
Transform::from_xyz(0.0, 2.0, 0.),
GlobalTransform::identity(),
))
.for_current_entity(|entity| children.push(entity))
.spawn((
Transform::from_translation(Vec3::new(0.0, 0.0, 3.)),
Transform::from_xyz(0.0, 0.0, 3.),
GlobalTransform::identity(),
))
.for_current_entity(|entity| children.push(entity));
Expand All @@ -116,14 +115,12 @@ mod test {

assert_eq!(
*world.get::<GlobalTransform>(children[0]).unwrap(),
GlobalTransform::from_translation(Vec3::new(1.0, 0.0, 0.0))
* Transform::from_translation(Vec3::new(0.0, 2.0, 0.0))
GlobalTransform::from_xyz(1.0, 0.0, 0.0) * Transform::from_xyz(0.0, 2.0, 0.0)
);

assert_eq!(
*world.get::<GlobalTransform>(children[1]).unwrap(),
GlobalTransform::from_translation(Vec3::new(1.0, 0.0, 0.0))
* Transform::from_translation(Vec3::new(0.0, 0.0, 3.0))
GlobalTransform::from_xyz(1.0, 0.0, 0.0) * Transform::from_xyz(0.0, 0.0, 3.0)
);
}

Expand All @@ -145,18 +142,18 @@ mod test {
let mut children = Vec::new();
commands
.spawn((
Transform::from_translation(Vec3::new(1.0, 0.0, 0.0)),
Transform::from_xyz(1.0, 0.0, 0.0),
GlobalTransform::identity(),
))
.with_children(|parent| {
parent
.spawn((
Transform::from_translation(Vec3::new(0.0, 2.0, 0.0)),
Transform::from_xyz(0.0, 2.0, 0.0),
GlobalTransform::identity(),
))
.for_current_entity(|entity| children.push(entity))
.spawn((
Transform::from_translation(Vec3::new(0.0, 0.0, 3.0)),
Transform::from_xyz(0.0, 0.0, 3.0),
GlobalTransform::identity(),
))
.for_current_entity(|entity| children.push(entity));
Expand All @@ -166,14 +163,12 @@ mod test {

assert_eq!(
*world.get::<GlobalTransform>(children[0]).unwrap(),
GlobalTransform::from_translation(Vec3::new(1.0, 0.0, 0.0))
* Transform::from_translation(Vec3::new(0.0, 2.0, 0.0))
GlobalTransform::from_xyz(1.0, 0.0, 0.0) * Transform::from_xyz(0.0, 2.0, 0.0)
);

assert_eq!(
*world.get::<GlobalTransform>(children[1]).unwrap(),
GlobalTransform::from_translation(Vec3::new(1.0, 0.0, 0.0))
* Transform::from_translation(Vec3::new(0.0, 0.0, 3.0))
GlobalTransform::from_xyz(1.0, 0.0, 0.0) * Transform::from_xyz(0.0, 0.0, 3.0)
);
}
}
3 changes: 1 addition & 2 deletions crates/bevy_ui/src/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use crate::{
};
use bevy_asset::Handle;
use bevy_ecs::Bundle;
use bevy_math::Vec3;
use bevy_render::{
camera::{Camera, OrthographicProjection, VisibleEntities, WindowOrigin},
draw::Draw,
Expand Down Expand Up @@ -189,7 +188,7 @@ impl Default for CameraUiBundle {
..Default::default()
},
visible_entities: Default::default(),
transform: Transform::from_translation(Vec3::new(0.0, 0.0, far - 0.1)),
transform: Transform::from_xyz(0.0, 0.0, far - 0.1),
global_transform: Default::default(),
}
}
Expand Down
2 changes: 1 addition & 1 deletion examples/2d/contributors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ fn setup(
// some sprites should be flipped
let flipped = rnd.gen_bool(0.5);

let mut transform = Transform::from_translation(Vec3::new(pos.0, pos.1, 0.0));
let mut transform = Transform::from_xyz(pos.0, pos.1, 0.0);
transform.scale.x *= if flipped { -1.0 } else { 1.0 };

commands
Expand Down
2 changes: 1 addition & 1 deletion examples/2d/texture_atlas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ fn setup(
// draw the atlas itself
.spawn(SpriteBundle {
material: materials.add(texture_atlas_texture.into()),
transform: Transform::from_translation(Vec3::new(-300.0, 0.0, 0.0)),
transform: Transform::from_xyz(-300.0, 0.0, 0.0),
..Default::default()
});
}
6 changes: 3 additions & 3 deletions examples/3d/3d_scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ fn setup(
.spawn(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Cube { size: 1.0 })),
material: materials.add(Color::rgb(0.8, 0.7, 0.6).into()),
transform: Transform::from_translation(Vec3::new(0.0, 0.5, 0.0)),
transform: Transform::from_xyz(0.0, 0.5, 0.0),
..Default::default()
})
// light
.spawn(LightBundle {
transform: Transform::from_translation(Vec3::new(4.0, 8.0, 4.0)),
transform: Transform::from_xyz(4.0, 8.0, 4.0),
..Default::default()
})
// camera
.spawn(Camera3dBundle {
transform: Transform::from_translation(Vec3::new(-2.0, 2.5, 5.0))
transform: Transform::from_xyz(-2.0, 2.5, 5.0)
.looking_at(Vec3::default(), Vec3::unit_y()),
..Default::default()
});
Expand Down
4 changes: 2 additions & 2 deletions examples/3d/load_gltf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ fn setup(commands: &mut Commands, asset_server: Res<AssetServer>) {
commands
.spawn_scene(asset_server.load("models/FlightHelmet/FlightHelmet.gltf#Scene0"))
.spawn(LightBundle {
transform: Transform::from_translation(Vec3::new(4.0, 5.0, 4.0)),
transform: Transform::from_xyz(4.0, 5.0, 4.0),
..Default::default()
})
.spawn(Camera3dBundle {
transform: Transform::from_translation(Vec3::new(0.7, 0.7, 1.0))
transform: Transform::from_xyz(0.7, 0.7, 1.0)
.looking_at(Vec3::new(0.0, 0.3, 0.0), Vec3::unit_y()),
..Default::default()
});
Expand Down
4 changes: 2 additions & 2 deletions examples/3d/msaa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ fn setup(
})
// light
.spawn(LightBundle {
transform: Transform::from_translation(Vec3::new(4.0, 8.0, 4.0)),
transform: Transform::from_xyz(4.0, 8.0, 4.0),
..Default::default()
})
// camera
.spawn(Camera3dBundle {
transform: Transform::from_translation(Vec3::new(-3.0, 3.0, 5.0))
transform: Transform::from_xyz(-3.0, 3.0, 5.0)
.looking_at(Vec3::default(), Vec3::unit_y()),
..Default::default()
});
Expand Down
8 changes: 4 additions & 4 deletions examples/3d/parenting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ fn setup(
.spawn(PbrBundle {
mesh: cube_handle.clone(),
material: cube_material_handle.clone(),
transform: Transform::from_translation(Vec3::new(0.0, 0.0, 1.0)),
transform: Transform::from_xyz(0.0, 0.0, 1.0),
..Default::default()
})
.with(Rotator)
Expand All @@ -47,18 +47,18 @@ fn setup(
parent.spawn(PbrBundle {
mesh: cube_handle,
material: cube_material_handle,
transform: Transform::from_translation(Vec3::new(0.0, 0.0, 3.0)),
transform: Transform::from_xyz(0.0, 0.0, 3.0),
..Default::default()
});
})
// light
.spawn(LightBundle {
transform: Transform::from_translation(Vec3::new(4.0, 5.0, -4.0)),
transform: Transform::from_xyz(4.0, 5.0, -4.0),
..Default::default()
})
// camera
.spawn(Camera3dBundle {
transform: Transform::from_translation(Vec3::new(5.0, 10.0, 10.0))
transform: Transform::from_xyz(5.0, 10.0, 10.0)
.looking_at(Vec3::default(), Vec3::unit_y()),
..Default::default()
});
Expand Down
8 changes: 4 additions & 4 deletions examples/3d/spawner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ fn setup(
commands
// light
.spawn(LightBundle {
transform: Transform::from_translation(Vec3::new(4.0, -4.0, 5.0)),
transform: Transform::from_xyz(4.0, -4.0, 5.0),
..Default::default()
})
// camera
.spawn(Camera3dBundle {
transform: Transform::from_translation(Vec3::new(0.0, 15.0, 150.0))
transform: Transform::from_xyz(0.0, 15.0, 150.0)
.looking_at(Vec3::default(), Vec3::unit_y()),
..Default::default()
});
Expand All @@ -62,11 +62,11 @@ fn setup(
),
..Default::default()
}),
transform: Transform::from_translation(Vec3::new(
transform: Transform::from_xyz(
rng.gen_range(-50.0, 50.0),
rng.gen_range(-50.0, 50.0),
0.0,
)),
),
..Default::default()
});
}
Expand Down
2 changes: 1 addition & 1 deletion examples/3d/texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ fn setup(
})
// camera
.spawn(Camera3dBundle {
transform: Transform::from_translation(Vec3::new(3.0, 5.0, 8.0))
transform: Transform::from_xyz(3.0, 5.0, 8.0)
.looking_at(Vec3::default(), Vec3::unit_y()),
..Default::default()
});
Expand Down
6 changes: 3 additions & 3 deletions examples/3d/update_gltf_scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ fn setup(
) {
commands
.spawn(LightBundle {
transform: Transform::from_translation(Vec3::new(4.0, 5.0, 4.0)),
transform: Transform::from_xyz(4.0, 5.0, 4.0),
..Default::default()
})
.spawn(Camera3dBundle {
transform: Transform::from_translation(Vec3::new(1.05, 0.9, 1.5))
transform: Transform::from_xyz(1.05, 0.9, 1.5)
.looking_at(Vec3::new(0.0, 0.3, 0.0), Vec3::unit_y()),
..Default::default()
});
Expand All @@ -39,7 +39,7 @@ fn setup(
// with its parent
commands
.spawn((
Transform::from_translation(Vec3::new(0.0, 0.0, -1.0)),
Transform::from_xyz(0.0, 0.0, -1.0),
GlobalTransform::default(),
))
.with_children(|parent| {
Expand Down
8 changes: 4 additions & 4 deletions examples/3d/z_sort_debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ fn setup(
shaded: false,
..Default::default()
}),
transform: Transform::from_translation(Vec3::new(0.0, 0.0, 1.0)),
transform: Transform::from_xyz(0.0, 0.0, 1.0),
..Default::default()
})
.with(Rotator)
Expand All @@ -68,7 +68,7 @@ fn setup(
shaded: false,
..Default::default()
}),
transform: Transform::from_translation(Vec3::new(0.0, 3.0, 0.0)),
transform: Transform::from_xyz(0.0, 3.0, 0.0),
..Default::default()
})
.spawn(PbrBundle {
Expand All @@ -77,13 +77,13 @@ fn setup(
shaded: false,
..Default::default()
}),
transform: Transform::from_translation(Vec3::new(0.0, -3.0, 0.0)),
transform: Transform::from_xyz(0.0, -3.0, 0.0),
..Default::default()
});
})
// camera
.spawn(Camera3dBundle {
transform: Transform::from_translation(Vec3::new(5.0, 10.0, 10.0))
transform: Transform::from_xyz(5.0, 10.0, 10.0)
.looking_at(Vec3::default(), Vec3::unit_y()),
..Default::default()
});
Expand Down
6 changes: 3 additions & 3 deletions examples/android/android.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@ fn setup(
.spawn(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Cube { size: 1.0 })),
material: materials.add(Color::rgb(0.8, 0.7, 0.6).into()),
transform: Transform::from_translation(Vec3::new(0.0, 0.5, 0.0)),
transform: Transform::from_xyz(0.0, 0.5, 0.0),
..Default::default()
})
// light
.spawn(LightBundle {
transform: Transform::from_translation(Vec3::new(4.0, 8.0, 4.0)),
transform: Transform::from_xyz(4.0, 8.0, 4.0),
..Default::default()
})
// camera
.spawn(Camera3dBundle {
transform: Transform::from_translation(Vec3::new(-2.0, 2.5, 5.0))
transform: Transform::from_xyz(-2.0, 2.5, 5.0)
.looking_at(Vec3::default(), Vec3::unit_y()),
..Default::default()
});
Expand Down
Loading

0 comments on commit 61b58c4

Please sign in to comment.