dimforge / bevy_rapier

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

Possible imprecision in cast_shape #119

Open FocusJP opened 2 years ago

FocusJP commented 2 years ago

I'm not sure if this is a real issue or just the reality of floating point precision, but I wanted to at least flag it as suspicious.

I have a simple scene with a floor collider of shape

ColliderShape::cuboid(500.0, 1.0, 500.0)

I do a vertical sphere cast from above the floor via

fn test_sphere_cast(
    q_pipeline: Res<QueryPipeline>,
    q_collider: QueryPipelineColliderComponentsQuery,
){
    let collider_set = QueryPipelineColliderComponentsSet(&q_collider);
    let shape = Ball::new(1.0);
    let start = Isometry::translation(0.0, 3.0, 0.0);
    let direction = vector![0.0, -1.0, 0.0];
    let max_toi = 2.0;
    let groups = InteractionGroups::all();
    let filter = None;

    if let Some((handle, hit)) = q_pipeline.cast_shape(
        &collider_set, &start, &direction, &shape, max_toi, groups, filter
    ){
        let y1 = hit.witness1.y;
        let y2 = start.translation.y + hit.toi * direction.y + hit.witness2.y;
        info!("{} {} {}", hit.toi, y1, y2); 
    }
}

I expect y1 and y2 will both agree on a hit height of ~1.0. In fact, y1 is always within about 1E-7. However, as I vary start.x and start.z in small 0.01 increments I notice that y2 may drift as little as 5E-8 or as much as 6E-2. I don't know the inner workings of the shape cast algorithm, but is this variation expected?

FocusJP commented 2 years ago

Reducing the cuboid dimensions to

ColliderShape::cuboid(20.0, 1.0, 20.0)

dramatically reduced the jitter I am getting. For now my workaround is to break up large floor panels into smaller panels.

DanielHZhang commented 1 year ago

I'm also running into this issue with cast_shape hit output being imprecise, especially when casting Collider::ball onto large surfaces, which results in a significant amount of jitter.

I made a minimal reproduction of the issue here: https://github.com/DanielHZhang/bevy_rapier_shape_cast_imprecision

https://user-images.githubusercontent.com/30360288/227809134-f2fd0770-f0da-420a-9393-d60b6f384e39.mov


This seems to be caused by rapier itself, and not just the bevy plugin: https://github.com/dimforge/rapier/issues/434