Skip to content

Commit 80f294d

Browse files
committed
Some random work on animations and mathematics
1 parent 3f10610 commit 80f294d

File tree

20 files changed

+143
-144
lines changed

20 files changed

+143
-144
lines changed

Cargo.lock

Lines changed: 0 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,14 @@ bitflags = "2.1.0"
3434
common = { package = "surreal-common", path = "./common" }
3535
editor = { package = "surreal-editor", path = "./editor", optional = true }
3636

37-
# backends
37+
# framework
3838
sdl = { package = "surreal-backend-sdl", path = "./backends/sdl", optional = true }
3939

4040
# modules
4141
audio = { package = "surreal-audio", path = "./modules/audio", optional = true }
4242
graphics = { package = "surreal-graphics", path = "./modules/graphics", optional = true }
4343
input = { package = "surreal-input", path = "./modules/input", optional = true }
44-
scripting = { package = "surreal-scripting", path = "./modules/scripting", optional = true }
4544

4645
[[example]]
4746
name = "hello-world"
4847
required-features = ["sdl", "graphics"]
49-
50-
[[example]]
51-
name = "sprites"
52-
required-features = ["sdl", "graphics"]

common/src/collections.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,4 @@ pub type FastMultiMap<K, V> = MultiMap<K, V, BuildHasherDefault<rustc_hash::FxHa
3939
pub type FastAnyMap = AnyMap<BuildHasherDefault<rustc_hash::FxHasher>>;
4040

4141
/// A faster spatial hash grid that is not resilient to DoS attacks.
42-
pub type FastSpatialHashGrid<T> = SpatialHashGrid<T, BuildHasherDefault<rustc_hash::FxHasher>>;
42+
pub type FastSpatialHashMap<T> = SpatialHashMap<T, BuildHasherDefault<rustc_hash::FxHasher>>;

common/src/maths/lerp.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::Scalar;
1+
use super::{Quat, Scalar, Vec2, Vec3};
22

33
/// Allows linear interpolation of arbitrary values.
44
pub trait Lerp {
@@ -15,6 +15,34 @@ impl<T: Scalar> Lerp for T {
1515
}
1616
}
1717

18+
impl Lerp for Vec2 {
19+
#[inline(always)]
20+
fn lerp(a: Self, b: Self, t: f32) -> Self {
21+
let x = f32::lerp(a.x, b.x, t);
22+
let y = f32::lerp(a.y, b.y, t);
23+
24+
Self::new(x, y)
25+
}
26+
}
27+
28+
impl Lerp for Vec3 {
29+
#[inline(always)]
30+
fn lerp(a: Self, b: Self, t: f32) -> Self {
31+
let x = f32::lerp(a.x, b.x, t);
32+
let y = f32::lerp(a.y, b.y, t);
33+
let z = f32::lerp(a.z, b.z, t);
34+
35+
Self::new(x, y, z)
36+
}
37+
}
38+
39+
impl Lerp for Quat {
40+
#[inline(always)]
41+
fn lerp(a: Self, b: Self, t: f32) -> Self {
42+
a.slerp(b, t)
43+
}
44+
}
45+
1846
#[cfg(test)]
1947
mod tests {
2048
use super::*;

common/src/maths/linear.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,11 @@ mod planes;
1818
mod rays;
1919
mod scalars;
2020
mod vectors;
21+
22+
/// Represents a numerical space with identity constants
23+
pub trait Identity {
24+
const ZERO: Self;
25+
const ONE: Self;
26+
const MIN: Self;
27+
const MAX: Self;
28+
}

common/src/maths/linear/scalars.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign};
22

3+
use crate::Identity;
4+
35
/// Represents a scalar type that allows standard equality and arithmetic.
46
pub trait Scalar:
57
Copy
68
+ Default
9+
+ Identity
710
+ PartialOrd
811
+ PartialEq
912
+ Add<Output = Self>
@@ -16,11 +19,6 @@ pub trait Scalar:
1619
+ DivAssign
1720
+ Sized
1821
{
19-
const ZERO: Self;
20-
const ONE: Self;
21-
const MIN: Self;
22-
const MAX: Self;
23-
2422
/// Converts a value from a 32-bit floating point number.
2523
fn from_f32(value: f32) -> Self;
2624

@@ -33,13 +31,15 @@ pub trait Scalar:
3331

3432
/// Implements the numeric traits for standard purpose a numeric type.
3533
macro_rules! impl_scalar {
36-
($type:ty) => {
37-
impl Scalar for $type {
34+
($name:ty) => {
35+
impl Identity for $name {
3836
const ZERO: Self = 0 as Self;
3937
const ONE: Self = 1 as Self;
40-
const MIN: Self = <$type>::MIN;
41-
const MAX: Self = <$type>::MAX;
38+
const MIN: Self = <$name>::MIN;
39+
const MAX: Self = <$name>::MAX;
40+
}
4241

42+
impl Scalar for $name {
4343
#[inline(always)]
4444
fn from_f32(value: f32) -> Self {
4545
value as Self

common/src/maths/linear/vectors.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ pub trait Vector:
3232
Copy
3333
+ Clone
3434
+ Default
35+
+ Identity
3536
+ Add<Output = Self>
3637
+ AddAssign
3738
+ Add<Self::Scalar, Output = Self>
@@ -46,11 +47,6 @@ pub trait Vector:
4647
+ DivAssign<Self::Scalar>
4748
+ Sized
4849
{
49-
const ZERO: Self;
50-
const ONE: Self;
51-
const MIN: Self;
52-
const MAX: Self;
53-
5450
/// The type of the space that this vector is in.
5551
type Space: Space;
5652

@@ -60,12 +56,14 @@ pub trait Vector:
6056

6157
macro_rules! impl_vector {
6258
($name:ident, $space:ident, $scalar:ident) => {
63-
impl Vector for $name {
59+
impl Identity for $name {
6460
const ZERO: Self = Self::splat($scalar::ZERO);
6561
const ONE: Self = Self::splat($scalar::ONE);
6662
const MIN: Self = Self::splat($scalar::MIN);
6763
const MAX: Self = Self::splat($scalar::MAX);
64+
}
6865

66+
impl Vector for $name {
6967
type Space = $space;
7068
type Scalar = $scalar;
7169
}

common/src/maths/time/clocks.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ impl DeltaClock {
3232
let delta_time = current_time - self.last_time;
3333

3434
self.last_time = current_time;
35-
self.last_delta_time = delta_time.total_seconds().min(self.max_delta_time);
35+
self.last_delta_time = delta_time.as_seconds().min(self.max_delta_time);
3636

3737
self.last_delta_time
3838
}
@@ -47,6 +47,6 @@ impl DeltaClock {
4747
pub fn total_time(&self) -> f32 {
4848
let now = TimeStamp::now();
4949

50-
(now - self.start_time).total_seconds()
50+
(now - self.start_time).as_seconds()
5151
}
5252
}

common/src/maths/time/counters.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ impl IntervalTimer {
1919

2020
pub fn tick(&mut self, delta_time: f32) -> bool {
2121
self.time_elapsed += delta_time;
22-
self.time_elapsed >= self.interval.total_seconds()
22+
self.time_elapsed >= self.interval.as_seconds()
2323
}
2424

2525
pub fn reset(&mut self) {

common/src/maths/time/spans.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -46,28 +46,28 @@ impl TimeSpan {
4646
}
4747

4848
#[inline]
49-
pub fn total_millis(&self) -> f32 {
50-
self.total_seconds() * 1000.
49+
pub fn as_millis(&self) -> f32 {
50+
self.as_seconds() * 1000.
5151
}
5252

5353
#[inline]
54-
pub fn total_seconds(&self) -> f32 {
54+
pub fn as_seconds(&self) -> f32 {
5555
self.seconds
5656
}
5757

5858
#[inline]
59-
pub fn total_minutes(&self) -> f32 {
60-
self.total_seconds() / 60.
59+
pub fn as_minutes(&self) -> f32 {
60+
self.as_seconds() / 60.
6161
}
6262

6363
#[inline]
64-
pub fn total_hours(&self) -> f32 {
65-
self.total_minutes() / 60.
64+
pub fn as_hours(&self) -> f32 {
65+
self.as_minutes() / 60.
6666
}
6767

6868
#[inline]
69-
pub fn total_days(&self) -> f32 {
70-
self.total_hours() / 24.
69+
pub fn as_days(&self) -> f32 {
70+
self.as_hours() / 24.
7171
}
7272
}
7373

@@ -145,11 +145,11 @@ impl From<TimeSpan> for Duration {
145145
impl Display for TimeSpan {
146146
fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
147147
match self {
148-
_ if self.total_days() > 1. => write!(formatter, "{} days", self.total_days()),
149-
_ if self.total_hours() > 1. => write!(formatter, "{} hours", self.total_hours()),
150-
_ if self.total_minutes() > 1. => write!(formatter, "{} minutes", self.total_minutes()),
151-
_ if self.total_seconds() > 1. => write!(formatter, "{} seconds", self.total_seconds()),
152-
_ => write!(formatter, "{} milliseconds", self.total_millis()),
148+
_ if self.as_days() > 1. => write!(formatter, "{} days", self.as_days()),
149+
_ if self.as_hours() > 1. => write!(formatter, "{} hours", self.as_hours()),
150+
_ if self.as_minutes() > 1. => write!(formatter, "{} minutes", self.as_minutes()),
151+
_ if self.as_seconds() > 1. => write!(formatter, "{} seconds", self.as_seconds()),
152+
_ => write!(formatter, "{} milliseconds", self.as_millis()),
153153
}
154154
}
155155
}
@@ -216,35 +216,35 @@ mod tests {
216216
fn test_time_span_from_millis() {
217217
let time_span = TimeSpan::from_millis(100.0);
218218

219-
assert_eq!(time_span.total_millis(), 100.0);
219+
assert_eq!(time_span.as_millis(), 100.0);
220220
}
221221

222222
#[test]
223223
fn test_time_span_from_seconds() {
224224
let time_span = TimeSpan::from_seconds(2.5);
225225

226-
assert_eq!(time_span.total_millis(), 2500.0);
226+
assert_eq!(time_span.as_millis(), 2500.0);
227227
}
228228

229229
#[test]
230230
fn test_time_span_from_minutes() {
231231
let time_span = TimeSpan::from_minutes(1.5);
232232

233-
assert_eq!(time_span.total_millis(), 90000.0);
233+
assert_eq!(time_span.as_millis(), 90000.0);
234234
}
235235

236236
#[test]
237237
fn test_time_span_from_hours() {
238238
let time_span = TimeSpan::from_hours(0.5);
239239

240-
assert_eq!(time_span.total_millis(), 1800000.0);
240+
assert_eq!(time_span.as_millis(), 1800000.0);
241241
}
242242

243243
#[test]
244244
fn test_u8_into_time_span() {
245245
let value: u8 = 10;
246246
let time_span = value.milliseconds();
247247

248-
assert_eq!(time_span.total_millis(), 10.0);
248+
assert_eq!(time_span.as_millis(), 10.0);
249249
}
250250
}

0 commit comments

Comments
 (0)