improve duration calculation for go_to and arc_drawing

This commit is contained in:
Franz Dietrich 2025-10-12 12:55:21 +02:00
parent 164be647b2
commit 62e87edd4d
4 changed files with 30 additions and 21 deletions

View File

@ -267,8 +267,11 @@ fn draw_tween_arc(
direction, direction,
); );
// Debug: draw center // Debug: draw center using Lyon tessellation
draw_circle(geom.center.x, geom.center.y, 5.0, GRAY); if let Ok(mesh_data) = crate::tessellation::tessellate_circle(geom.center, 5.0, GRAY, true, 1.0)
{
draw_mesh(&mesh_data.to_mesh());
}
// Calculate how much of the arc we've traveled based on tween progress // Calculate how much of the arc we've traveled based on tween progress
// Use the same eased progress as the turtle position for synchronized animation // Use the same eased progress as the turtle position for synchronized animation
@ -278,20 +281,18 @@ fn draw_tween_arc(
let angle_traveled = total_angle.to_radians() * progress; let angle_traveled = total_angle.to_radians() * progress;
let (rotation_degrees, arc_degrees) = geom.draw_arc_params_partial(angle_traveled); let (rotation_degrees, arc_degrees) = geom.draw_arc_params_partial(angle_traveled);
// Adjust radius inward by half the line width so the line sits on the turtle's path // Use Lyon to tessellate and draw the partial arc
let draw_radius = radius - tween.start_state.pen_width / 2.0; if let Ok(mesh_data) = crate::tessellation::tessellate_arc(
geom.center,
// Draw the partial arc radius,
draw_arc(
geom.center.x,
geom.center.y,
steps as u8,
draw_radius,
rotation_degrees, rotation_degrees,
tween.start_state.pen_width,
arc_degrees, arc_degrees,
tween.start_state.color, tween.start_state.color,
); tween.start_state.pen_width,
steps,
) {
draw_mesh(&mesh_data.to_mesh());
}
} }
/// Draw the turtle shape /// Draw the turtle shape

View File

@ -58,7 +58,7 @@ pub fn execute_command(command: &TurtleCommand, state: &mut TurtleState, world:
arc_degrees, arc_degrees,
state.color, state.color,
state.pen_width, state.pen_width,
*steps as u8, *steps,
) { ) {
world.add_command(DrawCommand::Mesh(mesh_data)); world.add_command(DrawCommand::Mesh(mesh_data));
} }
@ -248,7 +248,7 @@ pub fn add_draw_for_completed_tween(
arc_degrees, arc_degrees,
start_state.color, start_state.color,
start_state.pen_width, start_state.pen_width,
*steps as u8, *steps,
) { ) {
world.add_command(DrawCommand::Mesh(mesh_data)); world.add_command(DrawCommand::Mesh(mesh_data));
} }

View File

@ -266,7 +266,7 @@ pub fn tessellate_arc(
arc_angle_degrees: f32, arc_angle_degrees: f32,
color: Color, color: Color,
stroke_width: f32, stroke_width: f32,
segments: u8, segments: usize,
) -> Result<MeshData, Box<dyn std::error::Error>> { ) -> Result<MeshData, Box<dyn std::error::Error>> {
// Build arc path manually from segments // Build arc path manually from segments
let mut builder = Path::builder(); let mut builder = Path::builder();

View File

@ -419,7 +419,7 @@ impl TweenController {
} }
let speed = state.speed; // Extract speed before borrowing self let speed = state.speed; // Extract speed before borrowing self
let duration = self.calculate_duration(&command_clone, speed); let duration = self.calculate_duration_with_state(&command_clone, state, speed);
// Calculate target state // Calculate target state
let target_state = self.calculate_target_state(state, &command_clone); let target_state = self.calculate_target_state(state, &command_clone);
@ -477,7 +477,12 @@ impl TweenController {
) )
} }
fn calculate_duration(&self, command: &TurtleCommand, speed: AnimationSpeed) -> f64 { fn calculate_duration_with_state(
&self,
command: &TurtleCommand,
current: &TurtleState,
speed: AnimationSpeed,
) -> f64 {
let speed = speed.value(); let speed = speed.value();
let base_time = match command { let base_time = match command {
@ -490,9 +495,12 @@ impl TweenController {
let arc_length = radius * angle.to_radians().abs(); let arc_length = radius * angle.to_radians().abs();
arc_length / speed arc_length / speed
} }
TurtleCommand::Goto(_target) => { TurtleCommand::Goto(target) => {
// Calculate distance (handled in calculate_target_state) // Calculate actual distance from current position to target
0.1 // Placeholder, will be calculated properly let dx = target.x - current.position.x;
let dy = target.y - current.position.y;
let distance = (dx * dx + dy * dy).sqrt();
distance / speed
} }
_ => 0.0, // Instant commands _ => 0.0, // Instant commands
}; };