Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added some methods #1

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions liquidfun-c/Box2D/Collision/Shapes/c_b2PolygonShape.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,8 @@ extern "C" {
return static_cast<b2Shape*>(reinterpret_cast<b2PolygonShape*>(self));
}

void b2PolygonShape_Set(b2PolygonShape* self, const b2Vec2* vertices, int32 count) {
self->Set(vertices, count);
}

} // extern C
2 changes: 1 addition & 1 deletion liquidfun-c/Box2D/Collision/Shapes/c_b2PolygonShape.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ extern "C" {
void b2PolygonShape_SetAsBox(b2PolygonShape* self, float32 hx, float32 hy);
void b2PolygonShape_SetAsBox_Oriented(b2PolygonShape* self, float32 hx, float32 hy, const b2Vec2& center, float32 angle);
b2Shape* b2PolygonShape_Upcast(b2PolygonShape* self);

void b2PolygonShape_Set(b2PolygonShape* self, const b2Vec2* vertices, int32 count);

#ifdef __cplusplus
} // extern C
Expand Down
12 changes: 12 additions & 0 deletions liquidfun-c/Box2D/Dynamics/c_b2Body.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,17 @@ extern "C" {
return self->GetLocalPoint(worldPoint);
}

void b2Body_SetTransform(b2Body* self, const b2Vec2& position, float32 angle) {
self->SetTransform(position, angle);
}

void b2Body_SetLinearVelocity(b2Body* self, const b2Vec2& v) {
self->SetLinearVelocity(v);
}

const b2Vec2& b2Body_GetLinearVelocity(const b2Body* self) {
return self->GetLinearVelocity();
}

} // extern C

3 changes: 3 additions & 0 deletions liquidfun-c/Box2D/Dynamics/c_b2Body.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ extern "C" {
void* b2Body_GetUserData(const b2Body* self);
b2World* b2Body_GetWorld(b2Body* self);
b2Vec2 b2Body_GetLocalPoint(const b2Body* self, const b2Vec2& worldPoint);
void b2Body_SetTransform(b2Body* self, const b2Vec2& position, float32 angle);
void b2Body_SetLinearVelocity(b2Body* self, const b2Vec2& v);
const b2Vec2& b2Body_GetLinearVelocity(const b2Body* self);

#ifdef __cplusplus
} // extern C
Expand Down
10 changes: 10 additions & 0 deletions src/box2d/collision/shapes/polygon_shape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ extern {
fn b2PolygonShape_SetAsBox(ptr: *mut B2PolygonShape, hx: Float32, hy: Float32);
fn b2PolygonShape_SetAsBox_Oriented(ptr: *mut B2PolygonShape, hx: Float32, hy: Float32, center: &Vec2, angle: Float32);
fn b2PolygonShape_Upcast(ptr: *mut B2PolygonShape) -> *mut B2Shape;
fn b2PolygonShape_Set(ptr: *mut B2PolygonShape, vertices: *const Vec2, count: Int32);
}

/// A convex polygon. It is assumed that the interior of the polygon is to
Expand Down Expand Up @@ -79,6 +80,15 @@ impl PolygonShape {
b2PolygonShape_SetAsBox_Oriented(self.ptr, hx, hy, center, angle);
}
}

/// Build a convex polygon.
/// @param hx the half-width.
/// @param hy the half-height.
pub fn set(&mut self, vertices: &[Vec2]) {
unsafe {
b2PolygonShape_Set(self.ptr, vertices.as_ptr(), vertices.len() as Int32);
}
}
}

impl Drop for PolygonShape {
Expand Down
24 changes: 22 additions & 2 deletions src/box2d/dynamics/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,16 @@ pub enum B2Body {}
extern {
fn b2Body_CreateFixture_FromShape(this: *mut B2Body, shape: *const B2Shape, density: Float32) -> *mut B2Fixture;
fn b2Body_CreateFixture(this: *mut B2Body, def: *mut FixtureDef) -> *mut B2Fixture;
fn b2Body_GetAngle(this: *mut B2Body) -> Float32;
fn b2Body_GetAngle(this: *const B2Body) -> Float32;
fn b2Body_GetFixtureList(this: *mut B2Body) -> *mut B2Fixture;
fn b2Body_GetNext(this: *mut B2Body) -> *mut B2Body;
fn b2Body_GetPosition(this: *mut B2Body) -> &Vec2;
fn b2Body_GetPosition(this: *const B2Body) -> &Vec2;
fn b2Body_GetUserData(this: *const B2Body) -> usize;
fn b2Body_GetWorld(this: *const B2Body) -> *mut B2World;
fn b2Body_GetLocalPoint(this: *const B2Body, worldPoint: &Vec2) -> Vec2;
fn b2Body_SetTransform(this: *mut B2Body, position: &Vec2, angle: Float32);
fn b2Body_SetLinearVelocity(this: *mut B2Body, v: &Vec2);
fn b2Body_GetLinearVelocity(this: *const B2Body) -> &Vec2;
}

/// A rigid body. These are created via b2World::CreateBody.
Expand Down Expand Up @@ -213,4 +216,21 @@ impl Body {
}
}

pub fn set_transform(&mut self, position: &Vec2, angle: f32) {
unsafe {
b2Body_SetTransform(self.ptr, position, angle)
}
}

pub fn set_linear_velocity(&mut self, v: &Vec2) {
unsafe {
b2Body_SetLinearVelocity(self.ptr, v)
}
}

pub fn get_linear_velocity(&self) -> &Vec2 {
unsafe {
b2Body_GetLinearVelocity(self.ptr)
}
}
}
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
//! }
//! ```

#![allow(renamed_and_removed_lints)]

extern crate libc;
#[macro_use]
extern crate bitflags;
Expand Down
3 changes: 2 additions & 1 deletion tests/hello-world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,9 @@ fn hello_world() {
// Now print the position and angle of the body.
let position = body.get_position();
let angle = body.get_angle();
let vel = body.get_linear_velocity();

println!("{:?} angle: {:?}", position, angle);
println!("{:?} angle: {:?}, vel: {:?}", position, angle, vel);
}

// When the world destructor is called, all bodies and joints are freed. This can
Expand Down