#physics #interpolation #bevy #gamedev #physics-2d #xpbd

bevy_xpbd_2d_interp

A simple library for interpolation of bevy_xpbd rigidbodies

3 releases

0.1.2 Feb 21, 2024
0.1.1 Nov 9, 2023
0.1.0 Nov 8, 2023

#890 in Game dev

Download history 78/week @ 2024-02-15 63/week @ 2024-02-22 8/week @ 2024-02-29 4/week @ 2024-03-07 40/week @ 2024-03-28 21/week @ 2024-04-04

61 downloads per month

MIT/Apache

32KB
213 lines

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

Dependencies

~20–49MB
~772K SLoC