Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reflect resources #31

Merged
merged 2 commits into from
Jun 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,13 @@ impl Plugin for XpbdPlugin {
.init_resource::<NumPosIters>()
.init_resource::<XpbdLoop>()
.init_resource::<Gravity>()
.register_type::<PhysicsTimestep>()
.register_type::<DeltaTime>()
.register_type::<SubDeltaTime>()
.register_type::<NumSubsteps>()
.register_type::<NumPosIters>()
.register_type::<XpbdLoop>()
.register_type::<Gravity>()
.register_type::<RigidBody>()
.register_type::<Pos>()
.register_type::<Rot>()
Expand Down Expand Up @@ -225,7 +232,8 @@ fn draw_aabbs(aabbs: Query<&ColliderAabb>, mut lines: ResMut<DebugLines>) {
}

/// Data related to the simulation loop.
#[derive(Resource, Debug, Default)]
#[derive(Reflect, Resource, Debug, Default)]
#[reflect(Resource)]
pub struct XpbdLoop {
/// Time accumulated into the physics loop. This is consumed by the [`XpbdSchedule`].
pub(crate) accumulator: Scalar,
Expand Down
20 changes: 13 additions & 7 deletions src/resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

use bevy::prelude::Resource;

use crate::{Scalar, Vector};
use crate::prelude::*;

/// Configures how many times per second the physics simulation is run.
#[derive(Resource, Clone, Copy, Debug, PartialEq)]
#[derive(Reflect, Resource, Clone, Copy, Debug, PartialEq)]
#[reflect(Resource)]
pub enum PhysicsTimestep {
/// **Fixed timestep**: the physics simulation will be advanced by a fixed value `dt` for every `dt` seconds passed since the previous physics frame. This allows consistent behavior across different machines and framerates.
Fixed(Scalar),
Expand All @@ -25,15 +26,18 @@ impl Default for PhysicsTimestep {
}

/// How much time the previous physics frame took. The timestep can be configured with the [`PhysicsTimestep`] resource.
#[derive(Resource, Default)]
#[derive(Reflect, Resource, Default)]
#[reflect(Resource)]
pub struct DeltaTime(pub Scalar);

/// How much time the previous physics substep took. This depends on the [`DeltaTime`] and [`NumSubsteps`] resources.
#[derive(Resource, Default)]
#[derive(Reflect, Resource, Default)]
#[reflect(Resource)]
pub struct SubDeltaTime(pub Scalar);

/// The number of substeps used in XPBD simulation. A higher number of substeps reduces the value of [`SubDeltaTime`], which results in a more accurate simulation at the cost of performance.
#[derive(Resource, Clone, Copy)]
#[derive(Reflect, Resource, Clone, Copy)]
#[reflect(Resource)]
pub struct NumSubsteps(pub u32);

impl Default for NumSubsteps {
Expand All @@ -43,7 +47,8 @@ impl Default for NumSubsteps {
}

/// The number of iterations used in the position solver. It is recommended to keep this low and to increase [`NumSubsteps`] instead, as substepping can provide better convergence, accuracy and energy conservation.
#[derive(Resource)]
#[derive(Reflect, Resource)]
#[reflect(Resource)]
pub struct NumPosIters(pub u32);

impl Default for NumPosIters {
Expand All @@ -55,7 +60,8 @@ impl Default for NumPosIters {
/// The global gravitational acceleration. This is applied to dynamic bodies in the integration step.
///
/// The default is an acceleration of 9.81 m/s^2 pointing down, which is approximate to the gravitational acceleration near Earth's surface.
#[derive(Resource, Debug)]
#[derive(Reflect, Resource, Debug)]
#[reflect(Resource)]
pub struct Gravity(pub Vector);

impl Default for Gravity {
Expand Down