dimforge / dimforge/bevy_rapier

KinematicCharacterController Velocity is dependent on framerate.

Open
#475 13 comments 0 reactions 0 assignees View on GitHub
A-Dynamics A-Integration C-Bug D-Difficult P-Low question S-not-started
Dominant language
Rust
Stars
1.6k
Forks
282
PR merge metrics
No merged PRs in 30d

Description

The Velocity component is updated by bevy_rapier even for KinematicCharacterController/Rigidbody::KinematicPositionBased. Unfortunately, the reported velocity is tied to the framerate! Higher delta times results in a higher velocity.

This example (using Bevy 0.12 and bevy_rapier2d 0.24) shows what happens if we artificially decrease the framerate (by enabling the `laggy` system:
```rust
use std::time::Duration;

use bevy::{prelude::*, render::camera::ScalingMode};
use bevy_rapier2d::prelude::*;

fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugins(RapierPhysicsPlugin::::pixels_per_meter(350.0))
.add_plugins(RapierDebugRenderPlugin::default())
.add_systems(Startup, spawn_collider)
.add_systems(Update, set_move)
.add_systems(PostUpdate, print_velocity.after(PhysicsSet::Writeback))
// .add_systems(Update, laggy) // Uncomment for lag!
.run();
}

fn spawn_collider(mut commands: Commands) {
commands
.spawn(SpatialBundle::default())
.insert(Collider::cuboid(100.0, 100.0))
.insert(RigidBody::KinematicPositionBased)
.insert(Velocity::default())
.insert(KinematicCharacterController::default());

commands.spawn(Camera2dBundle {
projection: OrthographicProjection {
near: -1000.0,
far: 1000.0,
scaling_mode: ScalingMode::FixedVertical(700.0),
..Default::default()
},
..Default::default()
});
}

fn laggy() {
std::thread::sleep(Duration::from_millis(100));
}

fn set_move(time: Res

fn print_velocity(velocities: Query<&Velocity>) {
dbg!(velocities.single());
}
```
My results: Without the `laggy` system, my velocity is ~50. With the `laggy` system, my velocity is >300.

My concrete use case for the Velocity component on a KinematicCharacterController is a generic camera. I want my camera to be able to follow any entity and I also want the camera to "look ahead" of where the entity is going. I do this by adding the entity's velocity (multiplied by some "prediction time") to the entity's position and lerping the camera there. Ideally I want this system to work for physics objects or kinematic characters or whatever else. Unfortunately this breaks with kinematic characters because of this bug.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.