Coder Social home page Coder Social logo

Manually set mass about bevy_xpbd HOT 6 CLOSED

jondolf avatar jondolf commented on May 18, 2024
Manually set mass

from bevy_xpbd.

Comments (6)

stargazing-dino avatar stargazing-dino commented on May 18, 2024 1

Thanks for the help and explanation ! Setting the inertia makes a lot of sense and works perfect for my case 🙂

Also haha thank you for the Vector correction. I consider myself still new to rust so I'm just happy it compiled :P but i'll make that change as well

from bevy_xpbd.

Jondolf avatar Jondolf commented on May 18, 2024

Do the planets have colliders? If they do, then the mass properties of the colliders are computed and added on top of the body's existing mass properties, like in rapier. This is documented here.

You can set the mass properties of the colliders to zero by adding ColliderMassProperties::ZERO. With that I am not seeing any mass changing in my examples.

from bevy_xpbd.

stargazing-dino avatar stargazing-dino commented on May 18, 2024

That worked great for me ! Thank you. I'm enjoying the library :)

from bevy_xpbd.

stargazing-dino avatar stargazing-dino commented on May 18, 2024

tldr: Nvm, I'm got it :P

I feel like I'm silly for doing this but if I try to do the same thing of setting ColliderMassProperties::ZERO with a RigidBody::Dynamic it doesn't work and my little ball disappears off the screen before I've even added any forces to the ball.

let shape = shape::UVSphere {
    radius: 1.0,
    ..default()
};

commands.spawn((
    PbrBundle {
        mesh: meshes.add(shape.into()),
        material: materials.add(Color::rgb(0.8, 0.7, 0.6).into()),
        transform: Transform::from_xyz(0.0, 0.0, 0.0),
        ..default()
    },
    Collider::ball(1.0),
    ColliderMassProperties::ZERO,
    // RigidBody::Kinematic, // <- Works
    // RigidBody::Dynamic, // <- hops off the screen before I've started my physics stuff 
    Position(Vector::new(14.0, 0.0, 0.0)),
    ExternalForce::default(),
    Mass(1.0),
    Friction::new(6.0),
    Junk {},
));

Again, I'm crazy dumb when it comes to physics anything so feel free to let me know if I'm just doing stuff wrong.

Edit

So I guess it just needs a non-zero density in order to not freak out:

ColliderMassProperties {
    density: 1.0,
    ..default()
},
Mass(1.0),
RigidBody::Dynamic,

Which makes some sense. Anyways, I guess the problem now is that the mass is changing from the 1.0 I set it to to something like 6.4 😭

Edit 2

I guess setting the density to something super low actually fixes it and I get my mass of ~1.0 lol.

ColliderMassProperties {
    density: 0.00001, // honestly, just non-zero prob
    ..default()
},

from bevy_xpbd.

Jondolf avatar Jondolf commented on May 18, 2024

I'm pretty sure the problem isn't actually the density of the collider, it's that you only add Mass and not Inertia as well.

Angular inertia controls how much effort it requires to rotate a body, and when there's zero inertia, there is probably some kind of division by zero happening, which breaks the simulation and causes the bodies to accelerate into infinity (making them disappear).

So I would add something like this:

(
    ColliderMassProperties::ZERO,
    Mass(1.0),
    Inertia(1.0), // In 2D
    Inertia(Mat3::IDENTITY) // In 3D
    // ...other components
)

You can of course set the inertia to whatever you want, as long as it's greater than 0.

Thanks for the issue though, I definitely need to make the docs more explicit about this. I might also just add a warning when the engine detects a dynamic body with zero mass/inertia.

from bevy_xpbd.

Jondolf avatar Jondolf commented on May 18, 2024

Also FYI, it looks like you might be using the crate's math types like Vector. This is fine, but you can also just use the normal types like Vec2 or Vec3. The special math types are there because the crate needs to use different types depending on feature flags like 2d/3d and f32/f64, but in actual usage you already know the types because you have specified the features.

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.