Code Monkey home page Code Monkey logo

Comments (1)

TeamDman avatar TeamDman commented on June 5, 2024

I have made a nice reproduction of the weirdness

In my example, the character is the parent of both the pressure plate and the hand. As the character moves, its position and transform are both updated, but on the children only the transform is updated. This results in the children visually following the parent while the physics position not following the parent.

If the child has a velocity, the physics logic takes priority and overwrites the transform, resulting in the child moving with a velocity independent of the parent's velocity.

In the example, I have added ghosts to show the difference between the transform and the position

child_sensor_81wsXsbMJi.mp4

This is all related to the decision to use components instead of Transform to handle the physics (#16 and #96)

/// When synchronizing changes in [`Position`] or [`Rotation`] to `Transform`,
/// the engine treats nested [rigid bodies](RigidBody) as a flat structure. This means that
/// the bodies move independently of the parents, and moving the parent will not affect the child.
///
/// If you would like a child entity to be rigidly attached to its parent, you could use a [`FixedJoint`]
/// or write your own system to handle hierarchies differently.

ref

Here's a naΓ―ve attempt at inheriting the parent velocity. I changed the pressure plate from static to dynamic so that it also receives a velocity component that we can update.

fn inherit_velocity(
    mut kids: Query<(&mut LinearVelocity, &mut AngularVelocity, &Parent), Without<Character>>,
    parents: Query<(&LinearVelocity, &AngularVelocity), With<Character>>,
) {
    for (mut kid_linear_velocity, mut kid_angular_velocity, parent) in &mut kids.iter_mut() {
        if let Ok((parent_linear_velocity, parent_angular_velocity)) = parents.get(parent.get()) {
            kid_linear_velocity.x = parent_linear_velocity.x;
            kid_linear_velocity.y = parent_linear_velocity.y;
            kid_angular_velocity.0 = parent_angular_velocity.0;
        }
    }
}
child_sensor_d2hakh2r1j.mp4

This breaks the hand movement logic because the acceleration from its inputs gets clobbered when inheriting the parent velocity. Additionally, this would cause the children to rotate in place rather than pivoting around the parent.

Overall, the decision to have separate components instead of using Transform is because the physics system is inherently flat rather than hierarchical. The ECS itself is also flat, with Parent and Children being conveniences. Using a joint, as recommended by the code comment, is probably the best way to solve this issue.

If you're going to manually adjust transforms to have entities follow another, watch out for: Jitter when making the camera follow a physics object

from bevy_xpbd.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.