Skip to content
New issue

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

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

Already on GitHub? # to your account

TimestepMode::Interpolated + TransformInterpolation does not function correctly #341

Closed
HeartofPhos opened this issue Mar 13, 2023 · 4 comments · May be fixed by #463
Closed

TimestepMode::Interpolated + TransformInterpolation does not function correctly #341

HeartofPhos opened this issue Mar 13, 2023 · 4 comments · May be fixed by #463

Comments

@HeartofPhos
Copy link

HeartofPhos commented Mar 13, 2023

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()
        });
}
@Anti-Alias
Copy link
Contributor

Also facing this issue.

@Anti-Alias
Copy link
Contributor

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

@Vixenka
Copy link

Vixenka commented Jan 31, 2024

@sebcrozet I think #384 closed this.

@Vrixyz
Copy link
Contributor

Vrixyz commented May 23, 2024

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()
        });
}

interpolation_ok.mp4

@Vrixyz Vrixyz closed this as completed May 23, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants