-
-
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
10 changed files
with
244 additions
and
16 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
pub mod play_procedural_animation; | ||
#[allow(unused_imports)] | ||
pub use self::play_procedural_animation::*; | ||
pub mod procedural_animation_plugin; | ||
#[allow(unused_imports)] | ||
pub use self::procedural_animation_plugin::*; | ||
pub mod procedural_animation_shape; | ||
#[allow(unused_imports)] | ||
pub use self::procedural_animation_shape::*; | ||
pub mod procedural_animation_speed; | ||
#[allow(unused_imports)] | ||
pub use self::procedural_animation_speed::*; |
56 changes: 56 additions & 0 deletions
56
crates/beet_spatial/src/procedural_animation/play_procedural_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,56 @@ | ||
use crate::prelude::*; | ||
use beet_flow::prelude::*; | ||
use bevy::animation::RepeatAnimation; | ||
use bevy::prelude::*; | ||
|
||
|
||
#[derive(Debug, Clone, PartialEq, Component, Reflect, Action)] | ||
#[observers(play_procedural_animation)] | ||
#[reflect(Default, Component, ActionMeta)] | ||
pub struct PlayProceduralAnimation { | ||
pub shape: ProceduralAnimationShape, | ||
pub speed: ProceduralAnimationSpeed, | ||
pub repeat: RepeatAnimation, | ||
pub num_animations: u32, | ||
pub last_t: f32, | ||
} | ||
|
||
impl Default for PlayProceduralAnimation { | ||
fn default() -> Self { | ||
Self { | ||
shape:default(), | ||
repeat: default(), | ||
speed: default(), | ||
num_animations: 0, | ||
last_t: 0.0, | ||
} | ||
} | ||
} | ||
|
||
impl PlayProceduralAnimation { | ||
pub fn get_fraction(&self,time:Res<Time>) -> f32 { | ||
match self.speed{ | ||
ProceduralAnimationSpeed::MetersPerSecond(mps) => mps * time.delta_seconds(), | ||
ProceduralAnimationSpeed::FractionPerSecond(fps) => fps, | ||
} | ||
} | ||
|
||
} | ||
|
||
fn play_procedural_animation( | ||
trigger: Trigger<OnRun>, | ||
time:Res<Time>, | ||
mut transforms: Query<&mut Transform>, | ||
mut query: Query<(&mut PlayProceduralAnimation, &TargetAgent)>, | ||
) { | ||
let (mut play_procedural_animation, target_agent) = query | ||
.get_mut(trigger.entity()) | ||
.expect(expect_action::ACTION_QUERY_MISSING); | ||
|
||
let mut transform = transforms | ||
.get_mut(target_agent.0) | ||
.expect(expect_action::TARGET_MISSING); | ||
|
||
// let t = | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
crates/beet_spatial/src/procedural_animation/procedural_animation_plugin.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,10 @@ | ||
use crate::prelude::*; | ||
use beet_flow::prelude::*; | ||
use bevy::prelude::*; | ||
|
||
|
||
pub fn procedural_animation_plugin(app: &mut App) { | ||
app.add_plugins(ActionPlugin::<( | ||
InsertOnTrigger<OnRun, PlayProceduralAnimation>, | ||
)>::default()); | ||
} |
127 changes: 127 additions & 0 deletions
127
crates/beet_spatial/src/procedural_animation/procedural_animation_shape.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,127 @@ | ||
use bevy::prelude::*; | ||
|
||
pub trait ProceduralAnimationStrategy { | ||
/// For a given value between 0 (inclusive) and 1 (not inclusive), return a position. | ||
/// | ||
/// # Panics | ||
/// May panic if `t` is: | ||
/// - less than zero | ||
/// - equal to or greater than 1 | ||
fn get_position(&self, t: f32) -> Vec3; | ||
/// Sum of the lengths of all segments. | ||
/// For some shapes this may be an approximation. | ||
fn total_length(&self) -> f32; | ||
|
||
|
||
} | ||
|
||
#[derive(Debug, Clone, PartialEq, Component, Reflect)] | ||
#[reflect(Debug, Default, PartialEq)] | ||
pub enum ProceduralAnimationShape { | ||
Circle(CircleAnimation), | ||
Points(PointsAnimation), | ||
} | ||
|
||
impl Default for ProceduralAnimationShape { | ||
fn default() -> Self { | ||
Self::Circle(CircleAnimation::default()) | ||
} | ||
} | ||
|
||
|
||
|
||
impl ProceduralAnimationStrategy for ProceduralAnimationShape { | ||
fn get_position(&self, t: f32) -> Vec3 { | ||
match self { | ||
ProceduralAnimationShape::Circle(circle) => circle.get_position(t), | ||
ProceduralAnimationShape::Points(points) => points.get_position(t), | ||
} | ||
} | ||
fn total_length(&self) -> f32 { | ||
match self { | ||
ProceduralAnimationShape::Circle(circle) => circle.total_length(), | ||
ProceduralAnimationShape::Points(points) => points.total_length(), | ||
} | ||
} | ||
} | ||
|
||
|
||
#[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 get_position(&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 | ||
} | ||
} | ||
|
||
|
||
#[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 { | ||
pub fn num_segments(&self) -> usize { | ||
if self.wrap { | ||
self.points.len() | ||
} else { | ||
self.points.len() - 1 | ||
} | ||
} | ||
} | ||
|
||
impl ProceduralAnimationStrategy for PointsAnimation { | ||
fn get_position(&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 | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
crates/beet_spatial/src/procedural_animation/procedural_animation_speed.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,13 @@ | ||
use bevy::prelude::*; | ||
|
||
|
||
#[derive(Debug, Clone, PartialEq, Component, Reflect)] | ||
#[reflect(Debug, PartialEq, Default)] | ||
pub enum ProceduralAnimationSpeed { | ||
MetersPerSecond(f32), | ||
FractionPerSecond(f32), | ||
} | ||
|
||
impl Default for ProceduralAnimationSpeed { | ||
fn default() -> Self { Self::MetersPerSecond(1.0) } | ||
} |
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