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

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

Open
ES1993 opened this issue Jun 11, 2024 · 0 comments
Open

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

ES1993 opened this issue Jun 11, 2024 · 0 comments

Comments

@ES1993
Copy link

ES1993 commented Jun 11, 2024

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 }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant