dave: rotation tweaks

Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
William Casarin
2025-03-24 14:16:09 -07:00
parent 343f2ce410
commit 32f7d484f8
2 changed files with 13 additions and 7 deletions

View File

@@ -5,6 +5,7 @@ use egui::{Rect, Response};
pub struct DaveAvatar { pub struct DaveAvatar {
rotation: Quaternion, rotation: Quaternion,
rot_dir: egui::Vec2,
} }
// A simple quaternion implementation // A simple quaternion implementation
@@ -357,6 +358,7 @@ fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
Self { Self {
rotation: Quaternion::identity(), rotation: Quaternion::identity(),
rot_dir: egui::Vec2::ZERO,
} }
} }
} }
@@ -368,17 +370,21 @@ impl DaveAvatar {
// Update rotation based on drag or animation // Update rotation based on drag or animation
if response.dragged() { if response.dragged() {
// Create rotation quaternions based on drag // Create rotation quaternions based on drag
let x_rotation = let dx = response.drag_delta().x;
Quaternion::from_axis_angle([1.0, 0.0, 0.0], response.drag_delta().y * 0.01); let dy = response.drag_delta().y;
let y_rotation = let x_rotation = Quaternion::from_axis_angle([1.0, 0.0, 0.0], dy * 0.01);
Quaternion::from_axis_angle([0.0, 1.0, 0.0], response.drag_delta().x * 0.01); let y_rotation = Quaternion::from_axis_angle([0.0, 1.0, 0.0], dx * 0.01);
self.rot_dir = egui::Vec2::new(dx, dy);
// Apply rotations (order matters) // Apply rotations (order matters)
self.rotation = y_rotation.multiply(&x_rotation).multiply(&self.rotation); self.rotation = y_rotation.multiply(&x_rotation).multiply(&self.rotation);
} else { } else {
// Continuous rotation - reduced speed and simplified axis // Continuous rotation - reduced speed and simplified axis
let continuous_rotation = Quaternion::from_axis_angle([0.0, 1.0, 0.0], 0.005); let x_rotation = Quaternion::from_axis_angle([1.0, 0.0, 0.0], self.rot_dir.y * 0.01);
self.rotation = continuous_rotation.multiply(&self.rotation); let y_rotation = Quaternion::from_axis_angle([0.0, 1.0, 0.0], self.rot_dir.x * 0.01);
self.rotation = y_rotation.multiply(&x_rotation).multiply(&self.rotation);
} }
// Create model matrix from rotation quaternion // Create model matrix from rotation quaternion

View File

@@ -107,7 +107,7 @@ impl Dave {
}); });
if let Some(avatar) = &mut self.avatar { if let Some(avatar) = &mut self.avatar {
let avatar_size = Vec2::splat(400.0); let avatar_size = Vec2::splat(200.0);
let pos = Vec2::splat(100.0).to_pos2(); let pos = Vec2::splat(100.0).to_pos2();
let pos = Rect::from_min_max(pos, pos + avatar_size); let pos = Rect::from_min_max(pos, pos + avatar_size);
avatar.render(pos, ui); avatar.render(pos, ui);