A type of body that supports multiple fixtures that can break apart.
Example #1
0
        public void CreateColonyShip(float strength, Vector2 position)
        {
            uint[] data = new uint[_texture.Width * _texture.Height];
            _texture.GetData(data);
            List<Vertices> list = PolygonTools.CreatePolygon(data, _texture.Width, 1.55f, 1, false, false); //Play around with hullTolerance
            for (int i = 0; i < list.Count; i++)
            {
                Vertices polygon = list[i];
                Vector2 centroid = -polygon.GetCentroid();
                polygon.Translate(centroid);
                polygon = SimplifyTools.CollinearSimplify(polygon);
                polygon = SimplifyTools.ReduceByDistance(polygon, 4);
                List<Vertices> triangulation = Triangulate.ConvexPartition(polygon, TriangulationAlgorithm.Bayazit);

                // Scale to your screen view
                Vector2 vertScale = new Vector2(1 / _ratio);
                foreach (var vertices in triangulation)
                {
                    vertices.Scale(vertScale);
                }

                colonyShipBreakableBody = new BreakableBody(world, triangulation, 20, position: position, rotation: -MathHelper.PiOver2);
                colonyShipBreakableBody.Strength = strength;
                colonyShipBreakableBody.MainBody.CollisionCategories = Category.Cat1;
                colonyShipBreakableBody.MainBody.CollidesWith = Category.All;
                world.AddBreakableBody(colonyShipBreakableBody);

                _colonyShipSprite = new Sprite(_texture) { Position = colonyShipBreakableBody.MainBody.Position * _ratio, Rotation = colonyShipBreakableBody.MainBody.Rotation };

                colonyShipBreakableBody.MainBody.OnSeparation += Reload_OnSeparation;

            }
        }
Example #2
0
 public static BreakableBody CreateBreakableBody(World world, IEnumerable<Shape> shapes, Vector2 position = new Vector2(), float rotation = 0)
 {
     BreakableBody breakableBody = new BreakableBody(world, shapes, position, rotation);
     breakableBody.MainBody.Position = position;
     world.AddBreakableBody(breakableBody);
     return breakableBody;
 }
Example #3
0
        public void RemoveBreakableBody(BreakableBody breakableBody)
        {
            //The breakable body list does not contain the body you tried to remove.
            Debug.Assert(BreakableBodyList.Contains(breakableBody));

            BreakableBodyList.Remove(breakableBody);
        }
Example #4
0
        public static BreakableBody CreateBreakableBody(this World world, IEnumerable<Shape> shapes, Vector2 position)
        {
            BreakableBody breakableBody = new BreakableBody(shapes, world);
            breakableBody.MainBody.Position = position;
            world.AddBreakableBody(breakableBody);

            return breakableBody;
        }
        /// <summary>
        /// Creates a breakable body. You would want to remove collinear points before using this.
        /// </summary>
        /// <param name="world">The world.</param>
        /// <param name="vertices">The vertices.</param>
        /// <param name="density">The density.</param>
        /// <param name="position">The position.</param>
        /// <returns></returns>
        public static BreakableBody CreateBreakableBody(World world, Vertices vertices, float density, Vector2 position)
        {
            List<Vertices> triangles = EarclipDecomposer.ConvexPartition(vertices);

            BreakableBody breakableBody = new BreakableBody(triangles, world, density);
            breakableBody.MainBody.Position = position;
            world.AddBreakableBody(breakableBody);

            return breakableBody;
        }
Example #6
0
        public static BreakableBody CreateBreakableBody(World world, Vertices vertices, float density, Vector2 position = new Vector2(), float rotation = 0)
        {
            //TODO: Implement a Voronoi diagram algorithm to split up the vertices
            List<Vertices> triangles = Triangulate.ConvexPartition(vertices, TriangulationAlgorithm.Earclip);

            BreakableBody breakableBody = new BreakableBody(world, triangles, density, position, rotation);
            breakableBody.MainBody.Position = position;
            world.AddBreakableBody(breakableBody);
            return breakableBody;
        }
Example #7
0
        /// <summary>
        /// Creates a breakable body. You would want to remove collinear points before using this.
        /// </summary>
        /// <param name="world">The world.</param>
        /// <param name="vertices">The vertices.</param>
        /// <param name="density">The density.</param>
        /// <param name="position">The position.</param>
        /// <returns></returns>
        public static BreakableBody CreateBreakableBody(this World world, Vertices vertices, float density, Vector2 position)
        {
            List<Vertices> triangles = Triangulate.ConvexPartition(vertices, TriangulationAlgorithm.Earclip);

            BreakableBody breakableBody = new BreakableBody(triangles, world, density);
            breakableBody.MainBody.Position = position;
            world.AddBreakableBody(breakableBody);

            return breakableBody;
        }
Example #8
0
        /// <summary>
        /// Creates a breakable body. You would want to remove collinear points before using this.
        /// </summary>
        /// <param name="world">The world.</param>
        /// <param name="vertices">The vertices.</param>
        /// <param name="density">The density.</param>
        /// <param name="position">The position.</param>
        /// <returns></returns>
        public static BreakableBody CreateBreakableBody(World world, Vertices vertices, float density, Vector2 position,
            PressPlay.FFWD.Components.Collider userData)
        {
            List<Vertices> triangles = EarclipDecomposer.ConvexPartition(vertices);

            BreakableBody breakableBody = new BreakableBody(triangles, world, density, userData);
            breakableBody.MainBody.Position = position;
            world.AddBreakableBody(breakableBody);

            return breakableBody;
        }
        public InvaderMother(Game1 game, World world, float ratio, string texturePath, string textureMissilePath, string textureParticlePath, BreakableBody target)
            : base(game, world, texturePath)
        {
            _texture = Content.Load<Texture2D>(texturePath);

            _ratio = ratio;
            this.target = target;
            _missiles = new Missiles(game, world, ratio, textureMissilePath);
               // particles = new ParticleEngine(game, textureParticlePath);
            Practice = false;
            Alive = false;
        }
Example #10
0
        public void SetPlayerKeys(BreakableBody breakableBody, float speed, bool negativeForce = false)
        {
            var playerMovement = _inputManager.AddListener(new KeyboardListenerSettings());
            var cosAngle = (float)Math.Cos(breakableBody.MainBody.Rotation);
            var sinAngle = (float)Math.Sin(breakableBody.MainBody.Rotation);
            //var deltaTime = (float)gameTime.ElapsedGameTime.TotalSeconds;

            if (!negativeForce) playerMovement.KeyPressed += (sender, arg) => { if (arg.Key == Keys.Down) breakableBody.MainBody.ApplyLinearImpulse(new Vector2(-speed * cosAngle, -speed * sinAngle)); };

            playerMovement.KeyPressed += (sender, arg) =>
            {
                if (arg.Key == Keys.Up) breakableBody.MainBody.ApplyLinearImpulse(new Vector2(speed * cosAngle, speed * sinAngle));
                if (arg.Key == Keys.Left) breakableBody.MainBody.ApplyTorque(10f);
                if (arg.Key == Keys.Right) breakableBody.MainBody.ApplyTorque(-10f);
            };
        }
 public void FireMissile(BreakableBody body, float speed)
 {
     var cosAngle = (float)Math.Cos(body.MainBody.Rotation);
     var sinAngle = (float)Math.Sin(body.MainBody.Rotation);
     var distanceFromRotationX = (float)(body.MainBody.Position.X + 7.3 * Math.Cos(body.MainBody.Rotation));
     var distanceFromRotationY = (float)(body.MainBody.Position.Y + 7.3 * Math.Sin(body.MainBody.Rotation));
     _missiles.CreateBullet(new Vector2(distanceFromRotationX, distanceFromRotationY), body.MainBody.Rotation, new Vector2(speed * cosAngle * 30, speed * sinAngle * 30) + body.MainBody.LinearVelocity);
 }
        public override void LoadContent(AxiosGameScreen gameScreen)
        {
            base.LoadContent(gameScreen);

            Body = new BreakableBody();
        }
Example #13
0
        private void KeyboardHandlerPlayer(KeyboardState keyboardState, GameTime gameTime, BreakableBody playerBreakableBody,float speed = 1f)
        {
            var cosAngle = (float)Math.Cos(playerBreakableBody.MainBody.Rotation);
            var sinAngle = (float)Math.Sin(playerBreakableBody.MainBody.Rotation);
            /*this doesn't work if camera controlls called before */
            if (keyboardState.IsKeyDown(Keys.Escape) & !_previousState.IsKeyDown(Keys.Escape))Exit();
            if (keyboardState.IsKeyDown(Keys.L) & !_previousState.IsKeyDown(Keys.L))
            {
                foreach (var item in _invaders.invaderBreakableBody)
                {
                    item.Break();
                }
            }
            if (keyboardState.IsKeyDown(Keys.N) & !_previousState.IsKeyDown(Keys.N)) negativeForce = !negativeForce;
            if (keyboardState.IsKeyDown(Keys.P) & !_previousState.IsKeyDown(Keys.P)) Pause = !Pause;
            if (!Pause)
            {
                if (keyboardState.IsKeyDown(Keys.Space) && !_previousState.IsKeyDown(Keys.Space))
                {
                    _player.FireMissile(speed);
                    System.Diagnostics.Debug.WriteLine("bullet");
                }

                if (keyboardState.IsKeyDown(Keys.Up))
                {
                    _player.Boosting = true;
                    playerBreakableBody.MainBody.ApplyLinearImpulse(new Vector2(speed * cosAngle, speed * sinAngle));
                }
                else
                {
                    if (!GamePad.GetState(PlayerIndex.One).IsConnected) _player.Boosting = false;
                }

                if (keyboardState.IsKeyDown(Keys.Right))
                    playerBreakableBody.MainBody.ApplyTorque(10f);

                if (keyboardState.IsKeyDown(Keys.Left))
                    playerBreakableBody.MainBody.ApplyTorque(-10f);

                if (keyboardState.IsKeyDown(Keys.Down))
                {
                    if (!negativeForce)//!false
                        playerBreakableBody.MainBody.ApplyLinearImpulse(new Vector2(-speed * cosAngle * 0.3f, -speed * sinAngle * 0.3f));
                    else
                    {
                        playerBreakableBody.MainBody.LinearVelocity = Vector2.Zero;
                        playerBreakableBody.MainBody.AngularVelocity *= .1f; //playwith this
                    }
                }
            }

            _previousState = keyboardState;
        }
Example #14
0
        public void GamePadController(GamePadState state, GameTime gameTime, BreakableBody playerBreakableBody, float speed = 1f)
        {
            var deltaTime = (float)gameTime.ElapsedGameTime.TotalSeconds;
            const float movementSpeed = 200;
            const float zoomSpeed = 0.5f;
            var cosAngle = (float)Math.Cos(playerBreakableBody.MainBody.Rotation);
            var sinAngle = (float)Math.Sin(playerBreakableBody.MainBody.Rotation);
            if (_camera.Position.Y - 800 < 1388 && state.DPad.Down == ButtonState.Pressed )
                _camera.Move(new Vector2(0, movementSpeed) * deltaTime);
            if (_camera.Position.Y > -668 && state.DPad.Up == ButtonState.Pressed )
                _camera.Move(new Vector2(0, -movementSpeed) * deltaTime);
            if (_camera.Position.X > -628 && state.DPad.Left == ButtonState.Pressed )
                _camera.Move(new Vector2(-movementSpeed, 0) * deltaTime);
            if (_camera.Position.X - 720 < 1428 && state.DPad.Right == ButtonState.Pressed )
                _camera.Move(new Vector2(movementSpeed, 0) * deltaTime);
            if (state.Buttons.Y == ButtonState.Pressed )
                _camera.ZoomIn(zoomSpeed * deltaTime);
            if (state.Buttons.X == ButtonState.Pressed)
                _camera.ZoomOut(zoomSpeed * deltaTime);
            if (state.Buttons.Back == ButtonState.Pressed && !(_previousStateGamepad.Buttons.Back == ButtonState.Pressed))
                Exit();
            if (state.Buttons.Start == ButtonState.Pressed && !(_previousStateGamepad.Buttons.Start == ButtonState.Pressed))
                Pause = !Pause;
            if (state.Buttons.LeftShoulder == ButtonState.Pressed && !(_previousStateGamepad.Buttons.LeftShoulder == ButtonState.Pressed))
                negativeForce = !negativeForce;

            if (!Pause)
            {
                if (state.Buttons.RightShoulder == ButtonState.Pressed && !(_previousStateGamepad.Buttons.RightShoulder == ButtonState.Pressed))
                    _player.FireMissile(speed);
                if (state.Buttons.A == ButtonState.Pressed)
                {
                    _player.Boosting = true;
                    playerBreakableBody.MainBody.ApplyLinearImpulse(new Vector2(speed * cosAngle, speed * sinAngle));
                }
                else _player.Boosting = false;

                if (state.Buttons.B == ButtonState.Pressed)
                {
                    if (!negativeForce)//!false
                        playerBreakableBody.MainBody.ApplyLinearImpulse(new Vector2(-speed * cosAngle * 0.3f, -speed * sinAngle * 0.3f));
                    else
                    {
                        playerBreakableBody.MainBody.LinearVelocity = Vector2.Zero;
                        playerBreakableBody.MainBody.AngularVelocity *= .1f; //playwith this
                    }
                }

                playerBreakableBody.MainBody.ApplyTorque(8f * state.ThumbSticks.Left.X);
            }

            _previousStateGamepad = state;
        }
Example #15
0
        public override void LoadContent()
        {
            base.LoadContent();

            DebugView.AppendFlags(DebugViewFlags.Shape);

            World.Gravity = Vector2.Zero;

            _border = new Border(World, ScreenManager, Camera);

            Texture2D alphabet = ScreenManager.Content.Load<Texture2D>("Samples/alphabet");

            uint[] data = new uint[alphabet.Width * alphabet.Height];
            alphabet.GetData(data);

            List<Vertices> list = PolygonTools.CreatePolygon(data, alphabet.Width, 3.5f, 20, true, true);

            float yOffset = -5f;
            float xOffset = -14f;
            for (int i = 0; i < list.Count; i++)
            {
                if (i == 9)
                {
                    yOffset = 0f;
                    xOffset = -14f;
                }
                if (i == 18)
                {
                    yOffset = 5f;
                    xOffset = -12.25f;
                }
                Vertices polygon = list[i];
                Vector2 centroid = -polygon.GetCentroid();
                polygon.Translate(ref centroid);
                polygon = SimplifyTools.CollinearSimplify(polygon);
                polygon = SimplifyTools.ReduceByDistance(polygon, 4);
                List<Vertices> triangulated = Triangulate.ConvexPartition(polygon, TriangulationAlgorithm.Bayazit);

#if WINDOWS_PHONE
                const float scale = 0.6f;
#else
                const float scale = 1f;
#endif
                Vector2 vertScale = new Vector2(ConvertUnits.ToSimUnits(1)) * scale;
                foreach (Vertices vertices in triangulated)
                {
                    vertices.Scale(ref vertScale);
                }

                BreakableBody breakableBody = new BreakableBody(World, triangulated, 1);
                breakableBody.MainBody.Position = new Vector2(xOffset, yOffset);
                breakableBody.Strength = 100;
                World.AddBreakableBody(breakableBody);

                xOffset += 3.5f;
            }
        }
Example #16
0
        public static List<Texture2D> BreakableTextureFragments(BreakableBody body, string textureName)
        {
            List<Texture2D> result = new List<Texture2D>();
            if (_contentWrapper != null)
            {
                Vector2 scale = ConvertUnits.ToDisplayUnits(Vector2.One);
                foreach (Fixture f in body.Parts)
                {
                    Vertices v = null;
                    if (f.Shape is PolygonShape)
                    {
                        v = new Vertices(((PolygonShape)f.Shape).Vertices);
                        v.Scale(ref scale);
                    }
                    if (v != null)
                    {
                        AABB polygonBounds = v.GetAABB();
                        List<Vertices> decomposedVertices;
                        if (!v.IsConvex())
                        {
                            decomposedVertices = Triangulate.ConvexPartition(v, TriangulationAlgorithm.Bayazit);
                        }
                        else
                        {
                            decomposedVertices = new List<Vertices>();
                            decomposedVertices.Add(v);
                        }
                        List<VertexPositionColorTexture[]> verticesFill = new List<VertexPositionColorTexture[]>(decomposedVertices.Count);
                        for (int i = 0; i < decomposedVertices.Count; i++)
                        {
                            verticesFill.Add(new VertexPositionColorTexture[3 * (decomposedVertices[i].Count - 2)]);
                            for (int j = 0; j < decomposedVertices[i].Count - 2; j++)
                            {
                                // fill vertices
                                verticesFill[i][3 * j].Position = new Vector3(decomposedVertices[i][0] - polygonBounds.Center, 0f);
                                verticesFill[i][3 * j + 1].Position = new Vector3(decomposedVertices[i].NextVertex(j) - polygonBounds.Center, 0f);
                                verticesFill[i][3 * j + 2].Position = new Vector3(decomposedVertices[i].NextVertex(j + 1) - polygonBounds.Center, 0f);

                                verticesFill[i][3 * j].TextureCoordinate = new Vector2(decomposedVertices[i][0].X / _textureList[textureName].Width, decomposedVertices[i][0].Y / _textureList[textureName].Height - 1f);
                                verticesFill[i][3 * j + 1].TextureCoordinate = new Vector2(decomposedVertices[i].NextVertex(j).X / _textureList[textureName].Width, decomposedVertices[i].NextVertex(j).Y / _textureList[textureName].Height - 1f);
                                verticesFill[i][3 * j + 2].TextureCoordinate = new Vector2(decomposedVertices[i].NextVertex(j + 1).X / _textureList[textureName].Width, decomposedVertices[i].NextVertex(j + 1).Y / _textureList[textureName].Height - 1f);
                                verticesFill[i][3 * j].Color = verticesFill[i][3 * j + 1].Color = verticesFill[i][3 * j + 2].Color = Color.Transparent;
                            }
                        }

                        Vector2 vertsSize = new Vector2(polygonBounds.UpperBound.X - polygonBounds.LowerBound.X, polygonBounds.UpperBound.Y - polygonBounds.LowerBound.Y);
                        result.Add(_contentWrapper.RenderTexture((int)vertsSize.X, (int)vertsSize.Y, _textureList.ContainsKey(textureName) ? _textureList[textureName] : null, Color.White, verticesFill, new VertexPositionColor[0]));
                    }
                    else
                    {
                        result.Add(_textureList["Blank"]);
                    }
                }
            }
            return result;
        }
Example #17
0
        public override void LoadContent()
        {
            base.LoadContent();

            World.Gravity = Vector2.Zero;

            _border = new Border(World, this, ScreenManager.GraphicsDevice.Viewport);
            _breakableObject = new List<List<Vertices>>();

            Texture2D alphabet = ScreenManager.Content.Load<Texture2D>("Samples/alphabet");

            uint[] data = new uint[alphabet.Width * alphabet.Height];
            alphabet.GetData(data);

            List<Vertices> list = PolygonTools.CreatePolygon(data, alphabet.Width, 3.5f, 20, true, true);

            float yOffset = -5f;
            float xOffset = -14f;
            for (int i = 0; i < list.Count; i++)
            {
                if (i == 9)
                {
                    yOffset = 0f;
                    xOffset = -14f;
                }
                if (i == 18)
                {
                    yOffset = 5f;
                    xOffset = -12.25f;
                }
                Vertices polygon = list[i];
                Vector2 centroid = -polygon.GetCentroid();
                polygon.Translate(ref centroid);
                //polygon = SimplifyTools.CollinearSimplify(polygon); // this breaks the split hole function
                polygon = SimplifyTools.ReduceByDistance(polygon, 4);
                List<Vertices> triangulated = BayazitDecomposer.ConvexPartition(polygon);

            #if WINDOWS_PHONE
                const float scale = 0.6f;
            #else
                const float scale = 1f;
            #endif
                Vector2 vertScale = new Vector2(ConvertUnits.ToSimUnits(1)) * scale;
                foreach (Vertices vertices in triangulated)
                {
                    vertices.Scale(ref vertScale);
                }

                BreakableBody breakableBody = new BreakableBody(triangulated, World, 1);
                breakableBody.MainBody.Position = new Vector2(xOffset, yOffset);
                breakableBody.Strength = 100;
                breakableBody.MainBody.UserData = i;
                World.AddBreakableBody(breakableBody);

                polygon.Scale(ref vertScale);
                _breakableObject.Add(polygon.SplitAtHoles());

                xOffset += 3.5f;
            }
        }
        public void CreateMother(float strength, Vector2 position, float angle = 0f)
        {
            Alive = true;
            uint[] data = new uint[_texture.Width * _texture.Height];
            _texture.GetData(data);
            List<Vertices> list = PolygonTools.CreatePolygon(data, _texture.Width, 1.55f, 1, true, true); //Play around with hullTolerance
            for (int i = 0; i < list.Count; i++)
            {
                //System.Diagnostics.Debug.WriteLine(list.Count + "  List Count");
                Vertices polygon = list[i];
                Vector2 centroid = -polygon.GetCentroid();
                polygon.Translate(centroid);
                polygon = SimplifyTools.CollinearSimplify(polygon);
                polygon = SimplifyTools.ReduceByDistance(polygon, 4);
                List<Vertices> triangulation = Triangulate.ConvexPartition(polygon, TriangulationAlgorithm.Bayazit);

                // Scale to your screen view
                Vector2 vertScale = new Vector2(1 / _ratio);
                foreach (var vertices in triangulation)
                {
                    vertices.Scale(vertScale);
                }

                MotherBreakableBody = new BreakableBody(world, triangulation, 10, position: position, rotation: angle);
                MotherBreakableBody.Strength = strength;
                //MotherBreakableBody.MainBody.Restitution = 0.00010f;
                MotherBreakableBody.MainBody.CollisionCategories = Category.Cat2 & ~Category.Cat5;
                MotherBreakableBody.MainBody.CollidesWith = Category.All & ~Category.Cat5;
                //MotherBreakableBody.MainBody.SleepingAllowed = false;
                //System.Diagnostics.Debug.WriteLine("mass of invader: " + MotherBreakableBody.MainBody.Mass);
                world.AddBreakableBody(MotherBreakableBody);

                motherTexture= new Sprite(_texture) { Position = MotherBreakableBody.MainBody.Position * _ratio, Rotation = MotherBreakableBody.MainBody.Rotation };

            }
        }
Example #19
0
 public void AddBreakableBody(BreakableBody breakableBody)
 {
     BreakableBodyList.Add(breakableBody);
 }
Example #20
0
 public void AddBreakableBody(BreakableBody breakableBody)
 {
     BreakableBodyList.Add(breakableBody);
 }
Example #21
0
        public void RemoveBreakableBody(BreakableBody breakableBody)
        {
            //The breakable body list does not contain the body you tried to remove.
            Debug.Assert(BreakableBodyList.Contains(breakableBody));

            BreakableBodyList.Remove(breakableBody);
        }
Example #22
0
        private void CreatePlayerBreakableBody(float strength, Vector2 position)
        {
            uint[] data = new uint[_textureReference.Width * _textureReference.Height];
            _textureReference.GetData(data);
            List<Vertices> list = PolygonTools.CreatePolygon(data, _textureReference.Width, 40f, 1, true, false); //Play around with hullTolerance 1.55
            for (int i = 0; i < list.Count; i++)
            {
                Vertices polygon = list[i];
                Vector2 centroid = -polygon.GetCentroid();
                polygon.Translate(centroid);
                polygon = SimplifyTools.CollinearSimplify(polygon);
                polygon = SimplifyTools.ReduceByDistance(polygon, 4);
                List<Vertices> triangulation = Triangulate.ConvexPartition(polygon, TriangulationAlgorithm.Earclip);

                // Scale to your screen view. A little smaller so texture overlaps the body, usually this would be 1f.
                Vector2 vertScale = new Vector2(.9f / _ratio);
                foreach (var vertices in triangulation)
                {
                    vertices.Scale(vertScale);
                }

                playerBreakableBody = new BreakableBody(world, triangulation, 1, position: position, rotation:-MathHelper.PiOver2);
                playerBreakableBody.Strength = strength;
                //playerBreakableBody.MainBody.Restitution = 0.00010f;
                playerBreakableBody.MainBody.CollisionCategories = Category.Cat1;
                playerBreakableBody.MainBody.CollidesWith = Category.All;

                world.AddBreakableBody(playerBreakableBody);
            }
        }