Add global time scaling (#5752)

# Objective

- Make `Time` API more consistent.
- Support time accel/decel/pause.

## Solution

This is just the `Time` half of #3002. I was told that part isn't controversial.

- Give the "delta time" and "total elapsed time" methods `f32`, `f64`, and `Duration` variants with consistent naming.
- Implement accelerating / decelerating the passage of time.
- Implement stopping time.

---

## Changelog

- Changed `time_since_startup` to `elapsed` because `time.time_*` is just silly.
- Added `relative_speed` and `set_relative_speed` methods.
- Added `is_paused`, `pause`, `unpause` , and methods. (I'd prefer `resume`, but `unpause` matches `Timer` API.)
- Added `raw_*` variants of the "delta time" and "total elapsed time" methods.
- Added `first_update` method because there's a non-zero duration between startup and the first update.

## Migration Guide

- `time.time_since_startup()` -> `time.elapsed()`
- `time.seconds_since_startup()` -> `time.elapsed_seconds_f64()`
- `time.seconds_since_startup_wrapped_f32()` -> `time.elapsed_seconds_wrapped()`

If you aren't sure which to use, most systems should continue to use "scaled" time (e.g. `time.delta_seconds()`). The realtime "unscaled" time measurements (e.g. `time.raw_delta_seconds()`) are mostly for debugging and profiling.
This commit is contained in:
Cameron 2022-10-22 18:52:29 +00:00
parent cb5e2d84be
commit 7989cb2650
24 changed files with 637 additions and 194 deletions

View File

@ -35,12 +35,13 @@ impl FrameTimeDiagnosticsPlugin {
) { ) {
diagnostics.add_measurement(Self::FRAME_COUNT, || frame_count.0 as f64); diagnostics.add_measurement(Self::FRAME_COUNT, || frame_count.0 as f64);
if time.delta_seconds_f64() == 0.0 { let delta_seconds = time.raw_delta_seconds_f64();
if delta_seconds == 0.0 {
return; return;
} }
diagnostics.add_measurement(Self::FRAME_TIME, || time.delta_seconds_f64() * 1000.); diagnostics.add_measurement(Self::FRAME_TIME, || delta_seconds * 1000.0);
diagnostics.add_measurement(Self::FPS, || 1.0 / time.delta_seconds_f64()); diagnostics.add_measurement(Self::FPS, || 1.0 / delta_seconds);
} }
} }

View File

@ -85,7 +85,7 @@ impl LogDiagnosticsPlugin {
time: Res<Time>, time: Res<Time>,
diagnostics: Res<Diagnostics>, diagnostics: Res<Diagnostics>,
) { ) {
if state.timer.tick(time.delta()).finished() { if state.timer.tick(time.raw_delta()).finished() {
if let Some(ref filter) = state.filter { if let Some(ref filter) = state.filter {
for diagnostic in filter.iter().flat_map(|id| { for diagnostic in filter.iter().flat_map(|id| {
diagnostics diagnostics
@ -110,7 +110,7 @@ impl LogDiagnosticsPlugin {
time: Res<Time>, time: Res<Time>,
diagnostics: Res<Diagnostics>, diagnostics: Res<Diagnostics>,
) { ) {
if state.timer.tick(time.delta()).finished() { if state.timer.tick(time.raw_delta()).finished() {
if let Some(ref filter) = state.filter { if let Some(ref filter) = state.filter {
for diagnostic in filter.iter().flat_map(|id| { for diagnostic in filter.iter().flat_map(|id| {
diagnostics diagnostics

View File

@ -57,7 +57,7 @@ fn prepare_globals_buffer(
frame_count: Res<FrameCount>, frame_count: Res<FrameCount>,
) { ) {
let buffer = globals_buffer.buffer.get_mut(); let buffer = globals_buffer.buffer.get_mut();
buffer.time = time.seconds_since_startup_wrapped_f32(); buffer.time = time.elapsed_seconds_wrapped();
buffer.delta_time = time.delta_seconds(); buffer.delta_time = time.delta_seconds();
buffer.frame_count = frame_count.0; buffer.frame_count = frame_count.0;

View File

@ -2,55 +2,92 @@ use bevy_ecs::{reflect::ReflectResource, system::Resource};
use bevy_reflect::{FromReflect, Reflect}; use bevy_reflect::{FromReflect, Reflect};
use bevy_utils::{Duration, Instant}; use bevy_utils::{Duration, Instant};
const SECONDS_PER_HOUR: u64 = 60 * 60; /// Tracks how much time has advanced (and also how much real time has elapsed) since
/// the previous app update and since the app was started.
/// Tracks elapsed time since the last update and since the App has started
#[derive(Resource, Reflect, FromReflect, Debug, Clone)] #[derive(Resource, Reflect, FromReflect, Debug, Clone)]
#[reflect(Resource)] #[reflect(Resource)]
pub struct Time { pub struct Time {
delta: Duration,
last_update: Option<Instant>,
delta_seconds_f64: f64,
delta_seconds: f32,
seconds_since_startup: f64,
time_since_startup: Duration,
startup: Instant, startup: Instant,
/// The maximum period before [`Time::seconds_since_startup_wrapped_f32`] wraps to 0 first_update: Option<Instant>,
/// last_update: Option<Instant>,
/// Defaults to 1 hour relative_speed: f64, // using `f64` instead of `f32` to minimize drift from rounding errors
pub wrap_period: Duration, paused: bool,
delta: Duration,
delta_seconds: f32,
delta_seconds_f64: f64,
elapsed: Duration,
elapsed_seconds: f32,
elapsed_seconds_f64: f64,
raw_delta: Duration,
raw_delta_seconds: f32,
raw_delta_seconds_f64: f64,
raw_elapsed: Duration,
raw_elapsed_seconds: f32,
raw_elapsed_seconds_f64: f64,
// wrapping
wrap_period: Duration,
elapsed_wrapped: Duration,
elapsed_seconds_wrapped: f32,
elapsed_seconds_wrapped_f64: f64,
raw_elapsed_wrapped: Duration,
raw_elapsed_seconds_wrapped: f32,
raw_elapsed_seconds_wrapped_f64: f64,
} }
impl Default for Time { impl Default for Time {
fn default() -> Time { fn default() -> Self {
Time { Self {
delta: Duration::from_secs(0),
last_update: None,
startup: Instant::now(), startup: Instant::now(),
delta_seconds_f64: 0.0, first_update: None,
seconds_since_startup: 0.0, last_update: None,
time_since_startup: Duration::from_secs(0), relative_speed: 1.0,
paused: false,
delta: Duration::ZERO,
delta_seconds: 0.0, delta_seconds: 0.0,
wrap_period: Duration::from_secs(SECONDS_PER_HOUR), delta_seconds_f64: 0.0,
elapsed: Duration::ZERO,
elapsed_seconds: 0.0,
elapsed_seconds_f64: 0.0,
raw_delta: Duration::ZERO,
raw_delta_seconds: 0.0,
raw_delta_seconds_f64: 0.0,
raw_elapsed: Duration::ZERO,
raw_elapsed_seconds: 0.0,
raw_elapsed_seconds_f64: 0.0,
wrap_period: Duration::from_secs(3600), // 1 hour
elapsed_wrapped: Duration::ZERO,
elapsed_seconds_wrapped: 0.0,
elapsed_seconds_wrapped_f64: 0.0,
raw_elapsed_wrapped: Duration::ZERO,
raw_elapsed_seconds_wrapped: 0.0,
raw_elapsed_seconds_wrapped_f64: 0.0,
} }
} }
} }
impl Time { impl Time {
/// Updates the internal time measurements. /// Constructs a new `Time` instance with a specific startup `Instant`.
/// pub fn new(startup: Instant) -> Self {
/// Calling this method on the [`Time`] resource as part of your app will most likely result in Self {
/// inaccurate timekeeping, as the resource is ordinarily managed by the startup,
/// [`TimePlugin`](crate::TimePlugin). ..Default::default()
pub fn update(&mut self) { }
self.update_with_instant(Instant::now());
} }
/// Update time with a specified [`Instant`] /// Updates the internal time measurements.
/// ///
/// This method is provided for use in tests. Calling this method on the [`Time`] resource as /// Calling this method as part of your app will most likely result in inaccurate timekeeping,
/// part of your app will most likely result in inaccurate timekeeping, as the resource is /// as the `Time` resource is ordinarily managed by the [`TimePlugin`](crate::TimePlugin).
/// ordinarily managed by the [`TimePlugin`](crate::TimePlugin). pub fn update(&mut self) {
let now = Instant::now();
self.update_with_instant(now);
}
/// Updates time with a specified [`Instant`].
///
/// This method is provided for use in tests. Calling this method as part of your app will most
/// likely result in inaccurate timekeeping, as the `Time` resource is ordinarily managed by the
/// [`TimePlugin`](crate::TimePlugin).
/// ///
/// # Examples /// # Examples
/// ///
@ -99,75 +136,283 @@ impl Time {
/// } /// }
/// ``` /// ```
pub fn update_with_instant(&mut self, instant: Instant) { pub fn update_with_instant(&mut self, instant: Instant) {
if let Some(last_update) = self.last_update { let raw_delta = instant - self.last_update.unwrap_or(self.startup);
self.delta = instant - last_update; let delta = if self.paused {
self.delta_seconds_f64 = self.delta.as_secs_f64(); Duration::ZERO
} else if self.relative_speed != 1.0 {
raw_delta.mul_f64(self.relative_speed)
} else {
// avoid rounding errors at normal speed
raw_delta
};
if self.last_update.is_some() {
self.delta = delta;
self.delta_seconds = self.delta.as_secs_f32(); self.delta_seconds = self.delta.as_secs_f32();
self.delta_seconds_f64 = self.delta.as_secs_f64();
self.raw_delta = raw_delta;
self.raw_delta_seconds = self.raw_delta.as_secs_f32();
self.raw_delta_seconds_f64 = self.raw_delta.as_secs_f64();
} else {
self.first_update = Some(instant);
} }
self.time_since_startup = instant - self.startup; self.elapsed += delta;
self.seconds_since_startup = self.time_since_startup.as_secs_f64(); self.elapsed_seconds = self.elapsed.as_secs_f32();
self.elapsed_seconds_f64 = self.elapsed.as_secs_f64();
self.raw_elapsed += raw_delta;
self.raw_elapsed_seconds = self.raw_elapsed.as_secs_f32();
self.raw_elapsed_seconds_f64 = self.raw_elapsed.as_secs_f64();
self.elapsed_wrapped = duration_div_rem(self.elapsed, self.wrap_period).1;
self.elapsed_seconds_wrapped = self.elapsed_wrapped.as_secs_f32();
self.elapsed_seconds_wrapped_f64 = self.elapsed_wrapped.as_secs_f64();
self.raw_elapsed_wrapped = duration_div_rem(self.raw_elapsed, self.wrap_period).1;
self.raw_elapsed_seconds_wrapped = self.raw_elapsed_wrapped.as_secs_f32();
self.raw_elapsed_seconds_wrapped_f64 = self.raw_elapsed_wrapped.as_secs_f64();
self.last_update = Some(instant); self.last_update = Some(instant);
} }
/// The delta between the current tick and last tick as a [`Duration`] /// Returns the [`Instant`] the app was started.
#[inline]
pub fn delta(&self) -> Duration {
self.delta
}
/// The delta between the current and last tick as [`f32`] seconds
#[inline]
pub fn delta_seconds(&self) -> f32 {
self.delta_seconds
}
/// The delta between the current and last tick as [`f64`] seconds
#[inline]
pub fn delta_seconds_f64(&self) -> f64 {
self.delta_seconds_f64
}
/// The time from startup to the last update in seconds
///
/// If you intend to cast this to an `f32` value, note that this value is monotonically increasing,
/// and that its precision as an `f32` will noticeably degrade over time (in a matter of hours).
/// If that precision loss is unacceptable, you should use [`Time::seconds_since_startup_wrapped_f32`],
/// which will return the time from startup modulo a wrapping period.
#[inline]
pub fn seconds_since_startup(&self) -> f64 {
self.seconds_since_startup
}
/// The time from startup to the last update, modulo the [`Time::wrap_period`], in seconds.
///
/// Time from startup is a monotonically increasing value and so its precision when read as an `f32`
/// will noticeably degrade over time, which causes issues for some uses, e.g. shaders.
/// This method avoids noticeable degradation by limiting the values to a much smaller range.
///
/// The default wrapping period is one hour.
#[inline]
pub fn seconds_since_startup_wrapped_f32(&self) -> f32 {
(self.seconds_since_startup % self.wrap_period.as_secs_f64()) as f32
}
/// The [`Instant`] the app was started
#[inline] #[inline]
pub fn startup(&self) -> Instant { pub fn startup(&self) -> Instant {
self.startup self.startup
} }
/// The [`Instant`] when [`Time::update`] was last called, if it exists /// Returns the [`Instant`] when [`update`](#method.update) was first called, if it exists.
#[inline]
pub fn first_update(&self) -> Option<Instant> {
self.first_update
}
/// Returns the [`Instant`] when [`update`](#method.update) was last called, if it exists.
#[inline] #[inline]
pub fn last_update(&self) -> Option<Instant> { pub fn last_update(&self) -> Option<Instant> {
self.last_update self.last_update
} }
/// The [`Duration`] from startup to the last update /// Returns how much time has advanced since the last [`update`](#method.update), as a [`Duration`].
#[inline] #[inline]
pub fn time_since_startup(&self) -> Duration { pub fn delta(&self) -> Duration {
self.time_since_startup self.delta
} }
/// Returns how much time has advanced since the last [`update`](#method.update), as [`f32`] seconds.
#[inline]
pub fn delta_seconds(&self) -> f32 {
self.delta_seconds
}
/// Returns how much time has advanced since the last [`update`](#method.update), as [`f64`] seconds.
#[inline]
pub fn delta_seconds_f64(&self) -> f64 {
self.delta_seconds_f64
}
/// Returns how much time has advanced since [`startup`](#method.startup), as [`Duration`].
#[inline]
pub fn elapsed(&self) -> Duration {
self.elapsed
}
/// Returns how much time has advanced since [`startup`](#method.startup), as [`f32`] seconds.
///
/// **Note:** This is a monotonically increasing value. It's precision will degrade over time.
/// If you need an `f32` but that precision loss is unacceptable,
/// use [`elapsed_seconds_wrapped`](#method.elapsed_seconds_wrapped).
#[inline]
pub fn elapsed_seconds(&self) -> f32 {
self.elapsed_seconds
}
/// Returns how much time has advanced since [`startup`](#method.startup), as [`f64`] seconds.
#[inline]
pub fn elapsed_seconds_f64(&self) -> f64 {
self.elapsed_seconds_f64
}
/// Returns how much time has advanced since [`startup`](#method.startup) modulo
/// the [`wrap_period`](#method.wrap_period), as [`Duration`].
#[inline]
pub fn elapsed_wrapped(&self) -> Duration {
self.elapsed_wrapped
}
/// Returns how much time has advanced since [`startup`](#method.startup) modulo
/// the [`wrap_period`](#method.wrap_period), as [`f32`] seconds.
///
/// This method is intended for applications (e.g. shaders) that require an [`f32`] value but
/// suffer from the gradual precision loss of [`elapsed_seconds`](#method.elapsed_seconds).
#[inline]
pub fn elapsed_seconds_wrapped(&self) -> f32 {
self.elapsed_seconds_wrapped
}
/// Returns how much time has advanced since [`startup`](#method.startup) modulo
/// the [`wrap_period`](#method.wrap_period), as [`f64`] seconds.
#[inline]
pub fn elapsed_seconds_wrapped_f64(&self) -> f64 {
self.elapsed_seconds_wrapped_f64
}
/// Returns the exact clock time elapsed since the last [`update`](#method.update), as a [`Duration`].
#[inline]
pub fn raw_delta(&self) -> Duration {
self.raw_delta
}
/// Returns the exact clock time elapsed since the last [`update`](#method.update), as [`f32`] seconds.
#[inline]
pub fn raw_delta_seconds(&self) -> f32 {
self.raw_delta_seconds
}
/// Returns the exact clock time elapsed since the last [`update`](#method.update), as [`f64`] seconds.
#[inline]
pub fn raw_delta_seconds_f64(&self) -> f64 {
self.raw_delta_seconds_f64
}
/// Returns the exact clock time elapsed since [`startup`](#method.startup), as [`Duration`].
#[inline]
pub fn raw_elapsed(&self) -> Duration {
self.raw_elapsed
}
/// Returns the exact clock time elapsed since [`startup`](#method.startup), as [`f32`] seconds.
///
/// **Note:** This is a monotonically increasing value. It's precision will degrade over time.
/// If you need an `f32` but that precision loss is unacceptable,
/// use [`raw_elapsed_seconds_wrapped`](#method.raw_elapsed_seconds_wrapped).
#[inline]
pub fn raw_elapsed_seconds(&self) -> f32 {
self.raw_elapsed_seconds
}
/// Returns the exact clock time elapsed since [`startup`](#method.startup), as [`f64`] seconds.
#[inline]
pub fn raw_elapsed_seconds_f64(&self) -> f64 {
self.raw_elapsed_seconds_f64
}
/// Returns the exact clock time elapsed since [`startup`](#method.startup) modulo
/// the [`wrap_period`](#method.wrap_period), as [`Duration`].
#[inline]
pub fn raw_elapsed_wrapped(&self) -> Duration {
self.raw_elapsed_wrapped
}
/// Returns the exact clock time elapsed since [`startup`](#method.startup) modulo
/// the [`wrap_period`](#method.wrap_period), as [`f32`] seconds.
///
/// This method is intended for applications (e.g. shaders) that require an [`f32`] value but
/// suffer from the gradual precision loss of [`raw_elapsed_seconds`](#method.raw_elapsed_seconds).
#[inline]
pub fn raw_elapsed_seconds_wrapped(&self) -> f32 {
self.raw_elapsed_seconds_wrapped
}
/// Returns the exact clock time elapsed since [`startup`](#method.startup) modulo
/// the [`wrap_period`](#method.wrap_period), as [`f64`] seconds.
#[inline]
pub fn raw_elapsed_seconds_wrapped_f64(&self) -> f64 {
self.raw_elapsed_seconds_wrapped_f64
}
/// Returns the modulus used to calculate [`elapsed_wrapped`](#method.elapsed_wrapped) and
/// [`raw_elapsed_wrapped`](#method.raw_elapsed_wrapped).
///
/// **Note:** The default modulus is one hour.
#[inline]
pub fn wrap_period(&self) -> Duration {
self.wrap_period
}
/// Sets the modulus used to calculate [`elapsed_wrapped`](#method.elapsed_wrapped) and
/// [`raw_elapsed_wrapped`](#method.raw_elapsed_wrapped).
///
/// # Panics
///
/// Panics if `wrap_period` is zero.
#[inline]
pub fn set_wrap_period(&mut self, wrap_period: Duration) {
assert!(wrap_period != Duration::ZERO, "division by zero");
self.wrap_period = wrap_period;
}
/// Returns the rate that time advances relative to real time, as [`f32`].
/// You might recognize this as "time scaling" or "time dilation" in other engines.
///
/// **Note:** This function will return zero when time is paused.
#[inline]
pub fn relative_speed(&self) -> f32 {
self.relative_speed_f64() as f32
}
/// Returns the rate that time advances relative to real time, as [`f64`].
/// You might recognize this as "time scaling" or "time dilation" in other engines.
///
/// **Note:** This function will return zero when time is paused.
#[inline]
pub fn relative_speed_f64(&self) -> f64 {
if self.paused {
0.0
} else {
self.relative_speed
}
}
/// Sets the rate that time advances relative to real time, given as an [`f32`].
///
/// For example, if set to `2.0`, time will advance twice as fast as your system clock.
///
/// # Panics
///
/// Panics if `ratio` is negative or not finite.
#[inline]
pub fn set_relative_speed(&mut self, ratio: f32) {
self.set_relative_speed_f64(ratio as f64);
}
/// Sets the rate that time advances relative to real time, given as an [`f64`].
///
/// For example, if set to `2.0`, time will advance twice as fast as your system clock.
///
/// # Panics
///
/// Panics if `ratio` is negative or not finite.
#[inline]
pub fn set_relative_speed_f64(&mut self, ratio: f64) {
assert!(ratio.is_finite(), "tried to go infinitely fast");
assert!(ratio.is_sign_positive(), "tried to go back in time");
self.relative_speed = ratio;
}
/// Stops time, preventing it from advancing until resumed. Does not affect raw measurements.
#[inline]
pub fn pause(&mut self) {
self.paused = true;
}
/// Resumes time if paused.
#[inline]
pub fn unpause(&mut self) {
self.paused = false;
}
/// Returns `true` if time has been paused.
#[inline]
pub fn is_paused(&self) -> bool {
self.paused
}
}
fn duration_div_rem(dividend: Duration, divisor: Duration) -> (u32, Duration) {
// `Duration` does not have a built-in modulo operation
let quotient = (dividend.as_nanos() / divisor.as_nanos()) as u32;
let remainder = dividend - (quotient * divisor);
(quotient, remainder)
} }
#[cfg(test)] #[cfg(test)]
@ -176,77 +421,119 @@ mod tests {
use super::Time; use super::Time;
use bevy_utils::{Duration, Instant}; use bevy_utils::{Duration, Instant};
fn assert_float_eq(a: f32, b: f32) {
assert!((a - b).abs() <= f32::EPSILON, "{a} != {b}");
}
#[test] #[test]
fn update_test() { fn update_test() {
let start_instant = Instant::now(); let start_instant = Instant::now();
let mut time = Time::new(start_instant);
// Create a `Time` for testing // Ensure `time` was constructed correctly.
let mut time = Time {
startup: start_instant,
..Default::default()
};
// Ensure `time` was constructed correctly
assert_eq!(time.delta(), Duration::from_secs(0));
assert_eq!(time.last_update(), None);
assert_eq!(time.startup(), start_instant); assert_eq!(time.startup(), start_instant);
assert_eq!(time.delta_seconds_f64(), 0.0); assert_eq!(time.first_update(), None);
assert_eq!(time.seconds_since_startup(), 0.0); assert_eq!(time.last_update(), None);
assert_eq!(time.time_since_startup(), Duration::from_secs(0)); assert_eq!(time.relative_speed(), 1.0);
assert_eq!(time.delta(), Duration::ZERO);
assert_eq!(time.delta_seconds(), 0.0); assert_eq!(time.delta_seconds(), 0.0);
assert_eq!(time.seconds_since_startup_wrapped_f32(), 0.0); assert_eq!(time.delta_seconds_f64(), 0.0);
assert_eq!(time.raw_delta(), Duration::ZERO);
assert_eq!(time.raw_delta_seconds(), 0.0);
assert_eq!(time.raw_delta_seconds_f64(), 0.0);
assert_eq!(time.elapsed(), Duration::ZERO);
assert_eq!(time.elapsed_seconds(), 0.0);
assert_eq!(time.elapsed_seconds_f64(), 0.0);
assert_eq!(time.raw_elapsed(), Duration::ZERO);
assert_eq!(time.raw_elapsed_seconds(), 0.0);
assert_eq!(time.raw_elapsed_seconds_f64(), 0.0);
// Update `time` and check results // Update `time` and check results.
// The first update to `time` normally happens before other systems have run,
// so the first delta doesn't appear until the second update.
let first_update_instant = Instant::now(); let first_update_instant = Instant::now();
time.update_with_instant(first_update_instant); time.update_with_instant(first_update_instant);
assert_eq!(time.delta(), Duration::from_secs(0)); assert_eq!(time.startup(), start_instant);
assert_eq!(time.first_update(), Some(first_update_instant));
assert_eq!(time.last_update(), Some(first_update_instant)); assert_eq!(time.last_update(), Some(first_update_instant));
assert_eq!(time.startup(), start_instant); assert_eq!(time.relative_speed(), 1.0);
assert_eq!(time.delta_seconds_f64(), 0.0); assert_eq!(time.delta(), Duration::ZERO);
assert_eq!(
time.seconds_since_startup(),
(first_update_instant - start_instant).as_secs_f64()
);
assert_eq!(
time.time_since_startup(),
(first_update_instant - start_instant)
);
assert_eq!(time.delta_seconds(), 0.0); assert_eq!(time.delta_seconds(), 0.0);
assert_float_eq( assert_eq!(time.delta_seconds_f64(), 0.0);
time.seconds_since_startup_wrapped_f32(), assert_eq!(time.raw_delta(), Duration::ZERO);
time.seconds_since_startup() as f32, assert_eq!(time.raw_delta_seconds(), 0.0);
assert_eq!(time.raw_delta_seconds_f64(), 0.0);
assert_eq!(time.elapsed(), first_update_instant - start_instant,);
assert_eq!(
time.elapsed_seconds(),
(first_update_instant - start_instant).as_secs_f32(),
);
assert_eq!(
time.elapsed_seconds_f64(),
(first_update_instant - start_instant).as_secs_f64(),
);
assert_eq!(time.raw_elapsed(), first_update_instant - start_instant,);
assert_eq!(
time.raw_elapsed_seconds(),
(first_update_instant - start_instant).as_secs_f32(),
);
assert_eq!(
time.raw_elapsed_seconds_f64(),
(first_update_instant - start_instant).as_secs_f64(),
); );
// Update `time` again and check results // Update `time` again and check results.
// At this point its safe to use time.delta().
let second_update_instant = Instant::now(); let second_update_instant = Instant::now();
time.update_with_instant(second_update_instant); time.update_with_instant(second_update_instant);
assert_eq!(time.delta(), second_update_instant - first_update_instant);
assert_eq!(time.last_update(), Some(second_update_instant));
assert_eq!(time.startup(), start_instant); assert_eq!(time.startup(), start_instant);
// At this point its safe to use time.delta as a valid value assert_eq!(time.first_update(), Some(first_update_instant));
// because it's been previously verified to be correct assert_eq!(time.last_update(), Some(second_update_instant));
assert_eq!(time.delta_seconds_f64(), time.delta().as_secs_f64()); assert_eq!(time.relative_speed(), 1.0);
assert_eq!(time.delta(), second_update_instant - first_update_instant);
assert_eq!( assert_eq!(
time.seconds_since_startup(), time.delta_seconds(),
(second_update_instant - start_instant).as_secs_f64() (second_update_instant - first_update_instant).as_secs_f32(),
); );
assert_eq!( assert_eq!(
time.time_since_startup(), time.delta_seconds_f64(),
(second_update_instant - start_instant) (second_update_instant - first_update_instant).as_secs_f64(),
); );
assert_eq!(time.delta_seconds(), time.delta().as_secs_f32()); assert_eq!(
assert_float_eq( time.raw_delta(),
time.seconds_since_startup_wrapped_f32(), second_update_instant - first_update_instant,
time.seconds_since_startup() as f32, );
assert_eq!(
time.raw_delta_seconds(),
(second_update_instant - first_update_instant).as_secs_f32(),
);
assert_eq!(
time.raw_delta_seconds_f64(),
(second_update_instant - first_update_instant).as_secs_f64(),
);
assert_eq!(time.elapsed(), second_update_instant - start_instant,);
assert_eq!(
time.elapsed_seconds(),
(second_update_instant - start_instant).as_secs_f32(),
);
assert_eq!(
time.elapsed_seconds_f64(),
(second_update_instant - start_instant).as_secs_f64(),
);
assert_eq!(time.raw_elapsed(), second_update_instant - start_instant,);
assert_eq!(
time.raw_elapsed_seconds(),
(second_update_instant - start_instant).as_secs_f32(),
);
assert_eq!(
time.raw_elapsed_seconds_f64(),
(second_update_instant - start_instant).as_secs_f64(),
); );
} }
#[test] #[test]
fn update_wrapping() { fn wrapping_test() {
let start_instant = Instant::now(); let start_instant = Instant::now();
let mut time = Time { let mut time = Time {
@ -255,22 +542,171 @@ mod tests {
..Default::default() ..Default::default()
}; };
assert_eq!(time.seconds_since_startup_wrapped_f32(), 0.0); assert_eq!(time.elapsed_seconds_wrapped(), 0.0);
time.update_with_instant(start_instant + Duration::from_secs(1)); time.update_with_instant(start_instant + Duration::from_secs(1));
assert_float_eq(time.seconds_since_startup_wrapped_f32(), 1.0); assert_float_eq(time.elapsed_seconds_wrapped(), 1.0);
time.update_with_instant(start_instant + Duration::from_secs(2)); time.update_with_instant(start_instant + Duration::from_secs(2));
assert_float_eq(time.seconds_since_startup_wrapped_f32(), 2.0); assert_float_eq(time.elapsed_seconds_wrapped(), 2.0);
time.update_with_instant(start_instant + Duration::from_secs(3)); time.update_with_instant(start_instant + Duration::from_secs(3));
assert_float_eq(time.seconds_since_startup_wrapped_f32(), 0.0); assert_float_eq(time.elapsed_seconds_wrapped(), 0.0);
time.update_with_instant(start_instant + Duration::from_secs(4)); time.update_with_instant(start_instant + Duration::from_secs(4));
assert_float_eq(time.seconds_since_startup_wrapped_f32(), 1.0); assert_float_eq(time.elapsed_seconds_wrapped(), 1.0);
} }
fn assert_float_eq(a: f32, b: f32) { #[test]
assert!((a - b).abs() <= f32::EPSILON, "{a} != {b}"); fn relative_speed_test() {
let start_instant = Instant::now();
let mut time = Time::new(start_instant);
let first_update_instant = Instant::now();
time.update_with_instant(first_update_instant);
// Update `time` again and check results.
// At this point its safe to use time.delta().
let second_update_instant = Instant::now();
time.update_with_instant(second_update_instant);
assert_eq!(time.startup(), start_instant);
assert_eq!(time.first_update(), Some(first_update_instant));
assert_eq!(time.last_update(), Some(second_update_instant));
assert_eq!(time.relative_speed(), 1.0);
assert_eq!(time.delta(), second_update_instant - first_update_instant);
assert_eq!(
time.delta_seconds(),
(second_update_instant - first_update_instant).as_secs_f32(),
);
assert_eq!(
time.delta_seconds_f64(),
(second_update_instant - first_update_instant).as_secs_f64(),
);
assert_eq!(
time.raw_delta(),
second_update_instant - first_update_instant,
);
assert_eq!(
time.raw_delta_seconds(),
(second_update_instant - first_update_instant).as_secs_f32(),
);
assert_eq!(
time.raw_delta_seconds_f64(),
(second_update_instant - first_update_instant).as_secs_f64(),
);
assert_eq!(time.elapsed(), second_update_instant - start_instant,);
assert_eq!(
time.elapsed_seconds(),
(second_update_instant - start_instant).as_secs_f32(),
);
assert_eq!(
time.elapsed_seconds_f64(),
(second_update_instant - start_instant).as_secs_f64(),
);
assert_eq!(time.raw_elapsed(), second_update_instant - start_instant,);
assert_eq!(
time.raw_elapsed_seconds(),
(second_update_instant - start_instant).as_secs_f32(),
);
assert_eq!(
time.raw_elapsed_seconds_f64(),
(second_update_instant - start_instant).as_secs_f64(),
);
// Make app time advance at 2x the rate of your system clock.
time.set_relative_speed(2.0);
// Update `time` again 1 second later.
let elapsed = Duration::from_secs(1);
let third_update_instant = second_update_instant + elapsed;
time.update_with_instant(third_update_instant);
// Since app is advancing 2x your system clock, expect time
// to have advanced by twice the amount of real time elapsed.
assert_eq!(time.startup(), start_instant);
assert_eq!(time.first_update(), Some(first_update_instant));
assert_eq!(time.last_update(), Some(third_update_instant));
assert_eq!(time.relative_speed(), 2.0);
assert_eq!(time.delta(), elapsed.mul_f32(2.0));
assert_eq!(time.delta_seconds(), elapsed.mul_f32(2.0).as_secs_f32());
assert_eq!(time.delta_seconds_f64(), elapsed.mul_f32(2.0).as_secs_f64());
assert_eq!(time.raw_delta(), elapsed);
assert_eq!(time.raw_delta_seconds(), elapsed.as_secs_f32());
assert_eq!(time.raw_delta_seconds_f64(), elapsed.as_secs_f64());
assert_eq!(
time.elapsed(),
second_update_instant - start_instant + elapsed.mul_f32(2.0),
);
assert_eq!(
time.elapsed_seconds(),
(second_update_instant - start_instant + elapsed.mul_f32(2.0)).as_secs_f32(),
);
assert_eq!(
time.elapsed_seconds_f64(),
(second_update_instant - start_instant + elapsed.mul_f32(2.0)).as_secs_f64(),
);
assert_eq!(
time.raw_elapsed(),
second_update_instant - start_instant + elapsed,
);
assert_eq!(
time.raw_elapsed_seconds(),
(second_update_instant - start_instant + elapsed).as_secs_f32(),
);
assert_eq!(
time.raw_elapsed_seconds_f64(),
(second_update_instant - start_instant + elapsed).as_secs_f64(),
);
}
#[test]
fn pause_test() {
let start_instant = Instant::now();
let mut time = Time::new(start_instant);
let first_update_instant = Instant::now();
time.update_with_instant(first_update_instant);
assert!(!time.is_paused());
assert_eq!(time.relative_speed(), 1.0);
time.pause();
assert!(time.is_paused());
assert_eq!(time.relative_speed(), 0.0);
let second_update_instant = Instant::now();
time.update_with_instant(second_update_instant);
assert_eq!(time.startup(), start_instant);
assert_eq!(time.first_update(), Some(first_update_instant));
assert_eq!(time.last_update(), Some(second_update_instant));
assert_eq!(time.delta(), Duration::ZERO);
assert_eq!(
time.raw_delta(),
second_update_instant - first_update_instant,
);
assert_eq!(time.elapsed(), first_update_instant - start_instant);
assert_eq!(time.raw_elapsed(), second_update_instant - start_instant);
time.unpause();
assert!(!time.is_paused());
assert_eq!(time.relative_speed(), 1.0);
let third_update_instant = Instant::now();
time.update_with_instant(third_update_instant);
assert_eq!(time.startup(), start_instant);
assert_eq!(time.first_update(), Some(first_update_instant));
assert_eq!(time.last_update(), Some(third_update_instant));
assert_eq!(time.delta(), third_update_instant - second_update_instant);
assert_eq!(
time.raw_delta(),
third_update_instant - second_update_instant,
);
assert_eq!(
time.elapsed(),
(third_update_instant - second_update_instant) + (first_update_instant - start_instant),
);
assert_eq!(time.raw_elapsed(), third_update_instant - start_instant);
} }
} }

View File

@ -96,8 +96,8 @@ fn animate_translation(
mut query: Query<&mut Transform, (With<Text>, With<AnimateTranslation>)>, mut query: Query<&mut Transform, (With<Text>, With<AnimateTranslation>)>,
) { ) {
for mut transform in &mut query { for mut transform in &mut query {
transform.translation.x = 100.0 * time.seconds_since_startup().sin() as f32 - 400.0; transform.translation.x = 100.0 * time.elapsed_seconds().sin() - 400.0;
transform.translation.y = 100.0 * time.seconds_since_startup().cos() as f32; transform.translation.y = 100.0 * time.elapsed_seconds().cos();
} }
} }
@ -106,7 +106,7 @@ fn animate_rotation(
mut query: Query<&mut Transform, (With<Text>, With<AnimateRotation>)>, mut query: Query<&mut Transform, (With<Text>, With<AnimateRotation>)>,
) { ) {
for mut transform in &mut query { for mut transform in &mut query {
transform.rotation = Quat::from_rotation_z(time.seconds_since_startup().cos() as f32); transform.rotation = Quat::from_rotation_z(time.elapsed_seconds().cos());
} }
} }
@ -118,6 +118,6 @@ fn animate_scale(
// rendered quad, resulting in a pixellated look. // rendered quad, resulting in a pixellated look.
for mut transform in &mut query { for mut transform in &mut query {
transform.translation = Vec3::new(400.0, 0.0, 0.0); transform.translation = Vec3::new(400.0, 0.0, 0.0);
transform.scale = Vec3::splat((time.seconds_since_startup().sin() as f32 + 1.1) * 2.0); transform.scale = Vec3::splat((time.elapsed_seconds().sin() + 1.1) * 2.0);
} }
} }

View File

@ -1,6 +1,6 @@
//! Loads and renders a glTF file as a scene. //! Loads and renders a glTF file as a scene.
use std::f32::consts::PI; use std::f32::consts::*;
use bevy::prelude::*; use bevy::prelude::*;
@ -52,8 +52,8 @@ fn animate_light_direction(
transform.rotation = Quat::from_euler( transform.rotation = Quat::from_euler(
EulerRot::ZYX, EulerRot::ZYX,
0.0, 0.0,
time.seconds_since_startup() as f32 * PI / 5.0, time.elapsed_seconds() * PI / 5.0,
-PI / 4., -FRAC_PI_4,
); );
} }
} }

View File

@ -98,16 +98,16 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
}); });
} }
const CUBEMAP_SWAP_DELAY: f64 = 3.0; const CUBEMAP_SWAP_DELAY: f32 = 3.0;
fn cycle_cubemap_asset( fn cycle_cubemap_asset(
time: Res<Time>, time: Res<Time>,
mut next_swap: Local<f64>, mut next_swap: Local<f32>,
mut cubemap: ResMut<Cubemap>, mut cubemap: ResMut<Cubemap>,
asset_server: Res<AssetServer>, asset_server: Res<AssetServer>,
render_device: Res<RenderDevice>, render_device: Res<RenderDevice>,
) { ) {
let now = time.seconds_since_startup(); let now = time.elapsed_seconds();
if *next_swap == 0.0 { if *next_swap == 0.0 {
*next_swap = now + CUBEMAP_SWAP_DELAY; *next_swap = now + CUBEMAP_SWAP_DELAY;
return; return;

View File

@ -1,4 +1,4 @@
use std::f32::consts::PI; use std::f32::consts::*;
use bevy::{ use bevy::{
diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin}, diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
@ -127,11 +127,11 @@ fn light_sway(time: Res<Time>, mut query: Query<(&mut Transform, &mut SpotLight)
for (mut transform, mut angles) in query.iter_mut() { for (mut transform, mut angles) in query.iter_mut() {
transform.rotation = Quat::from_euler( transform.rotation = Quat::from_euler(
EulerRot::XYZ, EulerRot::XYZ,
-PI / 2. + (time.seconds_since_startup() * 0.67 * 3.0).sin() as f32 * 0.5, -FRAC_PI_2 + (time.elapsed_seconds() * 0.67 * 3.0).sin() * 0.5,
(time.seconds_since_startup() * 3.0).sin() as f32 * 0.5, (time.elapsed_seconds() * 3.0).sin() * 0.5,
0.0, 0.0,
); );
let angle = ((time.seconds_since_startup() * 1.2).sin() as f32 + 1.0) * (PI / 4. - 0.1); let angle = ((time.elapsed_seconds() * 1.2).sin() + 1.0) * (FRAC_PI_4 - 0.1);
angles.inner_angle = angle * 0.8; angles.inner_angle = angle * 0.8;
angles.outer_angle = angle; angles.outer_angle = angle;
} }

View File

@ -103,7 +103,7 @@ fn setup(
/// when the alpha value goes back below the threshold. /// when the alpha value goes back below the threshold.
/// - `Blend`: Object fades in and out smoothly. /// - `Blend`: Object fades in and out smoothly.
pub fn fade_transparency(time: Res<Time>, mut materials: ResMut<Assets<StandardMaterial>>) { pub fn fade_transparency(time: Res<Time>, mut materials: ResMut<Assets<StandardMaterial>>) {
let alpha = (time.time_since_startup().as_secs_f32().sin() / 2.0) + 0.5; let alpha = (time.elapsed_seconds().sin() / 2.0) + 0.5;
for (_, material) in materials.iter_mut() { for (_, material) in materials.iter_mut() {
material.base_color.set_a(alpha); material.base_color.set_a(alpha);
} }

View File

@ -54,9 +54,9 @@ fn move_scene_entities(
iter_hierarchy(moved_scene_entity, &children, &mut |entity| { iter_hierarchy(moved_scene_entity, &children, &mut |entity| {
if let Ok(mut transform) = transforms.get_mut(entity) { if let Ok(mut transform) = transforms.get_mut(entity) {
transform.translation = Vec3::new( transform.translation = Vec3::new(
offset * time.seconds_since_startup().sin() as f32 / 20., offset * time.elapsed_seconds().sin() / 20.,
0., 0.,
time.seconds_since_startup().cos() as f32 / 20., time.elapsed_seconds().cos() / 20.,
); );
offset += 1.0; offset += 1.0;
} }

View File

@ -1,7 +1,7 @@
//! Skinned mesh example with mesh and joints data defined in code. //! Skinned mesh example with mesh and joints data defined in code.
//! Example taken from <https://github.com/KhronosGroup/glTF-Tutorials/blob/master/gltfTutorial/gltfTutorial_019_SimpleSkin.md> //! Example taken from <https://github.com/KhronosGroup/glTF-Tutorials/blob/master/gltfTutorial/gltfTutorial_019_SimpleSkin.md>
use std::f32::consts::PI; use std::f32::consts::*;
use bevy::{ use bevy::{
pbr::AmbientLight, pbr::AmbientLight,
@ -162,7 +162,6 @@ fn setup(
/// Animate the joint marked with [`AnimatedJoint`] component. /// Animate the joint marked with [`AnimatedJoint`] component.
fn joint_animation(time: Res<Time>, mut query: Query<&mut Transform, With<AnimatedJoint>>) { fn joint_animation(time: Res<Time>, mut query: Query<&mut Transform, With<AnimatedJoint>>) {
for mut transform in &mut query { for mut transform in &mut query {
transform.rotation = transform.rotation = Quat::from_rotation_z(FRAC_PI_2 * time.elapsed_seconds().sin());
Quat::from_rotation_z(PI / 2. * time.time_since_startup().as_secs_f32().sin());
} }
} }

View File

@ -1,7 +1,7 @@
//! Skinned mesh example with mesh and joints data loaded from a glTF file. //! Skinned mesh example with mesh and joints data loaded from a glTF file.
//! Example taken from <https://github.com/KhronosGroup/glTF-Tutorials/blob/master/gltfTutorial/gltfTutorial_019_SimpleSkin.md> //! Example taken from <https://github.com/KhronosGroup/glTF-Tutorials/blob/master/gltfTutorial/gltfTutorial_019_SimpleSkin.md>
use std::f32::consts::PI; use std::f32::consts::*;
use bevy::{pbr::AmbientLight, prelude::*, render::mesh::skinning::SkinnedMesh}; use bevy::{pbr::AmbientLight, prelude::*, render::mesh::skinning::SkinnedMesh};
@ -67,6 +67,6 @@ fn joint_animation(
let mut second_joint_transform = transform_query.get_mut(second_joint_entity).unwrap(); let mut second_joint_transform = transform_query.get_mut(second_joint_entity).unwrap();
second_joint_transform.rotation = second_joint_transform.rotation =
Quat::from_rotation_z(PI / 2. * time.time_since_startup().as_secs_f32().sin()); Quat::from_rotation_z(FRAC_PI_2 * time.elapsed_seconds().sin());
} }
} }

View File

@ -32,7 +32,7 @@ fn update_speed(
time: Res<Time>, time: Res<Time>,
) { ) {
if let Some(sink) = audio_sinks.get(&music_controller.0) { if let Some(sink) = audio_sinks.get(&music_controller.0) {
sink.set_speed(((time.seconds_since_startup() / 5.0).sin() as f32 + 1.0).max(0.1)); sink.set_speed(((time.elapsed_seconds() / 5.0).sin() + 1.0).max(0.1));
} }
} }

View File

@ -14,7 +14,7 @@ fn main() {
} }
#[derive(Component, Debug)] #[derive(Component, Debug)]
struct MyComponent(f64); struct MyComponent(f32);
fn setup(mut commands: Commands) { fn setup(mut commands: Commands) {
commands.spawn(MyComponent(0.)); commands.spawn(MyComponent(0.));
@ -25,7 +25,7 @@ fn change_component(time: Res<Time>, mut query: Query<(Entity, &mut MyComponent)
for (entity, mut component) in &mut query { for (entity, mut component) in &mut query {
if rand::thread_rng().gen_bool(0.1) { if rand::thread_rng().gen_bool(0.1) {
info!("changing component {:?}", entity); info!("changing component {:?}", entity);
component.0 = time.seconds_since_startup(); component.0 = time.elapsed_seconds();
} }
} }
} }

View File

@ -31,22 +31,30 @@ fn main() {
.run(); .run();
} }
fn frame_update(mut last_time: Local<f64>, time: Res<Time>) { fn frame_update(mut last_time: Local<f32>, time: Res<Time>) {
info!("update: {}", time.seconds_since_startup() - *last_time); info!(
*last_time = time.seconds_since_startup(); "time since last frame_update: {}",
time.raw_elapsed_seconds() - *last_time
);
*last_time = time.raw_elapsed_seconds();
} }
fn fixed_update(mut last_time: Local<f64>, time: Res<Time>, fixed_timesteps: Res<FixedTimesteps>) { fn fixed_update(mut last_time: Local<f32>, time: Res<Time>, fixed_timesteps: Res<FixedTimesteps>) {
info!( info!(
"fixed_update: {}", "time since last fixed_update: {}\n",
time.seconds_since_startup() - *last_time, time.raw_elapsed_seconds() - *last_time
); );
let fixed_timestep = fixed_timesteps.get(LABEL).unwrap(); let state = fixed_timesteps.get(LABEL).unwrap();
info!(
" overstep_percentage: {}",
fixed_timestep.overstep_percentage()
);
*last_time = time.seconds_since_startup(); info!("fixed timestep: {}\n", 0.5);
info!(
"time accrued toward next fixed_update: {}\n",
state.accumulator()
);
info!(
"time accrued toward next fixed_update (% of timestep): {}",
state.overstep_percentage()
);
*last_time = time.raw_elapsed_seconds();
} }

View File

@ -1,6 +1,6 @@
//! Creates a hierarchy of parents and children entities. //! Creates a hierarchy of parents and children entities.
use std::f32::consts::PI; use std::f32::consts::*;
use bevy::prelude::*; use bevy::prelude::*;
@ -78,12 +78,12 @@ fn rotate(
} }
// To demonstrate removing children, we'll remove a child after a couple of seconds. // To demonstrate removing children, we'll remove a child after a couple of seconds.
if time.seconds_since_startup() >= 2.0 && children.len() == 2 { if time.elapsed_seconds() >= 2.0 && children.len() == 2 {
let child = children.last().unwrap(); let child = children.last().unwrap();
commands.entity(*child).despawn_recursive(); commands.entity(*child).despawn_recursive();
} }
if time.seconds_since_startup() >= 4.0 { if time.elapsed_seconds() >= 4.0 {
// This will remove the entity from its parent's list of children, as well as despawn // This will remove the entity from its parent's list of children, as well as despawn
// any children the entity has. // any children the entity has.
commands.entity(parent).despawn_recursive(); commands.entity(parent).despawn_recursive();

View File

@ -44,7 +44,7 @@ fn remove_component(
query: Query<Entity, With<MyComponent>>, query: Query<Entity, With<MyComponent>>,
) { ) {
// After two seconds have passed the `Component` is removed. // After two seconds have passed the `Component` is removed.
if time.seconds_since_startup() > 2.0 { if time.elapsed_seconds() > 2.0 {
if let Some(entity) = query.iter().next() { if let Some(entity) = query.iter().next() {
commands.entity(entity).remove::<MyComponent>(); commands.entity(entity).remove::<MyComponent>();
} }

View File

@ -134,6 +134,6 @@ fn change_color(time: Res<Time>, mut query: Query<&mut Sprite>) {
for mut sprite in &mut query { for mut sprite in &mut query {
sprite sprite
.color .color
.set_b((time.seconds_since_startup() * 0.5).sin() as f32 + 2.0); .set_b((time.elapsed_seconds() * 0.5).sin() as f32 + 2.0);
} }
} }

View File

@ -90,7 +90,7 @@ fn main() {
/// Example of a run criteria. /// Example of a run criteria.
/// Here we only want to run for a second, then stop. /// Here we only want to run for a second, then stop.
fn run_for_a_second(time: Res<Time>, mut done: ResMut<Done>) -> ShouldRun { fn run_for_a_second(time: Res<Time>, mut done: ResMut<Done>) -> ShouldRun {
let elapsed = time.seconds_since_startup(); let elapsed = time.elapsed_seconds();
if elapsed < 1.0 { if elapsed < 1.0 {
info!( info!(
"We should run again. Elapsed/remaining: {:.2}s/{:.2}s", "We should run again. Elapsed/remaining: {:.2}s/{:.2}s",

View File

@ -356,9 +356,8 @@ fn rotate_bonus(game: Res<Game>, time: Res<Time>, mut transforms: Query<&mut Tra
if let Some(entity) = game.bonus.entity { if let Some(entity) = game.bonus.entity {
if let Ok(mut cake_transform) = transforms.get_mut(entity) { if let Ok(mut cake_transform) = transforms.get_mut(entity) {
cake_transform.rotate_y(time.delta_seconds()); cake_transform.rotate_y(time.delta_seconds());
cake_transform.scale = Vec3::splat( cake_transform.scale =
1.0 + (game.score as f32 / 10.0 * time.seconds_since_startup().sin() as f32).abs(), Vec3::splat(1.0 + (game.score as f32 / 10.0 * time.elapsed_seconds().sin()).abs());
);
} }
} }
} }

View File

@ -52,7 +52,7 @@ impl FromWorld for ComponentB {
fn from_world(world: &mut World) -> Self { fn from_world(world: &mut World) -> Self {
let time = world.resource::<Time>(); let time = world.resource::<Time>();
ComponentB { ComponentB {
_time_since_startup: time.time_since_startup(), _time_since_startup: time.elapsed(),
value: "Default Value".to_string(), value: "Default Value".to_string(),
} }
} }

View File

@ -14,7 +14,7 @@ use bevy::{
scene::InstanceId, scene::InstanceId,
}; };
use std::f32::consts::PI; use std::f32::consts::*;
#[derive(Debug, Hash, PartialEq, Eq, Clone, SystemLabel)] #[derive(Debug, Hash, PartialEq, Eq, Clone, SystemLabel)]
struct CameraControllerCheckSystem; struct CameraControllerCheckSystem;
@ -331,8 +331,8 @@ fn update_lights(
transform.rotation = Quat::from_euler( transform.rotation = Quat::from_euler(
EulerRot::ZYX, EulerRot::ZYX,
0.0, 0.0,
time.seconds_since_startup() as f32 * PI / 15.0, time.elapsed_seconds() * PI / 15.0,
-PI / 4., -FRAC_PI_4,
); );
} }
} }

View File

@ -78,7 +78,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
fn text_color_system(time: Res<Time>, mut query: Query<&mut Text, With<ColorText>>) { fn text_color_system(time: Res<Time>, mut query: Query<&mut Text, With<ColorText>>) {
for mut text in &mut query { for mut text in &mut query {
let seconds = time.seconds_since_startup() as f32; let seconds = time.elapsed_seconds();
// Update the color of the first and only section. // Update the color of the first and only section.
text.sections[0].style.color = Color::Rgba { text.sections[0].style.color = Color::Rgba {

View File

@ -46,7 +46,7 @@ fn change_title(time: Res<Time>, mut windows: ResMut<Windows>) {
let window = windows.primary_mut(); let window = windows.primary_mut();
window.set_title(format!( window.set_title(format!(
"Seconds since startup: {}", "Seconds since startup: {}",
time.seconds_since_startup().round() time.elapsed_seconds().round()
)); ));
} }