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

Collider Systerm error #810

Open
AnlangA opened this issue Mar 12, 2025 · 2 comments
Open

Collider Systerm error #810

AnlangA opened this issue Mar 12, 2025 · 2 comments

Comments

@AnlangA
Copy link

AnlangA commented Mar 12, 2025

Some cubes passed through the floor:

//cargo.tom
[package]
name = "bevy_rapier"
version = "0.1.0"
edition = "2024"

[dependencies]
bevy = "0.15.3"
bevy_rapier3d = {version = "0.29.0", features = ["debug-render-3d"]}
rand = "0.9.0"
//main.rs
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] 

use bevy::prelude::*;
use bevy_rapier3d::prelude::*;

fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        .add_plugins(RapierPhysicsPlugin::<NoUserData>::default())
        //.add_plugins(RapierDebugRenderPlugin::default())
        .add_systems(Startup, setup_graphics)
        .add_systems(Startup, setup_physics)
        .add_systems(Update, (spawn_cube_timer, despawn_cubes, display_cube_count))
        .run();
}

fn setup_graphics(mut commands: Commands) {
    commands.spawn((
        PointLight {
            shadows_enabled: true,
            ..default()
        },
        
        Transform::from_xyz(4.0, 8.0, 4.0),
    ));
    // camera
    commands.spawn((
        Camera3d::default(),
        Transform::from_xyz(-5.5, 5.5, 20.0).looking_at(Vec3::ZERO, Vec3::Y),
    ));

    commands.spawn((
        Text::default(),
        Node {
            position_type: PositionType::Absolute,
            bottom: Val::Px(12.0),
            left: Val::Px(12.0),
            ..default()
        },
    ));
}

fn setup_physics(mut commands: Commands, mut meshes: ResMut<Assets<Mesh>>,mut materials: ResMut<Assets<StandardMaterial>>,) {
    let r = rand::random::<u8>();
    let g = rand::random::<u8>();
    let b = rand::random::<u8>();
    commands.spawn((
        Mesh3d(meshes.add(Cylinder::new(10.0, 0.1))),
        RigidBody::Fixed,
        Restitution::coefficient(0.4),
        Collider::cylinder(0.05, 10.0),
        MeshMaterial3d(materials.add(Color::srgb_u8(r, g, b))),
        Transform::from_xyz(0.0, -2.0, 0.0))
    );
}

fn spawn_cube_timer(
    mut commands: Commands,
    mut meshes: ResMut<Assets<Mesh>>,
    mut materials: ResMut<Assets<StandardMaterial>>,
    time: Res<Time>,
    mut next_spawn: Local<f32>,
) {
    if time.elapsed_secs() > *next_spawn {
        // 每200ms生成一次
        *next_spawn = time.elapsed_secs() + 1.0;
        
        // 生成随机颜色
        let r = rand::random::<u8>();
        let g = rand::random::<u8>();
        let b = rand::random::<u8>();
        
        // 生成随机角度和半径
        let angle = rand::random::<f32>() * std::f32::consts::PI * 2.0;
        let radius = rand::random::<f32>() * 4.0;
        
        // 计算x和z坐标
        let x = radius * angle.cos();
        let z = radius * angle.sin();
        
        commands.spawn((
            Mesh3d(meshes.add(Cuboid::new(0.1, 0.1, 0.1))),
            RigidBody::Dynamic,
            Restitution::coefficient(0.2),
            Collider::cuboid(0.05, 0.05, 0.05),
            MeshMaterial3d(materials.add(Color::srgb_u8(r, g, b))),
            //Velocity::angular(Vec3::new(1.0, 1.0, 1.0)),
            Transform::from_xyz(x, 20.0, z),
        ));
    }
}

fn despawn_cubes(
    mut commands: Commands,
    query: Query<(Entity, &Transform)>,
) {
    for (entity, transform) in query.iter() {
        if transform.translation.y < -20.0 {
            commands.entity(entity).despawn();
        }
    }
}

fn display_cube_count(
    query: Query<&Transform>,
    mut text: Single<&mut Text>,
) {
    let count = query.iter().filter(|transform| transform.translation.y > -20.0).count();
    text.0 = "Cube Count: ".to_owned() + &count.to_string();
}

@AnlangA
Copy link
Author

AnlangA commented Mar 12, 2025

2025.03.12.21.54.10.webm

@Johannes0021
Copy link
Contributor

I'm not an expert, but I suspect this might be a tunneling issue.
If the cubes fall too fast, they might skip the collision with the floor. Maybe enabling continuous collision detection(ccd) or reducing the time step could help.

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

2 participants