From 29f2a0f2270955b34502e471b82c53b81e291736 Mon Sep 17 00:00:00 2001 From: Viktor Lidholt Date: Wed, 14 Oct 2015 12:58:47 -0700 Subject: [PATCH] Adds missing sprite physics shapes --- .../lib/src/physics_shape.dart | 76 ++++++++++++++++++- 1 file changed, 75 insertions(+), 1 deletion(-) diff --git a/packages/flutter_sprites/lib/src/physics_shape.dart b/packages/flutter_sprites/lib/src/physics_shape.dart index 4a98d39eec..2669aeb2be 100644 --- a/packages/flutter_sprites/lib/src/physics_shape.dart +++ b/packages/flutter_sprites/lib/src/physics_shape.dart @@ -32,7 +32,6 @@ class PhysicsShapeCircle extends PhysicsShape { } class PhysicsShapePolygon extends PhysicsShape { - PhysicsShapePolygon(this.points); final List points; @@ -53,6 +52,81 @@ class PhysicsShapePolygon extends PhysicsShape { } } +class PhysicsShapeBox extends PhysicsShape { + PhysicsShapeBox( + this.width, + this.height, [ + this.center = Point.origin, + this.rotation = 0.0 + ]); + + final double width; + final double height; + final Point center; + final double rotation; + + box2d.Shape _createB2Shape(PhysicsNode node) { + box2d.PolygonShape shape = new box2d.PolygonShape(); + shape.setAsBox( + width / node.b2WorldToNodeConversionFactor, + height / node.b2WorldToNodeConversionFactor, + new Vector2( + center.x / node.b2WorldToNodeConversionFactor, + center.y / node.b2WorldToNodeConversionFactor + ), + radians(rotation) + ); + return shape; + } +} + +class PhysicsShapeChain extends PhysicsShape { + PhysicsShapeChain(this.points, [this.loop=false]); + + final List points; + final bool loop; + + box2d.Shape _createB2Shape(PhysicsNode node) { + List vectors = []; + for (Point point in points) { + Vector2 vec = new Vector2( + point.x / node.b2WorldToNodeConversionFactor, + point.y / node.b2WorldToNodeConversionFactor + ); + vectors.add(vec); + } + + box2d.ChainShape shape = new box2d.ChainShape(); + if (loop) + shape.createLoop(vectors, vectors.length); + else + shape.createChain(vectors, vectors.length); + return shape; + } +} + +class PhysicsShapeEdge extends PhysicsShape { + PhysicsShapeEdge(this.pointA, this.pointB); + + final Point pointA; + final Point pointB; + + box2d.Shape _createB2Shape(PhysicsNode node) { + box2d.EdgeShape shape = new box2d.EdgeShape(); + shape.set( + new Vector2( + pointA.x / node.b2WorldToNodeConversionFactor, + pointA.y / node.b2WorldToNodeConversionFactor + ), + new Vector2( + pointB.x / node.b2WorldToNodeConversionFactor, + pointB.y / node.b2WorldToNodeConversionFactor + ) + ); + return shape; + } +} + class PhysicsShapeGroup extends PhysicsShape { PhysicsShapeGroup(this.shapes);