bevy_rapier icon indicating copy to clipboard operation
bevy_rapier copied to clipboard

TimestepMode::Interpolated + TransformInterpolation does not function correctly

Open HeartofPhos opened this issue 2 years ago • 3 comments

Currently using TimestepMode::Interpolated with TransformInterpolation does not result in interpolated transforms It appears this issue is caused by src\plugin\systems.rs:332 where interpolation.start and interpolation.end are set to None without checking whether transform_changed returns true Updating the code to first check if the transform has changed results in the expected interpolated behavior

See minimal reproducible example

use bevy::{prelude::*, sprite::MaterialMesh2dBundle};
use bevy_rapier2d::prelude::*;

fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        .insert_resource(RapierConfiguration {
            // gravity: Vec2::ZERO,
            timestep_mode: TimestepMode::Interpolated {
                dt: 1.0 / 5.0,
                time_scale: 1.0,
                substeps: 1,
            },
            ..default()
        })
        .add_plugin(RapierPhysicsPlugin::<NoUserData>::pixels_per_meter(100.0))
        .add_plugin(RapierDebugRenderPlugin::default())
        .add_startup_system(setup_graphics)
        .add_startup_system(setup_physics)
        .run();
}

fn setup_graphics(mut commands: Commands) {
    // Add a camera so we can see the debug-render.
    commands.spawn(Camera2dBundle::default());
}

fn setup_physics(
    mut commands: Commands,
    mut meshes: ResMut<Assets<Mesh>>,
    mut materials: ResMut<Assets<ColorMaterial>>,
) {
    /* Create the ground. */
    commands
        .spawn(Collider::cuboid(500.0, 50.0))
        .insert(TransformBundle::from(Transform::from_xyz(0.0, -100.0, 0.0)));

    /* Create the bouncing ball with TransformInterpolation. */
    commands
        .spawn(RigidBody::Dynamic)
        .insert(Collider::ball(50.0))
        .insert(Restitution::coefficient(0.7))
        .insert(TransformInterpolation::default())
        .insert(MaterialMesh2dBundle {
            mesh: meshes.add(shape::Circle::new(50.).into()).into(),
            material: materials.add(ColorMaterial::from(Color::BLUE)),
            transform: Transform::from_xyz(0.0, 400.0, 0.0),
            ..default()
        });

    /* Create the bouncing ball without TransformInterpolation. */
    commands
        .spawn(RigidBody::Dynamic)
        .insert(Collider::ball(50.0))
        .insert(Restitution::coefficient(0.7))
        .insert(MaterialMesh2dBundle {
            mesh: meshes.add(shape::Circle::new(50.).into()).into(),
            material: materials.add(ColorMaterial::from(Color::GREEN)),
            transform: Transform::from_xyz(100.0, 400.0, 0.0),
            ..default()
        });
}

HeartofPhos avatar Mar 13 '23 03:03 HeartofPhos

Also facing this issue.

Anti-Alias avatar Jun 05 '23 17:06 Anti-Alias

If your code change fixes this, could this be a PR?

Anti-Alias avatar Jun 05 '23 18:06 Anti-Alias

@sebcrozet I think #384 closed this.

Vixenka avatar Jan 31 '24 10:01 Vixenka

seems fixed indeed,

updated repro code


use bevy::{prelude::*, sprite::MaterialMesh2dBundle};
use bevy_rapier2d::prelude::*;

fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        .insert_resource(RapierConfiguration {
            // gravity: Vec2::ZERO,
            timestep_mode: TimestepMode::Interpolated {
                dt: 1.0 / 5.0,
                time_scale: 1.0,
                substeps: 1,
            },
            ..RapierConfiguration::new(100f32)
        })
        .add_plugins(RapierPhysicsPlugin::<NoUserData>::pixels_per_meter(100.0))
        .add_plugins(RapierDebugRenderPlugin::default())
        .add_systems(Startup, setup_graphics)
        .add_systems(Startup, setup_physics)
        .run();
}

fn setup_graphics(mut commands: Commands) {
    // Add a camera so we can see the debug-render.
    commands.spawn(Camera2dBundle::default());
}

fn setup_physics(
    mut commands: Commands,
    mut meshes: ResMut<Assets<Mesh>>,
    mut materials: ResMut<Assets<ColorMaterial>>,
) {
    /* Create the ground. */
    commands
        .spawn(Collider::cuboid(500.0, 50.0))
        .insert(TransformBundle::from(Transform::from_xyz(0.0, -100.0, 0.0)));

    /* Create the bouncing ball with TransformInterpolation. */
    commands
        .spawn(RigidBody::Dynamic)
        .insert(Collider::ball(50.0))
        .insert(Restitution::coefficient(0.7))
        .insert(TransformInterpolation::default())
        .insert(MaterialMesh2dBundle {
            mesh: meshes.add(Circle::new(50.)).into(),
            material: materials.add(ColorMaterial::from(Color::BLUE)),
            transform: Transform::from_xyz(0.0, 400.0, 0.0),
            ..default()
        });

    /* Create the bouncing ball without TransformInterpolation. */
    commands
        .spawn(RigidBody::Dynamic)
        .insert(Collider::ball(50.0))
        .insert(Restitution::coefficient(0.7))
        .insert(MaterialMesh2dBundle {
            mesh: meshes.add(Circle::new(50.)).into(),
            material: materials.add(ColorMaterial::from(Color::GREEN)),
            transform: Transform::from_xyz(100.0, 400.0, 0.0),
            ..default()
        });
}

https://github.com/dimforge/bevy_rapier/assets/2290685/006a00d7-7a20-4b86-b3e7-f894b3d091f4

ThierryBerger avatar May 23 '24 15:05 ThierryBerger