Fix set_scale and set_rotation in new Transform api (#500)

This commit is contained in:
Carter Anderson 2020-09-16 00:19:14 -07:00 committed by GitHub
parent d4ab2f4d47
commit ad7613c674
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 77 additions and 24 deletions

View File

@ -28,11 +28,22 @@ impl GlobalTransform {
GlobalTransform::new(Mat4::from_quat(rotation))
}
// TODO: make sure scale is positive
pub fn from_scale(scale: Vec3) -> Self {
pub fn from_scale(scale: f32) -> Self {
GlobalTransform::new(Mat4::from_scale(Vec3::splat(scale)))
}
pub fn from_non_uniform_scale(scale: Vec3) -> Self {
GlobalTransform::new(Mat4::from_scale(scale))
}
pub fn from_translation_rotation_scale(translation: Vec3, rotation: Quat, scale: f32) -> Self {
GlobalTransform::new(Mat4::from_scale_rotation_translation(
Vec3::splat(scale),
rotation,
translation,
))
}
pub fn with_translation(mut self, translation: Vec3) -> Self {
self.translate(translation);
self
@ -43,11 +54,16 @@ impl GlobalTransform {
self
}
pub fn with_scale(mut self, scale: Vec3) -> Self {
pub fn with_scale(mut self, scale: f32) -> Self {
self.apply_scale(scale);
self
}
pub fn with_non_uniform_scale(mut self, scale: Vec3) -> Self {
self.apply_non_uniform_scale(scale);
self
}
pub fn value(&self) -> &Mat4 {
&self.value
}
@ -83,14 +99,21 @@ impl GlobalTransform {
}
pub fn set_rotation(&mut self, rotation: Quat) {
let rotation = rotation * self.rotation().conjugate();
rotation.normalize();
self.value = Mat4::from_quat(rotation) * self.value;
self.value =
Mat4::from_scale_rotation_translation(self.scale(), rotation, self.translation());
}
pub fn set_scale(&mut self, scale: Vec3) {
let scale = scale / self.scale();
self.value = Mat4::from_scale(scale) * self.value;
pub fn set_scale(&mut self, scale: f32) {
self.value = Mat4::from_scale_rotation_translation(
Vec3::splat(scale),
self.rotation(),
self.translation(),
);
}
pub fn set_non_uniform_scale(&mut self, scale: Vec3) {
self.value =
Mat4::from_scale_rotation_translation(scale, self.rotation(), self.translation());
}
pub fn translate(&mut self, translation: Vec3) {
@ -101,7 +124,11 @@ impl GlobalTransform {
self.value = Mat4::from_quat(rotation) * self.value;
}
pub fn apply_scale(&mut self, scale: Vec3) {
pub fn apply_scale(&mut self, scale: f32) {
self.value = Mat4::from_scale(Vec3::splat(scale)) * self.value;
}
pub fn apply_non_uniform_scale(&mut self, scale: Vec3) {
self.value = Mat4::from_scale(scale) * self.value;
}
}

View File

@ -28,8 +28,19 @@ impl Transform {
Transform::new(Mat4::from_quat(rotation))
}
// TODO: make sure scale is positive
pub fn from_scale(scale: Vec3) -> Self {
pub fn from_scale(scale: f32) -> Self {
Transform::new(Mat4::from_scale(Vec3::splat(scale)))
}
pub fn from_translation_rotation_scale(translation: Vec3, rotation: Quat, scale: f32) -> Self {
Transform::new(Mat4::from_scale_rotation_translation(
Vec3::splat(scale),
rotation,
translation,
))
}
pub fn from_non_uniform_scale(scale: Vec3) -> Self {
Transform::new(Mat4::from_scale(scale))
}
@ -43,11 +54,16 @@ impl Transform {
self
}
pub fn with_scale(mut self, scale: Vec3) -> Self {
pub fn with_scale(mut self, scale: f32) -> Self {
self.apply_scale(scale);
self
}
pub fn with_non_uniform_scale(mut self, scale: Vec3) -> Self {
self.apply_non_uniform_scale(scale);
self
}
pub fn value(&self) -> &Mat4 {
&self.value
}
@ -87,14 +103,21 @@ impl Transform {
}
pub fn set_rotation(&mut self, rotation: Quat) {
let rotation = rotation * self.rotation().conjugate();
rotation.normalize();
self.value = Mat4::from_quat(rotation) * self.value;
self.value =
Mat4::from_scale_rotation_translation(self.scale(), rotation, self.translation());
}
pub fn set_scale(&mut self, scale: Vec3) {
let scale = scale / self.scale();
self.value = Mat4::from_scale(scale) * self.value;
pub fn set_scale(&mut self, scale: f32) {
self.value = Mat4::from_scale_rotation_translation(
Vec3::splat(scale),
self.rotation(),
self.translation(),
);
}
pub fn set_non_uniform_scale(&mut self, scale: Vec3) {
self.value =
Mat4::from_scale_rotation_translation(scale, self.rotation(), self.translation());
}
pub fn translate(&mut self, translation: Vec3) {
@ -105,7 +128,11 @@ impl Transform {
self.value = Mat4::from_quat(rotation) * self.value;
}
pub fn apply_scale(&mut self, scale: Vec3) {
pub fn apply_scale(&mut self, scale: f32) {
self.value = Mat4::from_scale(Vec3::splat(scale)) * self.value;
}
pub fn apply_non_uniform_scale(&mut self, scale: Vec3) {
self.value = Mat4::from_scale(scale) * self.value;
}
}

View File

@ -39,7 +39,7 @@ fn setup(
.spawn(Camera2dComponents::default())
.spawn(SpriteSheetComponents {
texture_atlas: texture_atlas_handle,
transform: Transform::from_scale(Vec3::one() * 6.0),
transform: Transform::from_scale(6.0),
..Default::default()
})
.with(Timer::from_seconds(0.1, true));

View File

@ -61,8 +61,7 @@ fn load_atlas(
.spawn(Camera2dComponents::default())
// draw a sprite from the atlas
.spawn(SpriteSheetComponents {
transform: Transform::from_translation(Vec3::new(150.0, 0.0, 0.0))
.with_scale(Vec3::one() * 4.0),
transform: Transform::from_scale(4.0).with_translation(Vec3::new(150.0, 0.0, 0.0)),
sprite: TextureAtlasSprite::new(vendor_index as u32),
texture_atlas: atlas_handle,
..Default::default()

View File

@ -15,7 +15,7 @@ fn spawn_system(
commands
.spawn(SpriteComponents {
material,
transform: Transform::from_scale(Vec3::one() * 0.1),
transform: Transform::from_scale(0.1),
..Default::default()
})
.with(Velocity(