Beispiel #1
0
        protected override ICollider CreateCollider(IBody body)
        {
            var pointsInMeters = new Vector2[Points.Length];

            for (int i = 0; i < Points.Length; i++)
            {
                pointsInMeters[i] = Physics.PixelsToMeters(Points[i]);
            }

            return(body.AddPolygonCollider(Physics.PixelsToMeters(Position), pointsInMeters));
        }
Beispiel #2
0
        private void UpdateColliders()
        {
            if (Scene == null)
            {
                return;
            }

            _body = Scene.World.CreateStaticBody();

            var offset = -((TileMap.Size * TileMap.TileSize).ToVector2() * 0.5f);

            for (int y = 0; y < TileMap.Size.y; y++)
            {
                for (int x = 0; x < TileMap.Size.x; x++)
                {
                    var tile = Layer.Tiles[x + y * TileMap.Size.x];
                    if (null == tile)
                    {
                        continue;
                    }

                    if (tile.Polygons != null && tile.Polygons.Length > 0)
                    {
                        foreach (var polygon in tile.Polygons)
                        {
                            var collider = _body.AddPolygonCollider(
                                Physics.PixelsToMeters(offset + new Vector2(x * TileMap.TileSize.x, y * TileMap.TileSize.y)), polygon.Points);
                            if (polygon.Properties.TryGetValue("Layer", out var polygonLayer))
                            {
                                collider.CollisionMask    = (uint)(1 << Physics.NameToLayer(polygonLayer));
                                collider.CollidesWithMask = Physics.GetLayerCollisionMask(Physics.NameToLayer(polygonLayer));
                            }
                        }
                    }

                    if (tile.Properties.TryGetValue("Layer", out var layer))
                    {
                        var collider = _body.AddBoxCollider(
                            Physics.PixelsToMeters(offset + new Vector2((x + 0.5f) * TileMap.TileSize.x, (y + 0.5f) * TileMap.TileSize.y)),
                            Physics.PixelsToMeters(TileMap.TileSize.ToVector2()));
                        collider.CollisionMask    = (uint)(1 << Physics.NameToLayer(layer));
                        collider.CollidesWithMask = Physics.GetLayerCollisionMask(Physics.NameToLayer(layer));
                    }
                }
            }

            _body.Position = Position;
        }