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 in a bundle with KinematicPositionBased component has a velocity-based Lag #483

Open
CaptainPhilipp opened this issue Mar 5, 2024 · 0 comments
Labels
A-Dynamics A-Integration very bevy specific bug Something isn't working D-Difficult Needs strong technical background, domain knowledge, or impacts are high, needs testing... P-Medium S-not-started Work has not started

Comments

@CaptainPhilipp
Copy link

CaptainPhilipp commented Mar 5, 2024

I can't use Colliders in inherited Bundles because collider position is updating with lag relative to this bundle transform, meshes etc.

Bundle Alisa is a parent with RigidBody::Dynamic component, ExternalForces applied to it in Update schedule creates a large velocity, for example 100m/s.
Bundle Bob is a child of Alisa, with RigidBody::KinematicPositionBased and KinematicCharacterController components.
Bob has no relative movement to it's parent Alisa,
Bob has Mesh in his Bundle(Tuple), that stays where it's supposed to be "sticked" to parent correctly,
Bob has Collider::capsule in his Bundle, that has HUGE velocity-based lag from his mesh, parent mesh, parent collider.
In this moment there is no interaction with those objects.

child_id = commands
        .spawn(ChildTag::default())
        .insert(RigidBody::Dynamic)
        .insert(Collider::capsule(Vec2::new(0., capsule_half_height), Vec2::new(0., -capsule_half_height), capsule_radius))
        .insert(MaterialMesh2dBundle {
            mesh: bevy::sprite::Mesh2dHandle(meshes.add(spec.capsule_form(false))),
            material: materials.add(Color::rgb(0.3, 0.3, 0.3)),
            transform: Transform { translation: Vec3::new(0., 0., 0.2), ..default() },
            ..default()
        })
        .insert(AdditionalMassProperties::MassProperties(
            MassProperties {
                mass: 75.,
                principal_inertia: rectangle_inertia(75., Vec2::new(0.5, 1.75)),
                ..Default::default()
            }
        ))
        .insert(Sleeping { linear_threshold: -1., angular_threshold: -1., sleeping: false })
        .insert(SolverGroups::new(GROUP_CHILD, Group::ALL ^ GROUP_PARENT))
        .id();

    if let Ok(parent_id) = parents_query.get_single() {
        commands.entity(parent_id).add_child(child_id);
    }
commands.spawn((
        ParentTag::default(),
        RigidBody::Dynamic,
        Collider::compound(vec![
            (Vect::new(-spec.half.x + 1., -1.), 0., Collider::cuboid(1., spec.half.y - 3.)),
            (Vect::new( spec.half.x - 1., -1.), 0., Collider::cuboid(1., spec.half.y - 3.)),
            (Vect::new(0.,  spec.half.y - 2.), 0., Collider::cuboid(spec.half.x, 2.)),
            (Vect::new(0., -spec.half.y + 1.), 0., Collider::cuboid(spec.half.x, 1.)),
        ]),
        Velocity::default(),
        MaterialMesh2dBundle {
            mesh: bevy::sprite::Mesh2dHandle(meshes.add(Rectangle::new(spec.dim.x, spec.dim.y))),
            material: materials.add(Color::rgb(0.5, 0.5, 0.5)),
            ..default()
        },
        AdditionalMassProperties::MassProperties(
            MassProperties {
                mass: spec.mass,
                principal_inertia: spec.inertia,
                local_center_of_mass: spec.center_mass,
            }
        ),
        ExternalForce::default(),
        Sleeping { linear_threshold: -1., angular_threshold: -1., sleeping: false },
        SolverGroups::new(GROUP_PARENT, Group::ALL)
    ))
app.add_systems(Update, apply_forces.run_if(in_state(crate::commons::AppState::Game)));

pub fn apply_forces(mut query: Query<(&Forces, &Spec, &Transform, &mut ExternalForce), With<ParentTag>>) {
    for (forces, spec, transform, mut ext) in query.iter_mut() {
        ...
        ext.force = (transform.rotation * forces.force).truncate();
        ext.torque = forces.torque * spec.torque_rate;
    }
}
Screenshot 2024-03-05 at 00 59 57

this is with velocity

Screenshot 2024-03-05 at 01 51 52

this is without the velocity

Capsule Mesh and capsule Collider is in the same Bundle/Tuple

@CaptainPhilipp CaptainPhilipp changed the title Collider in bundle with KinematicPositionBased component has a Lag Collider in a bundle with KinematicPositionBased component has a Lag Mar 5, 2024
@CaptainPhilipp CaptainPhilipp changed the title Collider in a bundle with KinematicPositionBased component has a Lag Collider in a bundle with KinematicPositionBased component has a velocity-based Lag Mar 5, 2024
@CaptainPhilipp CaptainPhilipp changed the title Collider in a bundle with KinematicPositionBased component has a velocity-based Lag Collider in a bundle with яKinematicPositionBased component has a velocity-based Lag Mar 5, 2024
@CaptainPhilipp CaptainPhilipp changed the title Collider in a bundle with яKinematicPositionBased component has a velocity-based Lag Collider in a bundle with KinematicPositionBased component has a velocity-based Lag Mar 5, 2024
@Vrixyz Vrixyz added bug Something isn't working D-Difficult Needs strong technical background, domain knowledge, or impacts are high, needs testing... P-Medium S-not-started Work has not started A-Dynamics A-Integration very bevy specific labels May 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Dynamics A-Integration very bevy specific bug Something isn't working D-Difficult Needs strong technical background, domain knowledge, or impacts are high, needs testing... P-Medium S-not-started Work has not started
Projects
None yet
Development

No branches or pull requests

2 participants