-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
170 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
crates/beet_spatial/src/procedural_animation/circle_animation.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
use crate::prelude::*; | ||
use bevy::prelude::*; | ||
|
||
#[derive(Debug, Clone, PartialEq, Component, Reflect)] | ||
#[reflect(Debug, Default, PartialEq)] | ||
pub struct CircleAnimation { | ||
pub radius: f32, | ||
pub position: Vec3, | ||
pub rotation: Quat, | ||
} | ||
|
||
impl Default for CircleAnimation { | ||
fn default() -> Self { | ||
Self { | ||
radius: 0.5, | ||
position: Vec3::ZERO, | ||
rotation: Quat::IDENTITY, | ||
} | ||
} | ||
} | ||
|
||
|
||
impl ProceduralAnimationStrategy for CircleAnimation { | ||
/// C = 2πr | ||
fn total_length(&self) -> f32 { std::f32::consts::PI * 2.0 * self.radius } | ||
|
||
fn fraction_to_pos(&self, t: f32) -> Vec3 { | ||
let angle = t * std::f32::consts::PI * 2.0; | ||
let x = self.rotation * Vec3::X * angle.cos() * self.radius; | ||
let y = self.rotation * Vec3::Y * angle.sin() * self.radius; | ||
self.position + x + y | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
crates/beet_spatial/src/procedural_animation/points_animation.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
use crate::prelude::*; | ||
use bevy::prelude::*; | ||
|
||
|
||
|
||
#[derive(Debug, Clone, PartialEq, Component, Reflect)] | ||
#[reflect(Debug, Default, PartialEq)] | ||
pub struct PointsAnimation { | ||
pub wrap: bool, | ||
pub points: Vec<Vec3>, | ||
} | ||
|
||
impl Default for PointsAnimation { | ||
fn default() -> Self { | ||
Self { | ||
points: vec![-Vec3::X, Vec3::X], | ||
wrap: false, | ||
} | ||
} | ||
} | ||
|
||
impl PointsAnimation { | ||
/// create a new path that is open (i.e. not a loop) | ||
pub fn new_open(points: Vec<Vec3>) -> Self { | ||
Self { | ||
points, | ||
wrap: false, | ||
} | ||
} | ||
|
||
/// create a new path that is closed (i.e. a loop) | ||
pub fn new_closed(points: Vec<Vec3>) -> Self { | ||
Self { | ||
points, | ||
wrap: true, | ||
} | ||
} | ||
|
||
pub fn num_segments(&self) -> usize { | ||
if self.wrap { | ||
self.points.len() | ||
} else { | ||
self.points.len() - 1 | ||
} | ||
} | ||
} | ||
|
||
impl ProceduralAnimationStrategy for PointsAnimation { | ||
fn fraction_to_pos(&self, t: f32) -> Vec3 { | ||
let t = t.clamp(0.0, 1.0); | ||
let t = t * self.num_segments() as f32; | ||
let segment = t.floor() as usize; | ||
let t = t - segment as f32; | ||
let i1 = segment; | ||
let i2 = (segment + 1) % self.points.len(); | ||
self.points[i1].lerp(self.points[i2], t) | ||
} | ||
|
||
fn total_length(&self) -> f32 { | ||
let mut length = 0.0; | ||
for i in 0..self.num_segments() { | ||
let i1 = i; | ||
let i2 = (i + 1) % self.points.len(); | ||
length += self.points[i1].distance(self.points[i2]); | ||
} | ||
length | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters