bevy_rapier icon indicating copy to clipboard operation
bevy_rapier copied to clipboard

Why is the speed of the ball not constant in this demo?

Open ES1993 opened this issue 2 months ago • 0 comments

system:

macmini m2

cargo.toml version:

bevy = "0.13.2" bevy_rapier2d = { version = "0.26.0", features = [ "simd-stable", "debug-render-2d" ] }

code:

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

fn main() {
    App::new()
        .add_plugins((
            DefaultPlugins,
            RapierPhysicsPlugin::<NoUserData>::pixels_per_meter(100.0),
            RapierDebugRenderPlugin::default(),
        ))
        .add_systems(Startup, setup)
        .add_systems(Update, debug_print)
        .run();
}

fn debug_print(query: Query<&Velocity, With<Ball>>) {
    let velocity = query.single();
    println!("{velocity:?}");
}

#[derive(Component)]
struct Ball;

#[derive(Component)]
struct Wall;

fn setup(
    mut commands: Commands,
    mut material: ResMut<Assets<ColorMaterial>>,
    mut meshes: ResMut<Assets<Mesh>>,
) {
    commands.spawn(Camera2dBundle::default());

    commands.spawn((
        Ball,
        MaterialMesh2dBundle {
            mesh: meshes.add(Mesh::from(Circle::new(20.0))).into(),
            material: material.add(Color::RED),
            ..default()
        },
        RigidBody::Dynamic,
        Collider::ball(20.0),
        Restitution::coefficient(1.0),
        Friction::coefficient(0.0),
        GravityScale(0.0),
        Velocity {
            linvel: Vec2::new(500.0, 300.0),
            ..default()
        },
    ));

    commands.spawn((
        Wall,
        MaterialMesh2dBundle {
            mesh: meshes.add(Mesh::from(Rectangle::new(800.0, 10.0))).into(),
            material: material.add(Color::LIME_GREEN),
            transform: Transform::from_xyz(0.0, 300.0, 0.0),
            ..default()
        },
        RigidBody::Fixed,
        Collider::cuboid(400.0, 5.0),
        Restitution::coefficient(1.0),
        Friction::coefficient(0.0),
    ));

    commands.spawn((
        Wall,
        MaterialMesh2dBundle {
            mesh: meshes.add(Mesh::from(Rectangle::new(800.0, 10.0))).into(),
            material: material.add(Color::LIME_GREEN),
            transform: Transform::from_xyz(0.0, -300.0, 0.0),
            ..default()
        },
        RigidBody::Fixed,
        Collider::cuboid(400.0, 5.0),
        Restitution::coefficient(1.0),
        Friction::coefficient(0.0),
    ));

    commands.spawn((
        Wall,
        MaterialMesh2dBundle {
            mesh: meshes.add(Mesh::from(Rectangle::new(10.0, 600.0))).into(),
            material: material.add(Color::LIME_GREEN),
            transform: Transform::from_xyz(400.0, 0.0, 0.0),
            ..default()
        },
        RigidBody::Fixed,
        Collider::cuboid(5.0, 300.0),
        Restitution::coefficient(1.0),
        Friction::coefficient(0.0),
    ));

    commands.spawn((
        Wall,
        MaterialMesh2dBundle {
            mesh: meshes.add(Mesh::from(Rectangle::new(10.0, 600.0))).into(),
            material: material.add(Color::LIME_GREEN),
            transform: Transform::from_xyz(-400.0, 0.0, 0.0),
            ..default()
        },
        RigidBody::Fixed,
        Collider::cuboid(5.0, 300.0),
        Restitution::coefficient(1.0),
        Friction::coefficient(0.0),
    ));
}

expect:

the speed of the printed ball is constant

result:

The ball's speed changes after multiple collisions with the wall.

print log:

Velocity { linvel: Vec2(366.13907, 300.00055), angvel: 0.0 } Velocity { linvel: Vec2(366.13907, 300.00055), angvel: 0.0 } Velocity { linvel: Vec2(366.13907, 300.00055), angvel: 0.0 } Velocity { linvel: Vec2(366.13907, 300.00055), angvel: 0.0 } Velocity { linvel: Vec2(366.13907, 300.00055), angvel: 0.0 } Velocity { linvel: Vec2(366.13907, 300.00055), angvel: 0.0 } Velocity { linvel: Vec2(366.13907, 300.00055), angvel: 0.0 } Velocity { linvel: Vec2(366.13907, 300.00055), angvel: 0.0 } Velocity { linvel: Vec2(366.13907, 300.00055), angvel: 0.0 } Velocity { linvel: Vec2(366.13907, 300.00055), angvel: 0.0 } Velocity { linvel: Vec2(366.13907, 300.00055), angvel: 0.0 }

ES1993 avatar Jun 11 '24 14:06 ES1993