[][src]Crate wrapped2d

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 UserData;
impl UserDataTypes for UserData {
    type BodyData = Option<ObjectId>;
    type JointData = ();
    type FixtureData = FixtureKind;
}
 
pub type World = b2::World<UserData>;
 
pub enum FixtureKind {
    Wood,
    Metal,
}
 
// [ ... elsewhere ... ]
// let h = world.create_body(&def); // will use `Default` user data (`None` here)
// let h = world.create_body_with(&def, Some(object_id)); // specifying user data for the body
// let user_data = world.body(h).user_data(); // access the body user data

Modules

b2
collision
common
dynamics
user_data