dimforge / bevy_rapier

Official Rapier plugin for the Bevy game engine.
https://rapier.rs
Apache License 2.0
1.23k stars 259 forks source link

Setting `pixels_per_meter` doesn't seem to affect the physics scale #459

Open royvandewater opened 9 months ago

royvandewater commented 9 months ago

I think I might be misunderstanding what this value does. When running the boxes2 example, there seems to be no difference between adjusting the pixels_per_meter value. I have tried using a value of 1.0, 100.0 (default), and 10000.0 and the simulations seem to run exactly the same.

From Common Mistakes: Why is Everything Moving in Slow Motion docs, I got the impression that the pixels_per_meter constructor would adjust the simulation scale. I was expecting that a value of 1.0 would cause the boxes to fall a lot slower (since they'd be bigger and have further to fall) and I was expecting a value of 10000.0 to cause the boxes to fall a lot faster, since they'd be smaller and much closer to the ground.

royvandewater commented 9 months ago

I tried adjusting the physics scale by calling .with_physics_scale() with different values ranging from 0.2 to 2000.0, but it also seemed to have no impact.

GinjaNinja32 commented 5 months ago

that example is using the default gravity, with a fixed magnitude of 98.1 (vs 9.81 for 3D), which gets interpreted in scale units; due to how this value is set (in RapierConfiguration::default()), it can't take into account the varying scales that might be set; if you edit the example to apply the scale to the gravity too, the boxes act how you expected them to;

diff --git a/bevy_rapier2d/examples/boxes2.rs b/bevy_rapier2d/examples/boxes2.rs
index 6de77dd..1276491 100644
--- a/bevy_rapier2d/examples/boxes2.rs
+++ b/bevy_rapier2d/examples/boxes2.rs
@@ -1,6 +1,8 @@
 use bevy::prelude::*;
 use bevy_rapier2d::prelude::*;

+const SCALE: f32 = 10.0;
+
 fn main() {
     App::new()
         .insert_resource(ClearColor(Color::rgb(
@@ -10,7 +12,7 @@ fn main() {
         )))
         .add_plugins((
             DefaultPlugins,
-            RapierPhysicsPlugin::<NoUserData>::pixels_per_meter(100.0),
+            RapierPhysicsPlugin::<NoUserData>::pixels_per_meter(SCALE),
             RapierDebugRenderPlugin::default(),
         ))
         .add_systems(Startup, (setup_graphics, setup_physics))
@@ -24,7 +26,9 @@ pub fn setup_graphics(mut commands: Commands) {
     });
 }

-pub fn setup_physics(mut commands: Commands) {
+pub fn setup_physics(mut commands: Commands, mut rapier_config: ResMut<RapierConfiguration>) {
+    rapier_config.gravity = -Vect::Y * 9.81 * SCALE;
+
     /*
      * Ground
      */

effectively, the boxes are tiny with scale=10000.0, but the default gravity is weaker by the same amount and it cancels out.