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

KinematicCharacterController speed scales with the refresh rate. #379

Closed
Anti-Alias opened this issue Jun 5, 2023 · 4 comments · May be fixed by #463 or #474
Closed

KinematicCharacterController speed scales with the refresh rate. #379

Anti-Alias opened this issue Jun 5, 2023 · 4 comments · May be fixed by #463 or #474

Comments

@Anti-Alias
Copy link
Contributor

When I set my monitor to 144hz, my player character moves faster than at 60hz.
This is not true for dynamic bodies, who move at the same speed regardless of the refresh rate.

Setup

bevy = 0.10
bevy_rapier3d = 0.23

Code

use bevy::prelude::*;
use bevy_rapier3d::prelude::*;
use bevy_inspector_egui::quick::WorldInspectorPlugin;

fn main() {
    App::new()
        .add_plugins(DefaultPlugins.set(ImagePlugin::default_nearest()))
        .add_plugin(RapierPhysicsPlugin::<()>::default())
        .add_plugin(RapierDebugRenderPlugin::default())
        .add_plugin(WorldInspectorPlugin::new())
        .add_startup_system(spawn_bodies)
        .add_system(control_kitematics)
        .run();
}

fn spawn_bodies(mut commands: Commands) {

    // Camera
    commands.spawn(Camera3dBundle {
        transform: Transform::from_xyz(0.0, 10.0, 45.0).looking_at(Vec3::ZERO, Vec3::Y),
        ..Default::default()
    });

    // Ground
    commands
        .spawn(Collider::cuboid(20.0, 0.1, 20.0))
        .insert(TransformBundle::from(Transform::from_xyz(0.0, 0.0, 0.0)));

    // Player
    commands
        .spawn((
            Collider::capsule_y(0.5, 1.0),
            RigidBody::KinematicPositionBased,
            KinematicCharacterController::default(),
            TransformBundle::from(Transform::from_xyz(0.0, 2.0, 0.0)),
            Name::new("Player")
        ));
}

fn control_kitematics(mut controllers: Query<&mut KinematicCharacterController>) {
    for mut controller in &mut controllers {
        controller.translation = Some(Vec3::new(0.1, 0.0, 0.0));
    }
}
@Anti-Alias
Copy link
Contributor Author

Note: Tried both KinematicPositionBased and KinematicVelocityBased.
Tried a RapierConfiguration with an Interpolated timestep mode.

Issue persists.

@Anti-Alias
Copy link
Contributor Author

Was able to fix by putting the control system in the PhysicsSet::StepSimulation base set and using the Interpolated timestep mode in the configuration.

Is this one of the intended ways of getting this to work?

@Architector4
Copy link

This seems to make complete sense with your code. The control_cinematics system is added to the Bevy app without any schedules or sets, and so it is run once per frame. And from there on, it sets the controller's translation to Some(Vec3::new(0.1, 0.0, 0.0)), once per frame.

Rapier's physics scale with the timesteps taken between each frame by default. For example the gravity is multiplied with the timestep before being applied to rigid bodies, giving velocity of -9.81/60 each frame when running at 60FPS and -9.81/120 when running at 120FPS. That's why the movement of rigid bodies stay consistent between different framerates.

Using interpolated timestep mode makes the simulation run at a fixed rate, once per timestep, irrespective of the framerate of the renderer or the logic. That's where putting the system into PhysicsSet::StepSimulation helps, because then between your test runs it is indeed run the same amount of times in a second.


To get this to work, make your system take a time: Res<Time>, and then set translation by 0.1 * time.delta_seconds() (or adjust the constant multiplier to your liking).

@Vrixyz
Copy link
Contributor

Vrixyz commented May 23, 2024

Thanks Architector for your answer, seems correct. Closing now.

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