From ee403e5de95726a1e8d92283b4e9141725153d0a Mon Sep 17 00:00:00 2001 From: Boscop Date: Fri, 4 Nov 2016 10:28:51 +0100 Subject: [PATCH 1/5] add b2PolygonShape_Set() --- .../Box2D/Collision/Shapes/c_b2PolygonShape.cpp | 4 ++++ liquidfun-c/Box2D/Collision/Shapes/c_b2PolygonShape.h | 1 + src/box2d/collision/shapes/polygon_shape.rs | 10 ++++++++++ 3 files changed, 15 insertions(+) diff --git a/liquidfun-c/Box2D/Collision/Shapes/c_b2PolygonShape.cpp b/liquidfun-c/Box2D/Collision/Shapes/c_b2PolygonShape.cpp index f495615..21fa152 100644 --- a/liquidfun-c/Box2D/Collision/Shapes/c_b2PolygonShape.cpp +++ b/liquidfun-c/Box2D/Collision/Shapes/c_b2PolygonShape.cpp @@ -26,6 +26,10 @@ extern "C" { self->SetAsBox(hx, hy, center, angle); } + void b2PolygonShape_Set(b2PolygonShape* self, const b2Vec2* vertices, int32 count) { + self->Set(vertices, count); + } + b2Shape* b2PolygonShape_Upcast(b2PolygonShape* self) { return static_cast(reinterpret_cast(self)); } diff --git a/liquidfun-c/Box2D/Collision/Shapes/c_b2PolygonShape.h b/liquidfun-c/Box2D/Collision/Shapes/c_b2PolygonShape.h index 4259a27..c0afce2 100644 --- a/liquidfun-c/Box2D/Collision/Shapes/c_b2PolygonShape.h +++ b/liquidfun-c/Box2D/Collision/Shapes/c_b2PolygonShape.h @@ -11,6 +11,7 @@ extern "C" { int32 b2PolygonShape_GetVertexCount(const b2PolygonShape* self); void b2PolygonShape_SetAsBox(b2PolygonShape* self, float32 hx, float32 hy); void b2PolygonShape_SetAsBox_Oriented(b2PolygonShape* self, float32 hx, float32 hy, const b2Vec2& center, float32 angle); + void b2PolygonShape_Set(b2PolygonShape* self, const b2Vec2* vertices, int32 count); b2Shape* b2PolygonShape_Upcast(b2PolygonShape* self); diff --git a/src/box2d/collision/shapes/polygon_shape.rs b/src/box2d/collision/shapes/polygon_shape.rs index 4198e35..e78412a 100644 --- a/src/box2d/collision/shapes/polygon_shape.rs +++ b/src/box2d/collision/shapes/polygon_shape.rs @@ -11,6 +11,7 @@ extern { fn b2PolygonShape_New() -> *mut B2PolygonShape; 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_Set(ptr: *mut B2PolygonShape, vertices: *const Vec2, count: Int32); fn b2PolygonShape_Upcast(ptr: *mut B2PolygonShape) -> *mut B2Shape; } @@ -79,6 +80,15 @@ impl PolygonShape { b2PolygonShape_SetAsBox_Oriented(self.ptr, hx, hy, center, angle); } } + + /// Build vertices to represent an axis-aligned box centered on the local origin. + /// @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 { From 80eff3af24f0d7d51352235822704c316670d338 Mon Sep 17 00:00:00 2001 From: Boscop Date: Fri, 4 Nov 2016 15:19:18 +0100 Subject: [PATCH 2/5] add Body.set_transform() --- liquidfun-c/Box2D/Collision/Shapes/c_b2PolygonShape.cpp | 8 ++++---- liquidfun-c/Box2D/Collision/Shapes/c_b2PolygonShape.h | 3 +-- liquidfun-c/Box2D/Dynamics/c_b2Body.cpp | 4 ++++ liquidfun-c/Box2D/Dynamics/c_b2Body.h | 1 + src/box2d/collision/shapes/polygon_shape.rs | 2 +- src/box2d/dynamics/body.rs | 6 ++++++ src/lib.rs | 2 ++ 7 files changed, 19 insertions(+), 7 deletions(-) diff --git a/liquidfun-c/Box2D/Collision/Shapes/c_b2PolygonShape.cpp b/liquidfun-c/Box2D/Collision/Shapes/c_b2PolygonShape.cpp index 21fa152..9c31624 100644 --- a/liquidfun-c/Box2D/Collision/Shapes/c_b2PolygonShape.cpp +++ b/liquidfun-c/Box2D/Collision/Shapes/c_b2PolygonShape.cpp @@ -26,12 +26,12 @@ extern "C" { self->SetAsBox(hx, hy, center, angle); } - void b2PolygonShape_Set(b2PolygonShape* self, const b2Vec2* vertices, int32 count) { - self->Set(vertices, count); - } - b2Shape* b2PolygonShape_Upcast(b2PolygonShape* self) { return static_cast(reinterpret_cast(self)); } + void b2PolygonShape_Set(b2PolygonShape* self, const b2Vec2* vertices, int32 count) { + self->Set(vertices, count); + } + } // extern C \ No newline at end of file diff --git a/liquidfun-c/Box2D/Collision/Shapes/c_b2PolygonShape.h b/liquidfun-c/Box2D/Collision/Shapes/c_b2PolygonShape.h index c0afce2..0280755 100644 --- a/liquidfun-c/Box2D/Collision/Shapes/c_b2PolygonShape.h +++ b/liquidfun-c/Box2D/Collision/Shapes/c_b2PolygonShape.h @@ -11,9 +11,8 @@ extern "C" { int32 b2PolygonShape_GetVertexCount(const b2PolygonShape* self); void b2PolygonShape_SetAsBox(b2PolygonShape* self, float32 hx, float32 hy); void b2PolygonShape_SetAsBox_Oriented(b2PolygonShape* self, float32 hx, float32 hy, const b2Vec2& center, float32 angle); - void b2PolygonShape_Set(b2PolygonShape* self, const b2Vec2* vertices, int32 count); b2Shape* b2PolygonShape_Upcast(b2PolygonShape* self); - + void b2PolygonShape_Set(b2PolygonShape* self, const b2Vec2* vertices, int32 count); #ifdef __cplusplus } // extern C diff --git a/liquidfun-c/Box2D/Dynamics/c_b2Body.cpp b/liquidfun-c/Box2D/Dynamics/c_b2Body.cpp index d3e1adb..02035b5 100644 --- a/liquidfun-c/Box2D/Dynamics/c_b2Body.cpp +++ b/liquidfun-c/Box2D/Dynamics/c_b2Body.cpp @@ -39,5 +39,9 @@ extern "C" { return self->GetLocalPoint(worldPoint); } + void b2Body_SetTransform(b2Body* self, const b2Vec2& position, float32 angle) { + self->SetTransform(position, angle); + } + } // extern C diff --git a/liquidfun-c/Box2D/Dynamics/c_b2Body.h b/liquidfun-c/Box2D/Dynamics/c_b2Body.h index 162d8a7..fb28936 100644 --- a/liquidfun-c/Box2D/Dynamics/c_b2Body.h +++ b/liquidfun-c/Box2D/Dynamics/c_b2Body.h @@ -14,6 +14,7 @@ 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); #ifdef __cplusplus } // extern C diff --git a/src/box2d/collision/shapes/polygon_shape.rs b/src/box2d/collision/shapes/polygon_shape.rs index e78412a..c39a894 100644 --- a/src/box2d/collision/shapes/polygon_shape.rs +++ b/src/box2d/collision/shapes/polygon_shape.rs @@ -11,8 +11,8 @@ extern { fn b2PolygonShape_New() -> *mut B2PolygonShape; 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_Set(ptr: *mut B2PolygonShape, vertices: *const Vec2, count: Int32); 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 diff --git a/src/box2d/dynamics/body.rs b/src/box2d/dynamics/body.rs index eee3090..7c401f1 100644 --- a/src/box2d/dynamics/body.rs +++ b/src/box2d/dynamics/body.rs @@ -111,6 +111,7 @@ extern { 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); } /// A rigid body. These are created via b2World::CreateBody. @@ -213,4 +214,9 @@ impl Body { } } + pub fn set_transform(&mut self, position: &Vec2, angle: f32) { + unsafe { + b2Body_SetTransform(self.ptr, position, angle) + } + } } diff --git a/src/lib.rs b/src/lib.rs index 7b03920..b2e7187 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -50,6 +50,8 @@ //! } //! ``` +#![allow(renamed_and_removed_lints)] + extern crate libc; #[macro_use] extern crate bitflags; From 708bd0779c4a70d76f7b210fe955788dc1a1f110 Mon Sep 17 00:00:00 2001 From: Boscop Date: Mon, 7 Nov 2016 08:50:45 +0100 Subject: [PATCH 3/5] Body.set_linear_velocity() --- liquidfun-c/Box2D/Dynamics/c_b2Body.cpp | 4 ++++ liquidfun-c/Box2D/Dynamics/c_b2Body.h | 1 + src/box2d/dynamics/body.rs | 7 +++++++ 3 files changed, 12 insertions(+) diff --git a/liquidfun-c/Box2D/Dynamics/c_b2Body.cpp b/liquidfun-c/Box2D/Dynamics/c_b2Body.cpp index 02035b5..46f6ab5 100644 --- a/liquidfun-c/Box2D/Dynamics/c_b2Body.cpp +++ b/liquidfun-c/Box2D/Dynamics/c_b2Body.cpp @@ -43,5 +43,9 @@ extern "C" { self->SetTransform(position, angle); } + void b2Body_SetLinearVelocity(b2Body* self, const b2Vec2& v) { + self->SetLinearVelocity(v); + } + } // extern C diff --git a/liquidfun-c/Box2D/Dynamics/c_b2Body.h b/liquidfun-c/Box2D/Dynamics/c_b2Body.h index fb28936..ccaba11 100644 --- a/liquidfun-c/Box2D/Dynamics/c_b2Body.h +++ b/liquidfun-c/Box2D/Dynamics/c_b2Body.h @@ -15,6 +15,7 @@ extern "C" { 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); #ifdef __cplusplus } // extern C diff --git a/src/box2d/dynamics/body.rs b/src/box2d/dynamics/body.rs index 7c401f1..041653e 100644 --- a/src/box2d/dynamics/body.rs +++ b/src/box2d/dynamics/body.rs @@ -112,6 +112,7 @@ extern { 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); } /// A rigid body. These are created via b2World::CreateBody. @@ -219,4 +220,10 @@ impl Body { b2Body_SetTransform(self.ptr, position, angle) } } + + pub fn set_linear_velocity(&mut self, v: &Vec2) { + unsafe { + b2Body_SetLinearVelocity(self.ptr, v) + } + } } From 4eb0b9c6a3e50016e50752050ed578bd7f2b45f3 Mon Sep 17 00:00:00 2001 From: Boscop Date: Tue, 8 Nov 2016 14:14:03 +0100 Subject: [PATCH 4/5] Body.get_linear_velocity() --- liquidfun-c/Box2D/Dynamics/c_b2Body.cpp | 16 ++++++++++------ liquidfun-c/Box2D/Dynamics/c_b2Body.h | 1 + src/box2d/collision/shapes/polygon_shape.rs | 2 +- src/box2d/dynamics/body.rs | 7 +++++++ 4 files changed, 19 insertions(+), 7 deletions(-) diff --git a/liquidfun-c/Box2D/Dynamics/c_b2Body.cpp b/liquidfun-c/Box2D/Dynamics/c_b2Body.cpp index 46f6ab5..64ced60 100644 --- a/liquidfun-c/Box2D/Dynamics/c_b2Body.cpp +++ b/liquidfun-c/Box2D/Dynamics/c_b2Body.cpp @@ -39,13 +39,17 @@ extern "C" { return self->GetLocalPoint(worldPoint); } - void b2Body_SetTransform(b2Body* self, const b2Vec2& position, float32 angle) { - self->SetTransform(position, angle); - } + 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); + } - void b2Body_SetLinearVelocity(b2Body* self, const b2Vec2& v) { - self->SetLinearVelocity(v); - } + b2Vec2 b2Body_GetLinearVelocity(b2Body* self) { + return self->GetLinearVelocity(); + } } // extern C diff --git a/liquidfun-c/Box2D/Dynamics/c_b2Body.h b/liquidfun-c/Box2D/Dynamics/c_b2Body.h index ccaba11..f42f8eb 100644 --- a/liquidfun-c/Box2D/Dynamics/c_b2Body.h +++ b/liquidfun-c/Box2D/Dynamics/c_b2Body.h @@ -16,6 +16,7 @@ extern "C" { 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); + b2Vec2 b2Body_GetLinearVelocity(b2Body* self); #ifdef __cplusplus } // extern C diff --git a/src/box2d/collision/shapes/polygon_shape.rs b/src/box2d/collision/shapes/polygon_shape.rs index c39a894..b4d119a 100644 --- a/src/box2d/collision/shapes/polygon_shape.rs +++ b/src/box2d/collision/shapes/polygon_shape.rs @@ -81,7 +81,7 @@ impl PolygonShape { } } - /// Build vertices to represent an axis-aligned box centered on the local origin. + /// Build a convex polygon. /// @param hx the half-width. /// @param hy the half-height. pub fn set(&mut self, vertices: &[Vec2]) { diff --git a/src/box2d/dynamics/body.rs b/src/box2d/dynamics/body.rs index 041653e..5cb06eb 100644 --- a/src/box2d/dynamics/body.rs +++ b/src/box2d/dynamics/body.rs @@ -113,6 +113,7 @@ extern { 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: *mut B2Body) -> Vec2; } /// A rigid body. These are created via b2World::CreateBody. @@ -226,4 +227,10 @@ impl Body { b2Body_SetLinearVelocity(self.ptr, v) } } + + pub fn get_linear_velocity(&mut self) -> Vec2 { + unsafe { + b2Body_GetLinearVelocity(self.ptr) + } + } } From 616f2cfaa66f7f3c456fb967ee35020f07ca4606 Mon Sep 17 00:00:00 2001 From: Boscop Date: Tue, 8 Nov 2016 14:51:57 +0100 Subject: [PATCH 5/5] b2Body_GetLinearVelocity() returns &Vec2 --- liquidfun-c/Box2D/Dynamics/c_b2Body.cpp | 2 +- liquidfun-c/Box2D/Dynamics/c_b2Body.h | 2 +- src/box2d/dynamics/body.rs | 8 ++++---- tests/hello-world.rs | 3 ++- 4 files changed, 8 insertions(+), 7 deletions(-) diff --git a/liquidfun-c/Box2D/Dynamics/c_b2Body.cpp b/liquidfun-c/Box2D/Dynamics/c_b2Body.cpp index 64ced60..cc80190 100644 --- a/liquidfun-c/Box2D/Dynamics/c_b2Body.cpp +++ b/liquidfun-c/Box2D/Dynamics/c_b2Body.cpp @@ -47,7 +47,7 @@ extern "C" { self->SetLinearVelocity(v); } - b2Vec2 b2Body_GetLinearVelocity(b2Body* self) { + const b2Vec2& b2Body_GetLinearVelocity(const b2Body* self) { return self->GetLinearVelocity(); } diff --git a/liquidfun-c/Box2D/Dynamics/c_b2Body.h b/liquidfun-c/Box2D/Dynamics/c_b2Body.h index f42f8eb..dbb1278 100644 --- a/liquidfun-c/Box2D/Dynamics/c_b2Body.h +++ b/liquidfun-c/Box2D/Dynamics/c_b2Body.h @@ -16,7 +16,7 @@ extern "C" { 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); - b2Vec2 b2Body_GetLinearVelocity(b2Body* self); + const b2Vec2& b2Body_GetLinearVelocity(const b2Body* self); #ifdef __cplusplus } // extern C diff --git a/src/box2d/dynamics/body.rs b/src/box2d/dynamics/body.rs index 5cb06eb..ae2bed0 100644 --- a/src/box2d/dynamics/body.rs +++ b/src/box2d/dynamics/body.rs @@ -104,16 +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: *mut B2Body) -> Vec2; + fn b2Body_GetLinearVelocity(this: *const B2Body) -> &Vec2; } /// A rigid body. These are created via b2World::CreateBody. @@ -228,7 +228,7 @@ impl Body { } } - pub fn get_linear_velocity(&mut self) -> Vec2 { + pub fn get_linear_velocity(&self) -> &Vec2 { unsafe { b2Body_GetLinearVelocity(self.ptr) } diff --git a/tests/hello-world.rs b/tests/hello-world.rs index 6e767b6..e2bda85 100644 --- a/tests/hello-world.rs +++ b/tests/hello-world.rs @@ -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