public void ShapesProperty() { var space = new Space(); var body = new Body(1, 1.66); var body2 = new Body(1, 1.66); var shape = new Box(body, 100, 100, 0); var shape2 = new Box(body2, 100, 100, 0); body.Position = new Vect(10, 10); body.Position = new Vect(20, 20); Assert.AreEqual(0, space.Shapes.Count, "#1"); space.AddBody(body); space.AddShape(shape); Shape[] shapes = space.Shapes.ToArray(); Assert.AreEqual(1, shapes.Length, "#2.1"); Assert.AreSame(shape, shapes[0], "#2.2"); space.AddBody(body2); space.AddShape(shape2); shapes = space.Shapes.ToArray(); Assert.AreEqual(2, shapes.Length, "#3.1"); Assert.AreSame(shape, shapes[0], "#3.2"); Assert.AreSame(shape2, shapes[1], "#3.3"); body.Dispose(); space.Dispose(); }
public void SegmentTest() { var space = new Space(); var body = new Body(10, 224.037008503093); var shape = new Segment(body, new Vect(1, 2), new Vect(3, 5), 2); body.Position = new Vect(3, 2); shape.Density = 10; space.AddBody(body); space.AddShape(shape); Assert.AreEqual(new Vect(1, 2), shape.A, "#1"); Assert.AreEqual(new Vect(3, 5), shape.B, "#2"); Assert.AreEqual(2, shape.Radius, "#3"); Assert.AreEqual(0.832050294337844, shape.Normal.X, 0.00001, "#3"); Assert.AreEqual(-0.554700196225229, shape.Normal.Y, 0.00001, "#4"); double moment = shape.MomentForMass(10); double area = shape.Area; Assert.AreEqual(224.037008503093, moment, 0.0000001, "#5"); Assert.AreEqual(26.9885757162151, area, 0.0000001, "#6"); space.Dispose(); }
public void BoundBoxQueryTest() { var space = new Space(); var body = new Body(1, 1.66); var shape = new Box(body, 5, 5, 0); var pos = new Vect(3, 3); body.Position = pos; var bb = new BoundingBox(); bb.Left = -20; bb.Top = -20; bb.Right = +20; bb.Bottom = +20; Shape[] shapes = space.BoundBoxQuery(bb, ShapeFilter.All).ToArray(); Assert.AreEqual(0, shapes.Length, "#1"); space.AddBody(body); space.AddShape(shape); space.ReindexShape(shape); shapes = space.BoundBoxQuery(bb, ShapeFilter.All).ToArray(); Assert.AreEqual(1, shapes.Length, "#2"); Assert.AreSame(shape, shapes[0], "#3"); shape.Dispose(); body.Dispose(); space.Dispose(); }
public void UnsafePolygon() { var space = new Space(); var body = new Body(10, 16.666); Vect[] vertexs = { new Vect(-1, 0), new Vect(0, -1), new Vect(1, 0), new Vect(0, 1) }; var polygon = new Polygon(body, vertexs, 2.0); space.AddBody(body); space.AddShape(polygon); Vect[] vertexs2 = { new Vect(-2, 0), new Vect(0, -2), new Vect(2, 0), new Vect(0, 2) }; polygon.SetVertexes(vertexs2); for (int i = 0; i < vertexs.Length; i++) { Assert.AreEqual(vertexs2[i], polygon.GetVertex(i), "#2.1." + i); } polygon.SetRadius(1.33); Assert.AreEqual(1.33, polygon.Radius, "#3"); space.Dispose(); }
public void SegmentQueryTest() { var space = new Space(); var body = new Body(1, 1.66); var shape = new Box(body, 100, 100, 0); body.Position = new Vect(0, 0); var end = new Vect(0, 1); SegmentQueryInfo[] infos = space.SegmentQuery(body.Position, end, 2.0, ShapeFilter.All).ToArray(); Assert.AreEqual(0, infos.Length, "#1"); space.AddBody(body); space.AddShape(shape); infos = space.SegmentQuery(body.Position, end, 2.0, ShapeFilter.All).ToArray(); SegmentQueryInfo first = space.SegmentQueryFirst(body.Position, end, 2.0, ShapeFilter.All); Assert.AreEqual(1, infos.Length, "#2"); Assert.AreSame(shape, infos[0].Shape, "#3"); Assert.AreEqual(infos[0], first, "#4"); shape.Dispose(); body.Dispose(); space.Dispose(); }
public void PolygonTest() { var space = new Space(); var body = new Body(10, 3.33333333333333); var body2 = new Body(10, 3.33333333333333); Vect[] vertexs = { new Vect(-1, 0), new Vect(0, -1), new Vect(1, 0), new Vect(0, 1) }; var polygon = new Polygon(body, vertexs, 2.0); var polygon2 = new Polygon(body2, vertexs, Transform.Identity, 1.0); space.AddBody(body); space.AddShape(polygon); space.AddBody(body2); space.AddShape(polygon2); double area = polygon.Area; double area2 = polygon.Area; Assert.AreEqual(25.8800791133439, area, 0.000001, "#0.1"); Assert.AreEqual(25.8800791133439, area2, 0.000001, "#0.1"); Assert.IsTrue(vertexs.SequenceEqual(polygon.Vertices), "#0"); Assert.AreEqual(vertexs.Length, polygon.Count, "#1"); Assert.AreEqual(vertexs.Length, polygon2.Count, "#1.1"); for (int i = 0; i < vertexs.Length; i++) { Assert.AreEqual(vertexs[i], polygon.GetVertex(i), "#2.1." + i); Assert.AreEqual(vertexs[i], polygon2.GetVertex(i), "#2.2." + i); } Assert.AreEqual(2.0, polygon.Radius, "#3.1"); Assert.AreEqual(1.0, polygon2.Radius, "#3.1"); Assert.AreEqual(Vect.Zero, polygon.Centroid, "#3"); double moment = Polygon.MomentForPolygon(body.Mass, polygon.Vertices, Vect.Zero, polygon.Radius); Assert.AreEqual(3.33333333333333, moment, 0.0000001, "#4"); space.Dispose(); }
public void MiscPropertyTest() { var space = new Space(); var body = new Body(10, 16.666); var shape = new Box(body, 2, 2, 0); body.Position = new Vect(3, 2); shape.Density = 10; space.AddBody(body); space.AddShape(shape); Assert.AreEqual(10, shape.Density, "#1"); double moment = shape.Moment; Assert.AreEqual(26.666666666, moment, 0.0000001, "#2"); double area = shape.Area; Assert.AreEqual(4, area, "#3"); Vect center = shape.CenterOfGravity; Assert.AreEqual(new Vect(0, 0), center, "#4"); BoundingBox boundBox = shape.BoundingBox; Assert.AreEqual(new BoundingBox(2, 1, 4, 3), boundBox, "#5"); bool sensor = shape.Sensor; Assert.AreEqual(false, sensor, "#6"); shape.Sensor = true; Assert.AreEqual(true, shape.Sensor, "#6.1"); shape.Elasticity = 0.7; Assert.AreEqual(0.7, shape.Elasticity, "#7"); shape.Friction = 0.4; Assert.AreEqual(0.4, shape.Friction, "#8"); shape.SurfaceVelocity = new Vect(-1, 2); Assert.AreEqual(new Vect(-1, 2), shape.SurfaceVelocity, "#9"); shape.CollisionType = 13; Assert.AreEqual(13, shape.CollisionType, "#10"); ShapeFilter filter = shape.Filter; shape.Filter = ShapeFilter.All; Assert.AreEqual(ShapeFilter.All, shape.Filter, "#11"); space.Dispose(); }
public void SetUp() { space = new Space(); bodyA = new Body(1, 1.666); shapeA = new Box(bodyA, 2, 2, 0); bodyB = new Body(2, 3.222); shape2 = new Box(bodyB, 3, 3, 0); bodyA.Position = new Vect(2, 1); bodyB.Position = new Vect(3, 2); space.AddBody(bodyA); space.AddShape(shapeA); space.AddBody(bodyB); space.AddShape(shape2); }
public void CollideTest() { var space = new Space(); var body = new Body(1, 1.66); var shape = new Box(body, 2, 2, 0); var body2 = new Body(1, 1.66); var shape2 = new Box(body2, 3, 3, 0); body.Position = new Vect(2, 1); body2.Position = new Vect(3, 2); space.AddBody(body); space.AddShape(shape); space.AddBody(body2); space.AddShape(shape2); ContactPointSet pointSet = shape.Collide(shape2); Assert.AreEqual(2, pointSet.Count, "#1"); Assert.AreEqual(new Vect(0, 1), pointSet.Normal, "#2"); Assert.AreEqual(2, pointSet.Points.Count, "#3.0"); Assert.AreEqual(new Vect(3, 2), pointSet.Points[0].PointA, "#3.1"); Assert.AreEqual(new Vect(3, 0.5), pointSet.Points[0].PointB, "#3.2"); Assert.AreEqual(-1.5, pointSet.Points[0].Distance, "#3.3"); Assert.AreEqual(new Vect(1.5, 2), pointSet.Points[1].PointA, "#4.1"); Assert.AreEqual(new Vect(1.5, 0.5), pointSet.Points[1].PointB, "#4.2"); Assert.AreEqual(-1.5, pointSet.Points[1].Distance, "#4.3"); space.Dispose(); }
public void SpaceTest() { var space = new Space(); var body = new Body(1, 1.66); var shape = new Box(body, 2, 2, 0); Assert.IsNull(shape.Space, "#1"); space.AddBody(body); space.AddShape(shape); Assert.AreSame(space, shape.Space, "#2"); space.Dispose(); }
void AddBox(Space space) { const double size = 10.0; const double mass = 1.0; Vect[] verts = { new Vect(-size, -size), new Vect(-size, size), new Vect(size, size), new Vect(size, -size) }; double radius = verts[0].Length(); Vect position = RandPosition(radius); var body = new Body(mass, Polygon.MomentForPolygon(mass, verts, Vect.Zero, 0.0)); space.AddBody(body); body.VelocityUpdateFunction = planetGravityFunctionCallback; body.Position = position; // Set the box's velocity to put it into a circular orbit from its // starting position. double r = position.Length(); double v = Math.Sqrt(gravityStrength / r) / r; body.Velocity = position.Perpendicurlar * v; // Set the box's angular velocity to match its orbital period and // align its initial angle with its position. body.AngularVelocity = v; body.Angle = Math.Atan2(position.Y, position.X); var shape = new Polygon(body, verts, Transform.Identity, 0.0); space.AddShape(shape); shape.Elasticity = 0.0; shape.Friction = 0.7; }
public void UnsafeSegment() { var space = new Space(); var body = new Body(10, 16.666); var shape = new Segment(body, new Vect(1, 2), new Vect(3, 5), 2); body.Position = new Vect(3, 2); shape.Density = 10; space.AddBody(body); space.AddShape(shape); shape.SetEndpoints(new Vect(2, 4), new Vect(6, 2)); shape.SetRadius(3); Assert.AreEqual(new Vect(2, 4), shape.A, "#1"); Assert.AreEqual(new Vect(6, 2), shape.B, "#2"); Assert.AreEqual(3, shape.Radius, "#3"); }
[Ignore("TODO: ShapeQuery test better")] // TODO: ShapeQuery test better public void ShapeQueryTest() { var space = new Space(); var body = new Body(1, 1.66); var shape = new Box(body, 1, 1, 0); body.Position = new Vect(0, 0); ContactPointSet[] pointSet = space.ShapeQuery(shape).ToArray(); Assert.AreEqual(0, pointSet.Length, "#1"); space.AddBody(body); space.AddShape(shape); pointSet = space.ShapeQuery(shape).ToArray(); Assert.AreEqual(1, pointSet.Length, "#2"); shape.Dispose(); body.Dispose(); space.Dispose(); }
public void DebugDrawTest() { var space = new Space(); var body = new Body(1, 1.66); var shape = new Box(body, 100, 100, 0); string expected_calls = "DrawPolygon\nvectors[0] = (50,-50)\nvectors[1] = (50,50)\nvectors[2] = (-50,50)\nvectors[3] = (-50,-50)\nradius = 0\noutlineColor = (1,1,1,1)\nfillColor = (0,0,1,1)\n"; body.Position = new Vect(0, 0); space.AddBody(body); space.AddShape(shape); var debugDraw = new FakeDebugDraw(); space.DebugDraw(debugDraw); Assert.AreEqual(expected_calls, debugDraw.TracedCalls, "#1"); shape.Dispose(); body.Dispose(); space.Dispose(); }
public void PointQueryTest() { var space = new Space(); var body = new Body(1, 1.66); var shape = new Box(body, 100, 100, 0); body.Position = new Vect(0, 0); PointQueryInfo[] infos = space.PointQuery(body.Position, 10.0, ShapeFilter.All).ToArray(); Assert.AreEqual(0, infos.Length, "#1"); space.AddBody(body); space.AddShape(shape); infos = space.PointQuery(body.Position, 10.0, ShapeFilter.All).ToArray(); Assert.AreEqual(1, infos.Length, "#2"); Assert.AreSame(shape, infos[0].Shape, "#3"); shape.Dispose(); body.Dispose(); space.Dispose(); }
public void UnsafeCircle() { var space = new Space(); var body = new Body(10, 16.666); var shape = new Circle(body, 2, new Vect(3, 5)); body.Position = new Vect(3, 2); shape.Density = 10; space.AddBody(body); space.AddShape(shape); Assert.AreEqual(new Vect(3, 5), shape.Offset, "#1"); Assert.AreEqual(2, shape.Radius, "#2"); shape.SetOffset(new Vect(10, 13)); shape.SetRadius(4); Assert.AreEqual(new Vect(10, 13), shape.Offset, "#3"); Assert.AreEqual(4, shape.Radius, "#4"); space.Dispose(); }
public void CircleOffset() { var space = new Space(); var body = new Body(10, 360); var shape = new Circle(body, 2, new Vect(3, 5)); body.Position = new Vect(3, 2); shape.Density = 10; space.AddBody(body); space.AddShape(shape); Assert.AreEqual(new Vect(3, 5), shape.Offset, "#1"); Assert.AreEqual(2, shape.Radius, "#2"); double moment = shape.MomentForMass(10); double area = shape.Area; Assert.AreEqual(360, moment, "#3"); Assert.AreEqual(12.5663706143592, area, 0.00001, "#4"); space.Dispose(); }
public void TestCollisionHandler() { var space = new Space(); space.CollisionBias = 1.0; float radius = 5.0f; var body1 = new Body(1, 1) { Position = new Vect(0 * radius * 1.5, 0) }; space.AddBody(body1); space.AddShape(new Circle(body1, radius)); var body2 = new Body(1, 1) { Position = new Vect(0 * radius * 1.5, 0) }; space.AddBody(body2); var shape2 = new Circle(body2, radius); space.AddShape(shape2); CollisionHandler <StringBuilder> handler = space.GetOrCreateCollisionHandler <StringBuilder>(0, 0); CollisionHandler <StringBuilder> handler2 = space.GetOrCreateCollisionHandler <StringBuilder>(0, 0); Assert.AreSame(handler, handler2, "#0"); handler.Data = new StringBuilder(); handler.Begin = (a, s, builder) => { builder.Append("Begin-"); return(true); }; handler.PreSolve = (a, s, builder) => { builder.Append("PreSolve-"); return(true); }; handler.PostSolve = (a, s, builder) => builder.Append("PostSolve-"); handler.Separate = (a, s, builder) => builder.Append("Separete-"); space.Step(0.1); Assert.AreEqual("Begin-PreSolve-PostSolve-", handler.Data.ToString(), "#1"); space.Step(0.1); Assert.AreEqual("Begin-PreSolve-PostSolve-PreSolve-PostSolve-", handler.Data.ToString(), "#2"); shape2.Dispose(); body1.Dispose(); body2.Dispose(); space.Dispose(); }
public void TestArbiterProperties() { Space space; using (space = new Space()) { space.CollisionBias = 1.0; float radius = 5.0f; var body1 = new Body(1, 1.666) { Position = new Vect(0 * radius * 1.5, 0) }; space.AddBody(body1); Shape shape = new Circle(body1, radius); space.AddShape(shape); var body2 = new Body(1, 1.666) { Position = new Vect(0 * radius * 1.5, 0) }; space.AddBody(body2); var shape2 = new Circle(body2, radius); space.AddShape(shape2); CollisionHandler <object> handler = space.GetOrCreateCollisionHandler(0, 0); handler.Data = "object data"; Body bodyA = null; Body bodyB = null; Shape shapeA = null; Shape shapeB = null; double restituition = -1; double fricction = -1; Vect surfaceVelocity = new Vect(-1, -1); Vect totalImpulse = new Vect(-1, -1); double ke = -1; ContactPointSet pointSet = null; bool first = false; bool removal = true; int count = -1; Vect normal = new Vect(-1, -1); Vect pointA = new Vect(-1, -1); Vect pointB = new Vect(-1, -1); double depth = -1; var arb = default(Arbiter); handler.Begin = (arbiter, s, obj) => { Assert.IsNull(arbiter.Data, "arbiter.Data"); arbiter.Data = "another data"; arbiter.GetBodies(out bodyA, out bodyB); arbiter.GetShapes(out shapeA, out shapeB); restituition = arbiter.Restitution; fricction = arbiter.Friction; surfaceVelocity = arbiter.SurfaceVelocity; totalImpulse = arbiter.TotalImpulse; ke = arbiter.TotalKE; pointSet = arbiter.ContactPointSet; first = arbiter.IsFirstContact; removal = arbiter.IsRemoval; count = arbiter.Count; normal = arbiter.Normal; pointA = arbiter.GetPointA(0); pointB = arbiter.GetPointB(0); depth = arbiter.GetDepth(0); arb = arbiter; return(true); }; space.Step(0.1); Assert.AreSame(body2, bodyA, "#1"); Assert.AreSame(body1, bodyB, "#1.1"); var arbiters = bodyA.Arbiters; Assert.AreEqual(1, arbiters.Count, "#0"); Assert.AreEqual(arb, arbiters[0], "#0.1"); Assert.AreEqual("another data", arbiters[0].Data, "#0.2"); Assert.AreSame(shape2, shapeA, "#2"); Assert.AreSame(shape, shapeB, "#2.1"); Assert.AreEqual(0, restituition, 0.00001, "#3"); Assert.AreEqual(0, fricction, 0.00001, "#4"); Assert.AreEqual(Vect.Zero, surfaceVelocity, "#5"); Assert.AreEqual(Vect.Zero, totalImpulse, "#6"); Assert.AreEqual(double.NaN, ke, "#7"); Assert.AreEqual(1, pointSet.Count, "8.1"); Assert.AreEqual(new Vect(1, 0), pointSet.Normal, "8.2"); Assert.AreEqual(-10, pointSet.Points[0].Distance, 0.00001, "8.3"); Assert.AreEqual(new Vect(5, 0), pointSet.Points[0].PointA, "8.4"); Assert.AreEqual(new Vect(-5, 0), pointSet.Points[0].PointB, "8.5"); Assert.AreEqual(0, pointSet.Points[1].Distance, 0.00001, "8.6"); Assert.AreEqual(new Vect(0, 0), pointSet.Points[1].PointA, "8.7"); Assert.AreEqual(new Vect(0, 0), pointSet.Points[1].PointB, "8.8"); Assert.IsTrue(first, "#9"); Assert.IsFalse(removal, "#10"); Assert.AreEqual(1, count, "#11"); Assert.AreEqual(new Vect(1, 0), normal, "#12"); Assert.AreEqual(new Vect(5, 0), pointA, "#13"); Assert.AreEqual(new Vect(-5, 0), pointB, "#14"); Assert.AreEqual(-10, depth, 0.00001, "#15"); } }
public Space Initialize() { space = new Space(); space.Iterations = 30; space.Gravity = new Vect(0, -100); space.SleepTimeThreshold = 0.5f; space.CollisionSlop = 0.5f; Body body; Body staticBody = space.StaticBody; Shape shape = new Segment(staticBody, new Vect(-320, -240), new Vect(-320, 240), 0.0f); space.AddShape(shape); obj.Add(shape); shape.Elasticity = 1.0f; shape.Friction = 1.0f; shape.Filter = NotGrabbableFilter; shape = new Segment(staticBody, new Vect(320, -240), new Vect(320, 240), 0.0f); space.AddShape(shape); obj.Add(shape); shape.Elasticity = 1.0f; shape.Friction = 1.0f; shape.Filter = NotGrabbableFilter; shape = new Segment(staticBody, new Vect(-320, -240), new Vect(320, -240), 0.0f); space.AddShape(shape); obj.Add(shape); shape.Elasticity = 1.0f; shape.Friction = 1.0f; shape.Filter = NotGrabbableFilter; // Add lots of boxes. for (int i = 0; i < 14; i++) { for (int j = 0; j <= i; j++) { body = new Body(1.0f, Box.MomentForBox(1.0f, 30.0f, 30.0f)); space.AddBody(body); obj.Add(body); body.Position = new Vect(j * 32 - i * 16, 300 - i * 32); shape = new Box(body, 30.0f, 30.0f, 0.5f); space.AddShape(shape); obj.Add(shape); shape.Elasticity = 0.0f; shape.Friction = 0.8f; } } // Add a ball to make things more interesting double radius = 15.0f; body = new Body(10.0f, Circle.MomentForCircle(10.0f, 0.0f, radius, Vect.Zero)); space.AddBody(body); obj.Add(body); body.Position = new Vect(0, -240 + radius + 5); shape = new Circle(body, radius); space.AddShape(shape); obj.Add(shape); shape.Elasticity = 0.0f; shape.Friction = 0.9f; return(space); }