avian3d/schedule/time.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
//! Clocks used for tracking physics simulation time.
use crate::prelude::*;
use bevy::prelude::*;
/// The clock representing physics time, following the [`Time`] clock used by the schedule that physics runs in.
///
/// In [`FixedPostUpdate`] and other fixed schedules, this uses [`Time<Fixed>`](Fixed), while in schedules such as [`Update`],
/// a variable timestep following [`Time<Virtual>`](Virtual) is used.
///
/// [`Time<Physics>`](Physics) is automatically set as the generic [`Time`] resource for
/// the [`PhysicsSchedule`].
///
/// # Usage
///
/// ## Physics Speed
///
/// The relative speed of [`Time<Physics>`](Physics) can be configured at startup
/// using [`with_relative_speed`], or when the app is running using [`set_relative_speed`]:
///
/// ```no_run
#[cfg_attr(feature = "2d", doc = "use avian2d::prelude::*;")]
#[cfg_attr(feature = "3d", doc = "use avian3d::prelude::*;")]
/// use bevy::{prelude::*, utils::Duration};
///
/// fn main() {
/// App::new()
/// .add_plugins((DefaultPlugins, PhysicsPlugins::default()))
/// // Run physics at 0.5 speed
/// .insert_resource(Time::<Physics>::default().with_relative_speed(0.5))
/// .run();
/// }
/// ```
///
/// [`with_relative_speed`]: PhysicsTime::with_relative_speed
/// [`set_relative_speed`]: PhysicsTime::set_relative_speed
///
/// ## Pausing, Resuming, and Stepping Physics
///
/// [`Time<Physics>`](Physics) can be used to pause and resume the simulation:
///
/// ```
#[cfg_attr(feature = "2d", doc = "use avian2d::prelude::*;")]
#[cfg_attr(feature = "3d", doc = "use avian3d::prelude::*;")]
/// use bevy::prelude::*;
///
/// fn pause(mut time: ResMut<Time<Physics>>) {
/// time.pause();
/// }
///
/// fn unpause(mut time: ResMut<Time<Physics>>) {
/// time.unpause();
/// }
/// ```
///
/// To advance the simulation by a certain amount of time instantly, you can advance the
/// [`Time<Physics>`](Physics) clock and manually run the [`PhysicsSchedule`] in an exclusive system:
///
/// ```
#[cfg_attr(feature = "2d", doc = "use avian2d::prelude::*;")]
#[cfg_attr(feature = "3d", doc = "use avian3d::prelude::*;")]
/// use bevy::{prelude::*, utils::Duration};
///
/// fn run_physics(world: &mut World) {
/// // Advance the simulation by 10 steps at 120 Hz
/// for _ in 0..10 {
/// world
/// .resource_mut::<Time<Physics>>()
/// .advance_by(Duration::from_secs_f64(1.0 / 120.0));
/// world.run_schedule(PhysicsSchedule);
/// }
/// }
/// ```
///
/// ## When to Multiply by Delta Time?
///
/// Schedules like `Update` use a variable timestep, which can often cause frame rate dependent
/// behavior when moving bodies. One way to help address the issue is by multiplying by delta time.
///
/// In general, if you're doing a *continuous* operation, you should always multiply by delta time,
/// but for *instantaneous* operations it's not necessary.
///
/// Continuous operations move or accelerate bodies over time:
///
/// ```
/// # use bevy::math::Vec3;
/// #
/// # let mut position = Vec3::default();
/// # let mut velocity = Vec3::default();
/// # let mut acceleration = Vec3::default();
/// # let mut delta_time = 1.0 / 60.0;
/// #
/// // Move continuously over time
/// position += velocity * delta_time;
/// // Accelerate continuously
/// velocity += acceleration * delta_time;
/// ```
///
/// Instantaneous operations apply a singular sudden burst of velocity
/// or set it to a specific value:
///
/// ```
/// # use bevy::math::Vec3;
/// #
/// # let mut velocity = Vec3::default();
/// # let mut impulse = Vec3::default();
/// # let mut target_velocity = Vec3::default();
/// #
/// // Apply a burst of speed once (jumps, explosions and so on)
/// velocity += impulse;
/// // Set velocity to a specific value
/// velocity = target_velocity;
/// ```
///
/// For systems using a fixed timestep, using delta time is not necessary for frame rate
/// independence, but it's still recommended so that the physical units are more logical.
#[derive(Reflect, Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serialize", reflect(Serialize, Deserialize))]
#[reflect(Debug, PartialEq)]
pub struct Physics {
paused: bool,
relative_speed: f64,
}
impl Default for Physics {
fn default() -> Self {
Self {
paused: false,
relative_speed: 1.0,
}
}
}
/// An extension trait for [`Time<Physics>`](Physics).
pub trait PhysicsTime {
/// Returns the speed of physics relative to your system clock as an `f32`.
/// This is also known as "time scaling" or "time dilation" in other engines.
///
/// The speed impacts the accuracy of the simulation, and large values may
/// cause jittering or missed collisions. You can improve simulation consistency
/// by adjusting your timestep at the cost of performance.
fn relative_speed(&self) -> f32;
/// Returns the speed of physics relative to your system clock as an `f64`.
/// This is also known as "time scaling" or "time dilation" in other engines.
///
/// The speed impacts the accuracy of the simulation, and large values may
/// cause jittering or missed collisions. You can improve simulation consistency
/// by adjusting your timestep at the cost of performance.
fn relative_speed_f64(&self) -> f64;
/// Sets the speed of physics relative to your system clock, given as an `f32`.
///
/// For example, setting this to `2.0` will make the physics clock advance twice
/// as fast as your system clock.
///
/// The speed impacts the accuracy of the simulation, and large values may
/// cause jittering or missed collisions. You can improve simulation consistency
/// by adjusting your timestep at the cost of performance.
///
/// # Panics
///
/// Panics if `ratio` is negative or not finite.
fn with_relative_speed(self, ratio: f32) -> Self;
/// Sets the speed of physics relative to your system clock, given as an `f64`.
///
/// For example, setting this to `2.0` will make the physics clock advance twice
/// as fast as your system clock.
///
/// The speed impacts the accuracy of the simulation, and large values may
/// cause jittering or missed collisions. You can improve simulation consistency
/// by adjusting your timestep at the cost of performance.
///
/// # Panics
///
/// Panics if `ratio` is negative or not finite.
fn with_relative_speed_f64(self, ratio: f64) -> Self;
/// Sets the speed of physics relative to your system clock, given as an `f32`.
///
/// For example, setting this to `2.0` will make the physics clock advance twice
/// as fast as your system clock.
///
/// The speed impacts the accuracy of the simulation, and large values may
/// cause jittering or missed collisions. You can improve simulation consistency
/// by adjusting your timestep at the cost of performance.
///
/// # Panics
///
/// Panics if `ratio` is negative or not finite.
fn set_relative_speed(&mut self, ratio: f32);
/// Sets the speed of physics relative to your system clock, given as an `f64`.
///
/// For example, setting this to `2.0` will make the physics clock advance twice
/// as fast as your system clock.
///
/// The speed impacts the accuracy of the simulation, and large values may
/// cause jittering or missed collisions. You can improve simulation consistency
/// by adjusting your timestep at the cost of performance.
///
/// # Panics
///
/// Panics if `ratio` is negative or not finite.
fn set_relative_speed_f64(&mut self, ratio: f64);
/// Returns `true` if the physics clock is currently paused.
fn is_paused(&self) -> bool;
/// Stops the clock, preventing the physics simulation from advancing until resumed.
fn pause(&mut self);
/// Resumes the clock if paused.
#[doc(alias = "resume")]
fn unpause(&mut self);
}
impl PhysicsTime for Time<Physics> {
fn with_relative_speed(self, ratio: f32) -> Self {
self.with_relative_speed_f64(ratio as f64)
}
fn with_relative_speed_f64(mut self, ratio: f64) -> Self {
assert!(ratio.is_finite(), "tried to go infinitely fast");
assert!(ratio >= 0.0, "tried to go back in time");
self.context_mut().relative_speed = ratio;
self
}
fn relative_speed(&self) -> f32 {
self.relative_speed_f64() as f32
}
fn relative_speed_f64(&self) -> f64 {
self.context().relative_speed
}
fn set_relative_speed(&mut self, ratio: f32) {
self.set_relative_speed_f64(ratio as f64);
}
fn set_relative_speed_f64(&mut self, ratio: f64) {
assert!(ratio.is_finite(), "tried to go infinitely fast");
assert!(ratio >= 0.0, "tried to go back in time");
self.context_mut().relative_speed = ratio;
}
fn pause(&mut self) {
self.context_mut().paused = true;
}
fn unpause(&mut self) {
self.context_mut().paused = false;
}
fn is_paused(&self) -> bool {
self.context().paused
}
}
/// The clock representing physics substep time. It is updated based on
/// [`Time<Physics>`](Physics) and the [`SubstepCount`] resource.
///
/// The clock is automatically set as the generic `Time` resource for
/// the [`SubstepSchedule`].
#[derive(Reflect, Clone, Copy, Debug, Default, PartialEq)]
pub struct Substeps;
pub(crate) trait TimePrecisionAdjusted {
/// Returns how much time has advanced since the last update
/// as [`Scalar`] seconds.
fn delta_seconds_adjusted(&self) -> Scalar;
}
impl TimePrecisionAdjusted for Time {
/// Returns how much time has advanced since the last [`update`](#method.update)
/// as [`Scalar`] seconds.
fn delta_seconds_adjusted(&self) -> Scalar {
#[cfg(feature = "f32")]
{
self.delta_secs()
}
#[cfg(feature = "f64")]
{
self.delta_secs_f64()
}
}
}