improve duration calculation for go_to and arc_drawing
This commit is contained in:
parent
164be647b2
commit
62e87edd4d
@ -267,8 +267,11 @@ fn draw_tween_arc(
|
||||
direction,
|
||||
);
|
||||
|
||||
// Debug: draw center
|
||||
draw_circle(geom.center.x, geom.center.y, 5.0, GRAY);
|
||||
// Debug: draw center using Lyon tessellation
|
||||
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
|
||||
// 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 (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
|
||||
let draw_radius = radius - tween.start_state.pen_width / 2.0;
|
||||
|
||||
// Draw the partial arc
|
||||
draw_arc(
|
||||
geom.center.x,
|
||||
geom.center.y,
|
||||
steps as u8,
|
||||
draw_radius,
|
||||
// Use Lyon to tessellate and draw the partial arc
|
||||
if let Ok(mesh_data) = crate::tessellation::tessellate_arc(
|
||||
geom.center,
|
||||
radius,
|
||||
rotation_degrees,
|
||||
tween.start_state.pen_width,
|
||||
arc_degrees,
|
||||
tween.start_state.color,
|
||||
);
|
||||
tween.start_state.pen_width,
|
||||
steps,
|
||||
) {
|
||||
draw_mesh(&mesh_data.to_mesh());
|
||||
}
|
||||
}
|
||||
|
||||
/// Draw the turtle shape
|
||||
|
||||
@ -58,7 +58,7 @@ pub fn execute_command(command: &TurtleCommand, state: &mut TurtleState, world:
|
||||
arc_degrees,
|
||||
state.color,
|
||||
state.pen_width,
|
||||
*steps as u8,
|
||||
*steps,
|
||||
) {
|
||||
world.add_command(DrawCommand::Mesh(mesh_data));
|
||||
}
|
||||
@ -248,7 +248,7 @@ pub fn add_draw_for_completed_tween(
|
||||
arc_degrees,
|
||||
start_state.color,
|
||||
start_state.pen_width,
|
||||
*steps as u8,
|
||||
*steps,
|
||||
) {
|
||||
world.add_command(DrawCommand::Mesh(mesh_data));
|
||||
}
|
||||
|
||||
@ -266,7 +266,7 @@ pub fn tessellate_arc(
|
||||
arc_angle_degrees: f32,
|
||||
color: Color,
|
||||
stroke_width: f32,
|
||||
segments: u8,
|
||||
segments: usize,
|
||||
) -> Result<MeshData, Box<dyn std::error::Error>> {
|
||||
// Build arc path manually from segments
|
||||
let mut builder = Path::builder();
|
||||
|
||||
@ -419,7 +419,7 @@ impl TweenController {
|
||||
}
|
||||
|
||||
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
|
||||
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 base_time = match command {
|
||||
@ -490,9 +495,12 @@ impl TweenController {
|
||||
let arc_length = radius * angle.to_radians().abs();
|
||||
arc_length / speed
|
||||
}
|
||||
TurtleCommand::Goto(_target) => {
|
||||
// Calculate distance (handled in calculate_target_state)
|
||||
0.1 // Placeholder, will be calculated properly
|
||||
TurtleCommand::Goto(target) => {
|
||||
// Calculate actual distance from current position to target
|
||||
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
|
||||
};
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user