bevy_xpbd_2d_interp

Crates.iobevy_xpbd_2d_interp
lib.rsbevy_xpbd_2d_interp
version0.1.2
sourcesrc
created_at2023-11-08 20:17:20.859732
updated_at2024-02-21 16:48:07.554471
descriptionA simple library for interpolation of bevy_xpbd rigidbodies
homepage
repositoryhttps://github.com/rubengrim/bevy_xpbd_interp
max_upload_size
id1029369
size127,380
Ruben (rubengrim)

documentation

README

Bevy XPBD Interp is a simple library for interpolation of bevy_xpbd rigidbodies. It operates by interpolating between the position/rotation of the current and previous physics states based on how much time has accumulated since the last physics update. The interpolated value is stored in the Transform of some separate entity that may hold meshes/cameras etc. Physics objects essentially need to be split up into one entity being affected by physics, and one entity being rendered.

In a lot of cases interpolation makes a noticeable difference at normal/higher physics update frequencies, eg. 60hz, but you'll see perfectly smooth movement even at 1hz.

Note: bevy_xpbd is split into a 2d and a 3d crate. This library does the same and is split up into bevy_xpbd_2d_interp and bevy_xpbd_3d_interp.

Usage

Add bevy_xpbd and bevy_xpbd_interp as dependencies:

[dependencies]  
bevy_xpbd_2d = "0.4"
bevy_xpbd_2d_interp = "0.1.2"
# or
[dependencies]  
bevy_xpbd_3d = "0.4"
bevy_xpbd_3d_interp = "0.1.2"

Then add XPBDInterpolationPlugin to your app:

app.add_plugins(XPBDInterpolationPlugin);

Now you can add the InterpolatedPosition and/or InterpolatedRotation components to any entity with a Transform:

// The entity being affected by bevy_xpbd
let physics_entity = commands
    .spawn((
        RigidBody::Kinematic,
        Position::default(),
        Rotation::default(),
    ))
    .id();

// Rendered box that uses the interpolated position/rotation
commands.spawn((
    PbrBundle {
        mesh: mesh_assets.add(Mesh::from(shape::Box::new(1.0, 1.0, 1.0))),
        transform: Transform::default(), 
        ..default()
    }, 
    InterpolatedPosition::from_source(physics_entity),
    InterpolatedRotation::from_source(physics_entity),
));

See 'crates/bevy_xpbd_2d_interp/examples/box_2d.rs' and 'crates/bevy_xpbd_3d_interp/examples/box_3d.rs' for full examples. Run them with cargo run --example box_2d/box_3d.

Supported versions

Bevy Bevy XPBD Bevy XPBD Interp
0.13 0.4 0.1.2
0.12 0.3.2 0.1.1
0.12 0.3.1 0.1.0
Commit count: 12

cargo fmt