A loop Shape is a free form sequence of line segments that form a circular list. The loop may cross upon itself, but this is not recommended for smooth collision. The loop has double sided collision, so you can use inside and outside collision. Therefore, you may use any winding order.
Inheritance: Shape
 public override Shape Clone()
 {
     LoopShape loop = new LoopShape();
     loop.Count = Count;
     loop.Radius = Radius;
     loop.Vertices = (Vector2[]) Vertices.Clone();
     return loop;
 }
Example #2
0
 public override Shape Clone()
 {
     LoopShape loop = new LoopShape();
     loop.Radius = Radius;
     loop.Vertices = Vertices;
     loop._density = _density;
     loop.MassData = MassData;
     return loop;
 }
Example #3
0
        public override Shape Clone()
        {
            LoopShape loop = new LoopShape();

            loop.Count    = Count;
            loop.Radius   = Radius;
            loop.Vertices = (Vector2[])Vertices.Clone();
            return(loop);
        }
Example #4
0
        public override Shape Clone()
        {
            LoopShape loop = new LoopShape();

            loop._density = _density;
            loop._radius  = _radius;
            loop.Vertices = Vertices;
            loop.MassData = MassData;
            return(loop);
        }
Example #5
0
        //Contributed by Matthew Bettcher

        /// <summary>
        /// Convert a path into a set of edges and attaches them to the specified body.
        /// Note: use only for static edges.
        /// </summary>
        /// <param name="path">The path.</param>
        /// <param name="body">The body.</param>
        /// <param name="subdivisions">The subdivisions.</param>
        public static void ConvertPathToEdges(Path path, Body body, int subdivisions)
        {
            Vertices verts = path.GetVertices(subdivisions);

            if (path.Closed)
            {
                LoopShape loop = new LoopShape(verts);
                body.CreateFixture(loop);
            }
            else
            {
                for (int i = 1; i < verts.Count; i++)
                {
                    body.CreateFixture(new EdgeShape(verts[i], verts[i - 1]));
                }
            }
        }
Example #6
0
 public static Fixture AttachLoopShape(Vertices vertices, Body body, object userData)
 {
     LoopShape shape = new LoopShape(vertices);
     return body.CreateFixture(shape, userData);
 }
 public static Fixture CreateLoopShape(Vertices vertices, float density, Body body, Object userData)
 {
     LoopShape shape = new LoopShape(vertices, density);
     return body.CreateFixture(shape, userData);
 }
        private CharacterCollisionTest()
        {
            //Ground body
            Body ground = BodyFactory.CreateEdge(World, new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f));

            // Collinear edges
            EdgeShape shape = new EdgeShape(new Vector2(-8.0f, 1.0f), new Vector2(-6.0f, 1.0f));
            ground.CreateFixture(shape);
            shape = new EdgeShape(new Vector2(-6.0f, 1.0f), new Vector2(-4.0f, 1.0f));
            ground.CreateFixture(shape);
            shape = new EdgeShape(new Vector2(-4.0f, 1.0f), new Vector2(-2.0f, 1.0f));
            ground.CreateFixture(shape);

            // Square tiles
            PolygonShape tile = new PolygonShape(1);
            tile.SetAsBox(1.0f, 1.0f, new Vector2(4.0f, 3.0f), 0.0f);
            ground.CreateFixture(tile);
            tile.SetAsBox(1.0f, 1.0f, new Vector2(6.0f, 3.0f), 0.0f);
            ground.CreateFixture(tile);
            tile.SetAsBox(1.0f, 1.0f, new Vector2(8.0f, 3.0f), 0.0f);
            ground.CreateFixture(tile);

            // Square made from an edge loop.
            Vertices vertices = new Vertices(4);
            vertices.Add(new Vector2(-1.0f, 3.0f));
            vertices.Add(new Vector2(1.0f, 3.0f));
            vertices.Add(new Vector2(1.0f, 5.0f));
            vertices.Add(new Vector2(-1.0f, 5.0f));
            LoopShape loopShape = new LoopShape(vertices);
            ground.CreateFixture(loopShape);

            // Edge loop.
            vertices = new Vertices(10);
            vertices.Add(new Vector2(0.0f, 0.0f));
            vertices.Add(new Vector2(6.0f, 0.0f));
            vertices.Add(new Vector2(6.0f, 2.0f));
            vertices.Add(new Vector2(4.0f, 1.0f));
            vertices.Add(new Vector2(2.0f, 2.0f));
            vertices.Add(new Vector2(-2.0f, 2.0f));
            vertices.Add(new Vector2(-4.0f, 3.0f));
            vertices.Add(new Vector2(-6.0f, 2.0f));
            vertices.Add(new Vector2(-6.0f, 0.0f));

            BodyFactory.CreateLoopShape(World, vertices, new Vector2(-10, 4));

            // Square character
            Body squareCharacter = BodyFactory.CreateRectangle(World, 1, 1, 20);
            squareCharacter.Position = new Vector2(-3.0f, 5.0f);
            squareCharacter.BodyType = BodyType.Dynamic;
            squareCharacter.FixedRotation = true;
            squareCharacter.SleepingAllowed = false;

            squareCharacter.OnCollision += CharacterOnCollision;
            squareCharacter.OnSeparation += CharacterOnSeparation;

            // Square character 2
            Body squareCharacter2 = BodyFactory.CreateRectangle(World, 0.5f, 0.5f, 20);
            squareCharacter2.Position = new Vector2(-5.0f, 5.0f);
            squareCharacter2.BodyType = BodyType.Dynamic;
            squareCharacter2.FixedRotation = true;
            squareCharacter2.SleepingAllowed = false;

            // Hexagon character
            float angle = 0.0f;
            const float delta = Settings.Pi / 3.0f;
            vertices = new Vertices(6);

            for (int i = 0; i < 6; ++i)
            {
                vertices.Add(new Vector2(0.5f * (float)Math.Cos(angle), 0.5f * (float)Math.Sin(angle)));
                angle += delta;
            }

            Body hexCharacter = BodyFactory.CreatePolygon(World, vertices, 20);
            hexCharacter.Position = new Vector2(-5.0f, 8.0f);
            hexCharacter.BodyType = BodyType.Dynamic;
            hexCharacter.FixedRotation = true;
            hexCharacter.SleepingAllowed = false;

            // Circle character
            Body circleCharacter = BodyFactory.CreateCircle(World, 0.5f, 20);
            circleCharacter.Position = new Vector2(3.0f, 5.0f);
            circleCharacter.BodyType = BodyType.Dynamic;
            circleCharacter.FixedRotation = true;
            circleCharacter.SleepingAllowed = false;
        }
Example #9
0
        private PinballTest()
        {
            // Ground body
            Body ground;
            {
                ground = BodyFactory.CreateBody(World);

                Vertices vertices = new Vertices(5);
                vertices.Add(new Vector2(0.0f, -2.0f));
                vertices.Add(new Vector2(8.0f, 6.0f));
                vertices.Add(new Vector2(8.0f, 20.0f));
                vertices.Add(new Vector2(-8.0f, 20.0f));
                vertices.Add(new Vector2(-8.0f, 6.0f));

                LoopShape loop = new LoopShape(vertices);
                ground.CreateFixture(loop);
            }

            // Flippers
            {
                Vector2 p1 = new Vector2(-2.0f, 0f);
                Vector2 p2 = new Vector2(2.0f, 0f);

                Body leftFlipper = BodyFactory.CreateBody(World, p1);
                leftFlipper.BodyType = BodyType.Dynamic;
                Body rightFlipper = BodyFactory.CreateBody(World, p2);
                rightFlipper.BodyType = BodyType.Dynamic;

                PolygonShape box = new PolygonShape(1);
                box.SetAsBox(1.75f, 0.1f);

                leftFlipper.CreateFixture(box);
                rightFlipper.CreateFixture(box);

                _leftJoint = new RevoluteJoint(ground, leftFlipper, p1, Vector2.Zero);
                _leftJoint.MaxMotorTorque = 1000.0f;
                _leftJoint.LimitEnabled = true;
                _leftJoint.MotorEnabled = true;
                _leftJoint.MotorSpeed = 0.0f;
                _leftJoint.LowerLimit = -30.0f * Settings.Pi / 180.0f;
                _leftJoint.UpperLimit = 5.0f * Settings.Pi / 180.0f;
                World.AddJoint(_leftJoint);

                _rightJoint = new RevoluteJoint(ground, rightFlipper, p2, Vector2.Zero);
                _rightJoint.MaxMotorTorque = 1000.0f;
                _rightJoint.LimitEnabled = true;
                _rightJoint.MotorEnabled = true;
                _rightJoint.MotorSpeed = 0.0f;
                _rightJoint.LowerLimit = -5.0f * Settings.Pi / 180.0f;
                _rightJoint.UpperLimit = 30.0f * Settings.Pi / 180.0f;
                World.AddJoint(_rightJoint);
            }

            // Circle character
            {
                _ball = BodyFactory.CreateBody(World, new Vector2(1.0f, 15.0f));
                _ball.BodyType = BodyType.Dynamic;
                _ball.IsBullet = true;
                _ball.CreateFixture(new CircleShape(0.2f, 1.0f));
            }
        }
Example #10
0
        private void ParseFixture(JObject jsonFixture, Body body)
        {
            Shape shape = null;

            var circles = (JObject)jsonFixture["circle"];
            var polygons = (JObject)jsonFixture["polygon"];
            var chains = (JObject)jsonFixture["chain"];

            if (circles != null)
            {
                var center = Vector2.Zero;
                float radius = 0;
                foreach (JProperty circleProperty in circles.Children())
                {
                    switch (circleProperty.Name)
                    {
                        case "center":
                            center = ParseVector2(circleProperty);
                            break;
                        case "radius":
                            radius = HexToFloat(circleProperty.Value.ToString());
                            break;
                    }
                }
                shape = new CircleShape(radius, 1);
                var circleShape = (CircleShape) shape;
                circleShape.Position = center;
            }
            else if (polygons != null)
            {
                Vertices vertices = null;
                foreach (var polygonProperty in polygons.Children().Cast<JProperty>().Where(polygonProperty => polygonProperty.Name == "vertices"))
                    vertices = new Vertices(ParseVector2Array(polygonProperty));
                if (vertices != null)
                    shape = new PolygonShape(vertices, 1);
            }
            else if (chains != null)
            {
                //shape = new 
                Vertices vertices = null;
                bool isLoopShape = false;
                bool hasNextVertex;
                bool hasPrevVertex;
                Vector2 nextVertex = Vector2.Zero;
                Vector2 prevVertex = Vector2.Zero;

                foreach (JProperty chainProperty in chains.Children())
                {
                    switch (chainProperty.Name)
                    {
                        case "vertices":
                            vertices = new Vertices(ParseVector2Array(chainProperty));
                            break;
                        case "hasNextVertex":
                            isLoopShape = true;
                            hasNextVertex = (bool) chainProperty.Value;
                            break;
                        case "hasPrevVertex":
                            hasPrevVertex = (bool) chainProperty.Value;
                            break;
                        case "nextVertex":
                            nextVertex = ParseVector2(chainProperty);
                            break;
                        case "prevVertex":
                            prevVertex = ParseVector2(chainProperty);
                            break;
                        default:
                            System.Diagnostics.Debug.WriteLine(chainProperty.Name + " not supported!");
                            break;
                    }
                }

                if (isLoopShape)
                {
                    var lastvertexIndex = vertices.Count - 1;
                    if (vertices[0] == vertices[lastvertexIndex])
                        vertices.RemoveAt(lastvertexIndex);
                    shape = new LoopShape(vertices);
                    //var loopShape = (LoopShape) shape;
                }
                else
                {
                    throw new NotImplementedException();
                    //shape = new EdgeShape(prevVertex, nextVertex);
                    //var edgeShape = (EdgeShape) shape;
                }
            }

            var fixture = body.CreateFixture(shape);

            foreach (JProperty fixtureProperty in jsonFixture.Children())
            {
                //Fixture properties
                switch (fixtureProperty.Name)
                {
                    case "name":
                        var value = fixtureProperty.Value.ToString();
                        _namedFixtures.Add(fixture, value);
                        break;
                    case "density":
                        shape.Density = HexToFloat(fixtureProperty.Value.ToString());
                        break;
                    case "friction":
                        fixture.Friction = HexToFloat(fixtureProperty.Value.ToString());
                        break;
                    case "restitution":
                        fixture.Restitution = HexToFloat(fixtureProperty.Value.ToString());
                        break;
                    case "sensor":
                        fixture.IsSensor = (bool) fixtureProperty.Value;
                        break;
                    case "circle":
                    case "polygon":
                    case "chain":
                    //case "filter-categoryBits":
                    //case "filter-maskBits":
                    //case "filter-groupIndex":
                        break; //ignore
                    default:
                        System.Diagnostics.Debug.WriteLine(fixtureProperty.Name + " not supported!");
                        break;
                }
            }
        }
Example #11
0
 private static JObject SerializeLoopShape(LoopShape shape)
 {
     var jsonLoopShape = new JObject();
     jsonLoopShape.Add(new JProperty("vertices", ToJsonObject(shape.Vertices)));
     jsonLoopShape.Add(new JProperty("hasNextVertex", true));
     jsonLoopShape.Add(new JProperty("hasPrevVertex", true));
     jsonLoopShape.Add(new JProperty("nextVertex", ToJsonObject(shape.Vertices[1])));
     jsonLoopShape.Add(new JProperty("prevVertex", ToJsonObject(shape.Vertices[shape.Vertices.Count - 2])));
     return jsonLoopShape;
 }