Crate wrapped2d

source ·
Expand description

Bod2D for Rust

You won’t find a lot of information about Box2D itself here, look at the official website instead.

World

use wrapped2d::b2;
use wrapped2d::user_data::NoUserData;

let gravity = b2::Vec2 { x: 0., y: -10. };
let world = b2::World::<NoUserData>::new(&gravity);

Handles

Bodies, fixtures and joints are accessed through handles and their borrowing is dynamically checked by RefCells.

let mut def = b2::BodyDef {
    body_type: b2::BodyType::Dynamic,
    position: b2::Vec2 { x: 10., y: 10. },
    .. b2::BodyDef::new()
};

let handle = world.create_body(&def);
let mut body = world.body_mut(handle);

let shape = b2::PolygonShape::new_box(0.5, 0.5);

let handle = body.create_fast_fixture(&shape, 2.);
let fixture = body.fixture(handle);

User Data

You can provide a unit struct to specify the user data types that will be used for bodies, joints and fixtures. For example:

use wrapped2d::b2;
use wrapped2d::user_data::*;

pub type ObjectId = u64;

pub struct CustomUserData;
impl UserDataTypes for CustomUserData {
    type BodyData = Option<ObjectId>;
    type JointData = ();
    type FixtureData = FixtureKind;
}
 
pub type World = b2::World<CustomUserData>;
 
pub enum FixtureKind {
    Wood,
    Metal,
}
 
fn main() {
    let mut world = World::new(&b2::Vec2 { x: 0., y: -10. });
     
    let def = b2::BodyDef {
        body_type: b2::BodyType::Dynamic,
        position: b2::Vec2 { x: 0., y: -15. },
        ..b2::BodyDef::new()
    };
 
    let h1 = world.create_body(&def); // will use `Default` user data (`None` here)
    let h2 = world.create_body_with(&def, Some(2)); // specifying user data for the body
 
    let user_data = world.body(h2).user_data(); // access the body user data
}

Modules