From 99beaab15858365acd9357da2cf679c82ec63d75 Mon Sep 17 00:00:00 2001 From: Logan Date: Thu, 11 Jan 2024 18:47:48 -0700 Subject: [PATCH 1/4] simplified example of shape order issue --- sample/Main.hx | 2 +- sample/state/ListenerState.hx | 81 +++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 sample/state/ListenerState.hx diff --git a/sample/Main.hx b/sample/Main.hx index fa8fbd6..a2bbc10 100644 --- a/sample/Main.hx +++ b/sample/Main.hx @@ -46,7 +46,7 @@ class Main extends BaseApp { // Set up our Sample States sample_states = [ - PolygonState, StackingState, MultiShapeState, ShapesState, GroupsState, StaticState, LinecastState, Linecast2State, TileMapState, TileMapState2, + ListenerState, PolygonState, StackingState, MultiShapeState, ShapesState, GroupsState, StaticState, LinecastState, Linecast2State, TileMapState, TileMapState2, BezierState, VerletState ]; index = 0; diff --git a/sample/state/ListenerState.hx b/sample/state/ListenerState.hx new file mode 100644 index 0000000..1e36a3a --- /dev/null +++ b/sample/state/ListenerState.hx @@ -0,0 +1,81 @@ +package state; + +import echo.data.Options.ListenerOptions; +import echo.Material; +import echo.Body; +import echo.World; +import util.Random; + +class ListenerState extends BaseState { + + override public function enter(world:World) { + Main.instance.state_text.text = "Sample: Collision Listener"; + + // Create a material for all the shapes to share + var material:Material = {elasticity: 0.7}; + + var bodyA = new Body({ + x: Random.range(60, world.width - 60), + y: Random.range(0, world.height / 2), + rotation: Random.range(0, 360), + material: material, + shapes: [ + { + type: POLYGON, + radius: Random.range(16, 32), + width: Random.range(16, 48), + height: Random.range(16, 48), + sides: Random.range_int(3, 8), + offset_y: -10, + }, + { + type: POLYGON, + radius: Random.range(16, 32), + width: Random.range(16, 48), + height: Random.range(16, 48), + sides: Random.range_int(3, 8), + offset_y: 10, + } + ] + }); + world.add(bodyA); + + // Add a Physics body at the bottom of the screen for the other Physics Bodies to stack on top of + // This body has a mass of 0, so it acts as an immovable object + var bodyB = new Body({ + mass: STATIC, + x: world.width / 5, + y: world.height - 40, + material: material, + rotation: 5, + shape: { + type: RECT, + width: world.width, + height: 20 + } + }); + world.add(bodyB); + + var dbgOpts:ListenerOptions = { + enter: (a, b, data) -> { + trace('bodyA == listener `a`: ${bodyA == a}'); + trace('bodyA shape == data `shape a`: ${bodyA.shape == data[0].sa}'); + trace('bodyB == listener `b`: ${bodyB == b}'); + trace('bodyB shape == data `shape b`: ${bodyB.shape == data[0].sb}'); + }, + }; + + // Create a listener for collisions between the Physics Bodies + world.listen(bodyA, bodyB, dbgOpts); + } + + override function step(world:World, dt:Float) { + // Reset any off-screen Bodies + world.for_each((member) -> { + if (offscreen(member, world)) { + member.velocity.set(0, 0); + member.set_position(Random.range(0, world.width), 0); + } + }); + } +} \ No newline at end of file From bdf99d8d34c19f4a9cf2d05004aa2d925911761a Mon Sep 17 00:00:00 2001 From: Logan Date: Mon, 15 Jan 2024 21:23:40 -0700 Subject: [PATCH 2/4] more tinkering. physics oddities isolated to circles --- echo/Shape.hx | 6 +-- echo/shape/Circle.hx | 6 +-- echo/shape/Polygon.hx | 8 +-- echo/shape/Rect.hx | 8 +-- echo/util/SAT.hx | 31 +++++++---- sample/Main.hx | 2 +- sample/state/ListenerState.hx | 7 +-- sample/state/OverlappingSpawnState.hx | 78 +++++++++++++++++++++++++++ sample/state/PolygonState.hx | 2 +- sample/state/StaticState.hx | 7 ++- 10 files changed, 124 insertions(+), 31 deletions(-) create mode 100644 sample/state/OverlappingSpawnState.hx diff --git a/echo/Shape.hx b/echo/Shape.hx index 1938a1f..30b0875 100644 --- a/echo/Shape.hx +++ b/echo/Shape.hx @@ -206,11 +206,11 @@ class Shape #if cog implements cog.IComponent #end { public function collides(s:Shape):Null return null; - function collide_rect(r:Rect):Null return null; + function collide_rect(r:Rect, flip:Bool = false):Null return null; - function collide_circle(c:Circle):Null return null; + function collide_circle(c:Circle, flip:Bool = false):Null return null; - function collide_polygon(p:Polygon):Null return null; + function collide_polygon(p:Polygon, flip:Bool = false):Null return null; function toString() { var s = switch (type) { diff --git a/echo/shape/Circle.hx b/echo/shape/Circle.hx index 833e6e3..4f42ae2 100644 --- a/echo/shape/Circle.hx +++ b/echo/shape/Circle.hx @@ -89,11 +89,11 @@ class Circle extends Shape implements Poolable { override inline function collides(s:Shape):Null return s.collide_circle(this); - override inline function collide_rect(r:Rect):Null return r.rect_and_circle(this, true); + override inline function collide_rect(r:Rect, flip:Bool = false):Null return r.rect_and_circle(this, !flip); - override inline function collide_circle(c:Circle):Null return c.circle_and_circle(this); + override inline function collide_circle(c:Circle, flip:Bool = false):Null return c.circle_and_circle(this, !flip); - override inline function collide_polygon(p:Polygon):Null return this.circle_and_polygon(p, true); + override inline function collide_polygon(p:Polygon, flip:Bool = false):Null return this.circle_and_polygon(p, flip); // getters inline function get_radius():Float return local_radius * scale_x; diff --git a/echo/shape/Polygon.hx b/echo/shape/Polygon.hx index add301f..3df13ea 100644 --- a/echo/shape/Polygon.hx +++ b/echo/shape/Polygon.hx @@ -197,13 +197,13 @@ class Polygon extends Shape implements Poolable { return false; } - override inline function collides(s:Shape):Null return s.collide_polygon(this); + override inline function collides(s:Shape):Null return s.collide_polygon(this, true); - override inline function collide_rect(r:Rect):Null return r.rect_and_polygon(this, true); + override inline function collide_rect(r:Rect, flip:Bool = false):Null return r.rect_and_polygon(this, !flip); - override inline function collide_circle(c:Circle):Null return c.circle_and_polygon(this); + override inline function collide_circle(c:Circle, flip:Bool = false):Null return c.circle_and_polygon(this, !flip); - override inline function collide_polygon(p:Polygon):Null return p.polygon_and_polygon(this, true); + override inline function collide_polygon(p:Polygon, flip:Bool = false):Null return this.polygon_and_polygon(p, flip); override inline function get_top():Float { if (count == 0 || vertices[0] == null) return y; diff --git a/echo/shape/Rect.hx b/echo/shape/Rect.hx index 7d5ba23..1658339 100644 --- a/echo/shape/Rect.hx +++ b/echo/shape/Rect.hx @@ -174,13 +174,13 @@ class Rect extends Shape implements Poolable { return false; } - override inline function collides(s:Shape):Null return s.collide_rect(this); + override inline function collides(s:Shape):Null return s.collide_rect(this, true); - override inline function collide_rect(r:Rect):Null return r.rect_and_rect(this); + override inline function collide_rect(r:Rect, flip:Bool = false):Null return this.rect_and_rect(r, flip); - override inline function collide_circle(c:Circle):Null return this.rect_and_circle(c); + override inline function collide_circle(c:Circle, flip:Bool = false):Null return this.rect_and_circle(c, flip); - override inline function collide_polygon(p:Polygon):Null return this.rect_and_polygon(p); + override inline function collide_polygon(p:Polygon, flip:Bool = false):Null return this.rect_and_polygon(p, flip); override function set_parent(?body:Body) { super.set_parent(body); diff --git a/echo/util/SAT.hx b/echo/util/SAT.hx index e3868e0..58809af 100644 --- a/echo/util/SAT.hx +++ b/echo/util/SAT.hx @@ -190,17 +190,17 @@ class SAT { var col:CollisionData = null; if (rect1.rotation != 0 || rect2.rotation != 0) { if (rect1.transformed_rect != null) { - col = rect_and_polygon(rect2, rect1.transformed_rect, flip); + col = rect_and_polygon(rect2, rect1.transformed_rect, !flip); if (col == null) return null; - if (flip) col.sa = rect1; + if (flip) col.sa = rect2; else col.sb = rect1; return col; } if (rect2.transformed_rect != null) { - col = rect_and_polygon(rect1, rect2.transformed_rect, !flip); + col = rect_and_polygon(rect1, rect2.transformed_rect, flip); if (col == null) return null; @@ -318,6 +318,7 @@ class SAT { data1.put(); return data2; } + /** * Test a Rect and a Circle for a Collision. * @param r @@ -327,12 +328,17 @@ class SAT { */ public static function rect_and_circle(r:Rect, c:Circle, flip:Bool = false):Null { if (r.transformed_rect != null && r.rotation != 0) { - var col = circle_and_polygon(c, r.transformed_rect, flip); + var col = circle_and_polygon(c, r.transformed_rect, !flip); if (col == null) return null; - if (flip) col.sa = r; - else col.sb = r; + // collisions used the transformed rect, set the collision data's shape back + // to the original rect + if (!flip) { + col.sa = r; + } else { + col.sb = r; + } return col; } @@ -407,8 +413,13 @@ class SAT { if (col == null) return null; - if (flip) col.sb = r; - else col.sa = r; + // collisions were done with a polygon derrived from the provided rect + // so we need to set our collision data shape back to the original rectangle + if (flip) { + col.sb = r; + } else { + col.sa = r; + } return col; } @@ -518,7 +529,7 @@ class SAT { col.overlap = Math.abs(col.overlap); - if (flip) { + if (!flip) { col.normal.negate(); } @@ -579,7 +590,7 @@ class SAT { col.sa = flip ? polygon2 : polygon1; col.sb = flip ? polygon1 : polygon2; - if (flip) { + if (!flip) { col.normal.negate(); } diff --git a/sample/Main.hx b/sample/Main.hx index a2bbc10..55fd7e6 100644 --- a/sample/Main.hx +++ b/sample/Main.hx @@ -46,7 +46,7 @@ class Main extends BaseApp { // Set up our Sample States sample_states = [ - ListenerState, PolygonState, StackingState, MultiShapeState, ShapesState, GroupsState, StaticState, LinecastState, Linecast2State, TileMapState, TileMapState2, + OverlappingSpawnState, ListenerState, PolygonState, StackingState, MultiShapeState, ShapesState, GroupsState, StaticState, LinecastState, Linecast2State, TileMapState, TileMapState2, BezierState, VerletState ]; index = 0; diff --git a/sample/state/ListenerState.hx b/sample/state/ListenerState.hx index 1e36a3a..55b62e7 100644 --- a/sample/state/ListenerState.hx +++ b/sample/state/ListenerState.hx @@ -17,7 +17,7 @@ class ListenerState extends BaseState { var bodyA = new Body({ x: Random.range(60, world.width - 60), y: Random.range(0, world.height / 2), - rotation: Random.range(0, 360), + // rotation: Random.range(0, 360), material: material, shapes: [ { @@ -59,9 +59,10 @@ class ListenerState extends BaseState { var dbgOpts:ListenerOptions = { enter: (a, b, data) -> { trace('bodyA == listener `a`: ${bodyA == a}'); - trace('bodyA shape == data `shape a`: ${bodyA.shape == data[0].sa}'); + // the second shape is our 'bottom' shape that is making the collision + trace('bodyA owns data `shape a`: ${bodyA.shapes.contains(data[0].sa)}'); trace('bodyB == listener `b`: ${bodyB == b}'); - trace('bodyB shape == data `shape b`: ${bodyB.shape == data[0].sb}'); + trace('bodyB owns `shape b`: ${bodyB.shapes.contains(data[0].sb)}'); }, }; diff --git a/sample/state/OverlappingSpawnState.hx b/sample/state/OverlappingSpawnState.hx new file mode 100644 index 0000000..df746f1 --- /dev/null +++ b/sample/state/OverlappingSpawnState.hx @@ -0,0 +1,78 @@ +package state; + +import echo.data.Options.ListenerOptions; +import echo.Material; +import echo.Body; +import echo.World; +import util.Random; + +class OverlappingSpawnState extends BaseState { + + override public function enter(world:World) { + Main.instance.state_text.text = "Sample: Collision Listener"; + + // Create a material for all the shapes to share + var material:Material = {elasticity: 0.7}; + + var body = new Body({ + x: 200, + y: 50, + rotation: 0, + material: material, + shape: { + type: POLYGON, + radius: 16, + width: 16, + height: 16, + sides: 5, + offset_y: 0, + } + }); + world.add(body); + + body = new Body({ + x: 200, + y: 53, + rotation: 0, + material: material, + shape: { + type: POLYGON, + radius: 10, + width: 10, + height: 10, + sides: 5, + offset_y: 0, + } + }); + world.add(body); + + // Add a Physics body at the bottom of the screen for the other Physics Bodies to stack on top of + // This body has a mass of 0, so it acts as an immovable object + var floor = new Body({ + mass: STATIC, + x: world.width / 5, + y: world.height - 40, + material: material, + rotation: 5, + shape: { + type: RECT, + width: world.width, + height: 20 + } + }); + world.add(floor); + + // Create a listener for collisions between the Physics Bodies + world.listen(); + } + + override function step(world:World, dt:Float) { + // Reset any off-screen Bodies + world.for_each((member) -> { + if (offscreen(member, world)) { + member.velocity.set(0, 0); + member.set_position(Random.range(0, world.width), 0); + } + }); + } +} \ No newline at end of file diff --git a/sample/state/PolygonState.hx b/sample/state/PolygonState.hx index 96827bb..297ac86 100644 --- a/sample/state/PolygonState.hx +++ b/sample/state/PolygonState.hx @@ -21,7 +21,7 @@ class PolygonState extends BaseState { var b = new Body({ x: Random.range(60, world.width - 60), y: Random.range(0, world.height / 2), - rotation: Random.range(0, 360), + rotation: 0, material: material, shape: { type: POLYGON, diff --git a/sample/state/StaticState.hx b/sample/state/StaticState.hx index 226697f..711669b 100644 --- a/sample/state/StaticState.hx +++ b/sample/state/StaticState.hx @@ -31,8 +31,11 @@ class StaticState extends BaseState { y: (world.height * 0.5) * Math.sin(i) + world.height * 0.5, material: static_material, shape: { - type: CIRCLE, - radius: Random.range(2, 4), + // type: CIRCLE, + // radius: Random.range(2, 4), + type: RECT, + width: Random.range(2, 4), + height: Random.range(2, 4) } }); world.add(b); From 04fec0d7182360e7f82d218a38273032af2c0727 Mon Sep 17 00:00:00 2001 From: Logan Date: Tue, 16 Jan 2024 10:15:36 -0700 Subject: [PATCH 3/4] sort out circle collision flip logic --- echo/shape/Circle.hx | 2 +- echo/util/SAT.hx | 10 ++++---- sample/state/OverlappingSpawnState.hx | 34 +++++++++++---------------- sample/state/StaticState.hx | 5 ++-- 4 files changed, 23 insertions(+), 28 deletions(-) diff --git a/echo/shape/Circle.hx b/echo/shape/Circle.hx index 4f42ae2..d3ad065 100644 --- a/echo/shape/Circle.hx +++ b/echo/shape/Circle.hx @@ -87,7 +87,7 @@ class Circle extends Shape implements Poolable { return false; } - override inline function collides(s:Shape):Null return s.collide_circle(this); + override inline function collides(s:Shape):Null return s.collide_circle(this, true); override inline function collide_rect(r:Rect, flip:Bool = false):Null return r.rect_and_circle(this, !flip); diff --git a/echo/util/SAT.hx b/echo/util/SAT.hx index 58809af..0cf586a 100644 --- a/echo/util/SAT.hx +++ b/echo/util/SAT.hx @@ -344,8 +344,8 @@ class SAT { } // Vector from A to B - var nx = flip ? c.x - r.x : r.x - c.x; - var ny = flip ? c.y - r.y : r.y - c.y; + var nx = flip ? r.x - c.x : c.x - r.x; + var ny = flip ? r.y - c.y : c.y - r.y; // Closest point on A to center of B var cx = nx; var cy = ny; @@ -506,7 +506,7 @@ class SAT { test1 = min1 - max2; test2 = min2 - max1; - // Preform another test + // Preform another test // TODO: What is this test doing, exactly? if (test1 > 0 || test2 > 0) { col.put(); return null; @@ -529,7 +529,7 @@ class SAT { col.overlap = Math.abs(col.overlap); - if (!flip) { + if (flip) { col.normal.negate(); } @@ -590,6 +590,8 @@ class SAT { col.sa = flip ? polygon2 : polygon1; col.sb = flip ? polygon1 : polygon2; + // collision normal is calculated as resolution for poly2, so we need to + // negate the normal if we are not flipping the collision check. if (!flip) { col.normal.negate(); } diff --git a/sample/state/OverlappingSpawnState.hx b/sample/state/OverlappingSpawnState.hx index df746f1..7ca3ebf 100644 --- a/sample/state/OverlappingSpawnState.hx +++ b/sample/state/OverlappingSpawnState.hx @@ -20,31 +20,25 @@ class OverlappingSpawnState extends BaseState { rotation: 0, material: material, shape: { - type: POLYGON, + type: CIRCLE, radius: 16, - width: 16, - height: 16, - sides: 5, offset_y: 0, } }); world.add(body); - body = new Body({ - x: 200, - y: 53, - rotation: 0, - material: material, - shape: { - type: POLYGON, - radius: 10, - width: 10, - height: 10, - sides: 5, - offset_y: 0, - } - }); - world.add(body); + // body = new Body({ + // x: 200, + // y: 53, + // rotation: 0, + // material: material, + // shape: { + // type: CIRCLE, + // radius: 12, + // offset_y: 0, + // } + // }); + // world.add(body); // Add a Physics body at the bottom of the screen for the other Physics Bodies to stack on top of // This body has a mass of 0, so it acts as an immovable object @@ -53,7 +47,7 @@ class OverlappingSpawnState extends BaseState { x: world.width / 5, y: world.height - 40, material: material, - rotation: 5, + // rotation: 5, shape: { type: RECT, width: world.width, diff --git a/sample/state/StaticState.hx b/sample/state/StaticState.hx index 711669b..5301c68 100644 --- a/sample/state/StaticState.hx +++ b/sample/state/StaticState.hx @@ -31,9 +31,8 @@ class StaticState extends BaseState { y: (world.height * 0.5) * Math.sin(i) + world.height * 0.5, material: static_material, shape: { - // type: CIRCLE, - // radius: Random.range(2, 4), - type: RECT, + type: CIRCLE, + radius: Random.range(2, 4), width: Random.range(2, 4), height: Random.range(2, 4) } From 1927b631816208745144567df72d943fb69e30e3 Mon Sep 17 00:00:00 2001 From: Logan Date: Sat, 2 Mar 2024 13:27:28 -0700 Subject: [PATCH 4/4] cleanup formatting and test states --- echo/util/SAT.hx | 16 ++---- sample/Main.hx | 5 +- sample/state/ListenerState.hx | 82 --------------------------- sample/state/OverlappingSpawnState.hx | 72 ----------------------- sample/state/PolygonState.hx | 2 +- sample/state/StaticState.hx | 2 - 6 files changed, 10 insertions(+), 169 deletions(-) delete mode 100644 sample/state/ListenerState.hx delete mode 100644 sample/state/OverlappingSpawnState.hx diff --git a/echo/util/SAT.hx b/echo/util/SAT.hx index 0cf586a..473e88c 100644 --- a/echo/util/SAT.hx +++ b/echo/util/SAT.hx @@ -334,11 +334,8 @@ class SAT { // collisions used the transformed rect, set the collision data's shape back // to the original rect - if (!flip) { - col.sa = r; - } else { - col.sb = r; - } + if (!flip) col.sa = r; + else col.sb = r; return col; } @@ -415,11 +412,8 @@ class SAT { // collisions were done with a polygon derrived from the provided rect // so we need to set our collision data shape back to the original rectangle - if (flip) { - col.sb = r; - } else { - col.sa = r; - } + if (flip) col.sb = r; + else col.sa = r; return col; } @@ -506,7 +500,7 @@ class SAT { test1 = min1 - max2; test2 = min2 - max1; - // Preform another test // TODO: What is this test doing, exactly? + // Perform another test if (test1 > 0 || test2 > 0) { col.put(); return null; diff --git a/sample/Main.hx b/sample/Main.hx index 55fd7e6..85c55e8 100644 --- a/sample/Main.hx +++ b/sample/Main.hx @@ -1,5 +1,7 @@ package; +import state.test.ListenerState; +import state.test.OverlappingSpawnState; import hxd.Key; import echo.Echo; import echo.World; @@ -46,9 +48,10 @@ class Main extends BaseApp { // Set up our Sample States sample_states = [ - OverlappingSpawnState, ListenerState, PolygonState, StackingState, MultiShapeState, ShapesState, GroupsState, StaticState, LinecastState, Linecast2State, TileMapState, TileMapState2, + PolygonState, StackingState, MultiShapeState, ShapesState, GroupsState, StaticState, LinecastState, Linecast2State, TileMapState, TileMapState2, BezierState, VerletState ]; + index = 0; // Create a State Manager and pass it the World and the first Sample fsm = new FSM(world, Type.createInstance(sample_states[index], [])); diff --git a/sample/state/ListenerState.hx b/sample/state/ListenerState.hx deleted file mode 100644 index 55b62e7..0000000 --- a/sample/state/ListenerState.hx +++ /dev/null @@ -1,82 +0,0 @@ -package state; - -import echo.data.Options.ListenerOptions; -import echo.Material; -import echo.Body; -import echo.World; -import util.Random; - -class ListenerState extends BaseState { - - override public function enter(world:World) { - Main.instance.state_text.text = "Sample: Collision Listener"; - - // Create a material for all the shapes to share - var material:Material = {elasticity: 0.7}; - - var bodyA = new Body({ - x: Random.range(60, world.width - 60), - y: Random.range(0, world.height / 2), - // rotation: Random.range(0, 360), - material: material, - shapes: [ - { - type: POLYGON, - radius: Random.range(16, 32), - width: Random.range(16, 48), - height: Random.range(16, 48), - sides: Random.range_int(3, 8), - offset_y: -10, - }, - { - type: POLYGON, - radius: Random.range(16, 32), - width: Random.range(16, 48), - height: Random.range(16, 48), - sides: Random.range_int(3, 8), - offset_y: 10, - } - ] - }); - world.add(bodyA); - - // Add a Physics body at the bottom of the screen for the other Physics Bodies to stack on top of - // This body has a mass of 0, so it acts as an immovable object - var bodyB = new Body({ - mass: STATIC, - x: world.width / 5, - y: world.height - 40, - material: material, - rotation: 5, - shape: { - type: RECT, - width: world.width, - height: 20 - } - }); - world.add(bodyB); - - var dbgOpts:ListenerOptions = { - enter: (a, b, data) -> { - trace('bodyA == listener `a`: ${bodyA == a}'); - // the second shape is our 'bottom' shape that is making the collision - trace('bodyA owns data `shape a`: ${bodyA.shapes.contains(data[0].sa)}'); - trace('bodyB == listener `b`: ${bodyB == b}'); - trace('bodyB owns `shape b`: ${bodyB.shapes.contains(data[0].sb)}'); - }, - }; - - // Create a listener for collisions between the Physics Bodies - world.listen(bodyA, bodyB, dbgOpts); - } - - override function step(world:World, dt:Float) { - // Reset any off-screen Bodies - world.for_each((member) -> { - if (offscreen(member, world)) { - member.velocity.set(0, 0); - member.set_position(Random.range(0, world.width), 0); - } - }); - } -} \ No newline at end of file diff --git a/sample/state/OverlappingSpawnState.hx b/sample/state/OverlappingSpawnState.hx deleted file mode 100644 index 7ca3ebf..0000000 --- a/sample/state/OverlappingSpawnState.hx +++ /dev/null @@ -1,72 +0,0 @@ -package state; - -import echo.data.Options.ListenerOptions; -import echo.Material; -import echo.Body; -import echo.World; -import util.Random; - -class OverlappingSpawnState extends BaseState { - - override public function enter(world:World) { - Main.instance.state_text.text = "Sample: Collision Listener"; - - // Create a material for all the shapes to share - var material:Material = {elasticity: 0.7}; - - var body = new Body({ - x: 200, - y: 50, - rotation: 0, - material: material, - shape: { - type: CIRCLE, - radius: 16, - offset_y: 0, - } - }); - world.add(body); - - // body = new Body({ - // x: 200, - // y: 53, - // rotation: 0, - // material: material, - // shape: { - // type: CIRCLE, - // radius: 12, - // offset_y: 0, - // } - // }); - // world.add(body); - - // Add a Physics body at the bottom of the screen for the other Physics Bodies to stack on top of - // This body has a mass of 0, so it acts as an immovable object - var floor = new Body({ - mass: STATIC, - x: world.width / 5, - y: world.height - 40, - material: material, - // rotation: 5, - shape: { - type: RECT, - width: world.width, - height: 20 - } - }); - world.add(floor); - - // Create a listener for collisions between the Physics Bodies - world.listen(); - } - - override function step(world:World, dt:Float) { - // Reset any off-screen Bodies - world.for_each((member) -> { - if (offscreen(member, world)) { - member.velocity.set(0, 0); - member.set_position(Random.range(0, world.width), 0); - } - }); - } -} \ No newline at end of file diff --git a/sample/state/PolygonState.hx b/sample/state/PolygonState.hx index 297ac86..96827bb 100644 --- a/sample/state/PolygonState.hx +++ b/sample/state/PolygonState.hx @@ -21,7 +21,7 @@ class PolygonState extends BaseState { var b = new Body({ x: Random.range(60, world.width - 60), y: Random.range(0, world.height / 2), - rotation: 0, + rotation: Random.range(0, 360), material: material, shape: { type: POLYGON, diff --git a/sample/state/StaticState.hx b/sample/state/StaticState.hx index 5301c68..226697f 100644 --- a/sample/state/StaticState.hx +++ b/sample/state/StaticState.hx @@ -33,8 +33,6 @@ class StaticState extends BaseState { shape: { type: CIRCLE, radius: Random.range(2, 4), - width: Random.range(2, 4), - height: Random.range(2, 4) } }); world.add(b);