Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion examples/math/cubic_splines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ fn setup(mut commands: Commands) {

let default_control_data = ControlPoints {
points_and_tangents: default_points.into_iter().zip(default_tangents).collect(),
preview_point: None,
};

let curve = form_curve(&default_control_data, spline_mode, cycling_mode);
Expand Down Expand Up @@ -146,6 +147,7 @@ struct Curve(Option<CubicCurve<Vec2>>);
#[derive(Clone, Resource)]
struct ControlPoints {
points_and_tangents: Vec<(Vec2, Vec2)>,
preview_point: Option<(Vec2, Vec2)>,
}

/// This system is responsible for updating the [`Curve`] when the [control points] or active modes
Expand Down Expand Up @@ -205,9 +207,14 @@ fn form_curve(
spline_mode: SplineMode,
cycling_mode: CyclingMode,
) -> Curve {
let (points, tangents): (Vec<_>, Vec<_>) =
let (mut points, mut tangents): (Vec<_>, Vec<_>) =
control_points.points_and_tangents.iter().copied().unzip();

if let Some((preview_point, preview_tangent)) = control_points.preview_point {
points.push(preview_point);
tangents.push(preview_tangent);
}

match spline_mode {
SplineMode::Hermite => {
let spline = CubicHermite::new(points, tangents);
Expand Down Expand Up @@ -350,6 +357,7 @@ fn handle_mouse_press(

// The start of the click-and-drag motion represents the point to add,
// while the difference with the current position represents the tangent.
control_points.preview_point = None;
control_points.points_and_tangents.push((point, tangent));

// Reset the edit move since we've consumed it.
Expand All @@ -363,6 +371,7 @@ fn handle_mouse_press(
fn draw_edit_move(
edit_move: Res<MouseEditMove>,
mouse_position: Res<MousePosition>,
mut control_points: ResMut<ControlPoints>,
mut gizmos: Gizmos,
camera: Single<(&Camera, &GlobalTransform)>,
) {
Expand All @@ -384,6 +393,10 @@ fn draw_edit_move(
return;
};

// Set preview_point for previewing the curve while dragging the tangent
let tangent = end - start;
control_points.preview_point = Some((start, tangent));

gizmos.circle_2d(start, 10.0, Color::srgb(0.0, 1.0, 0.7));
gizmos.circle_2d(start, 7.0, Color::srgb(0.0, 1.0, 0.7));
gizmos.arrow_2d(start, end, Color::srgb(1.0, 0.0, 0.7));
Expand Down
Loading