16 unstable releases (6 breaking)

0.7.1 Aug 13, 2020
0.7.0 Jun 29, 2020
0.6.1 Jun 2, 2020
0.5.1 Mar 4, 2020
0.4.0 Oct 31, 2019

#1 in Simulation

Download history 17/week @ 2020-06-08 29/week @ 2020-06-15 4/week @ 2020-06-22 49/week @ 2020-06-29 20/week @ 2020-07-06 40/week @ 2020-07-13 32/week @ 2020-07-20 3/week @ 2020-07-27 14/week @ 2020-08-03 66/week @ 2020-08-10 37/week @ 2020-08-17 39/week @ 2020-08-24 28/week @ 2020-08-31 57/week @ 2020-09-07 14/week @ 2020-09-14 10/week @ 2020-09-21

134 downloads per month


175K SLoC

C++ 158K SLoC // 0.2% comments Rust 16K SLoC // 0.0% comments Shell 35 SLoC

🎳 physx

Build Status Crates.io Docs Contributor Covenant Embark

This is a work in progress 🚧

physx is intended to be an easy to use high-level wrapper for the physx-sys bindings. The goal of this is to make ownership clearer and leverage the safety of Rust.

The overall goal is to maintain a close mapping to the underlying PhysX API while improving safety and reliability of the code. This means, for example, that we do not expose the PxLoadExtensions() function but rather attach this to the Physics builder.

Please also see the repository containing an unsafe low-level binding.


Basic usage

const PX_PHYSICS_VERSION: u32 = physx::version(4, 1, 1);
let mut foundation = Foundation::new(PX_PHYSICS_VERSION);

let mut physics = PhysicsBuilder::default()
    .load_extensions(false) // Flip this flag to load extensions during setup
    .build(&mut foundation);

let mut scene = physics.create_scene(
        .set_gravity(glm::vec3(0.0, -9.81, 0.0))

// Your physics simulation goes here

For a full example, have a look at the bouncing ball example and compare it to the raw sys example.

How it works

Wrapping a C++ API in Rust is not straightforward, and requires some extra steps to work. The first, and most basic one is creating a C wrapper over the C++ API. Using C as an intermediary allows us to leverage a stable ABI through which C++ and Rust can communicate. The physx-sys crate provides this interface.

Since PhysX makes significant use of inheritance, there is no straightforward mapping to Rust code. To simulate the inheritance, we have a pointer-wrapper called PxType<T>. We implement the functions on each wrapped PxType<PxRgidiActor> and expose an alias RigidActor.

The deref pattern is used to simulate inheritance. For example RigidBody::set_angular_damping, can be called from the child RigidDynamic because RigidDynamic implements Deref<Target = RigidBody>.

// `set_angular_damping` is not defined in `RigidDynamic`, it is defined in `RidigBody`.
// This works because `Deref` is used to emulate inheritance.
let mut sphere_actor: RigidDynamic = unsafe { physics.create_dynamic(..) };
// The verbose example to show what happens behind the scenes.
let mut sphere_actor: RigidDynamic = unsafe { physics.create_dynamic(..) };
    // `RidigDynamic` implements `Deref/DerefMut` to a `RigidBody`.
    let sphere_actor: &mut RigidBody = &mut *sphere_actor;


Licensed under either of

at your option.

Note that the PhysX C++ SDK has it's own BSD 3 license and depends on additional C++ third party libraries.


Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.