Cut a body with CuttingTools..

Topics: Developer Forum
Mar 20, 2013 at 7:25 PM
Hello mates.

I'm trying to cut a body I have to 2.

I'm using farseer physics, with there CuttingTools lib.

I have an object-but when i try to cut it it doesnt split to 2.

I even draw the line of the cut:

befor cut:

http://postimage.org/image/4xk8e63hv/

after cut:

http://postimage.org/image/pt6ipezoz

Here's my main code(debugging code):

*Im doing few cuts but nothing is working

What can I do to make it work?

Thank you.
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;
using FarseerPhysics.Dynamics;
using FarseerPhysics.Factories;
using FarseerPhysics.Collision.Shapes;
using FarseerPhysics.Common;
using FarseerPhysics.Controllers;
using FarseerPhysics.Dynamics.Contacts;
using FarseerPhysics.Common.PolygonManipulation;
using FarseerPhysics.DebugViews;

namespace WindowsPhoneGame4
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        World _world;
        DebugViewXNA _debugView;

        Body box1;
        Texture2D box;

        bool check;
        static Texture2D line;

        Fixture fixture;

        private const float Scale = 100f;
        Vector2 origin;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            graphics.PreferredBackBufferWidth = 480;
            graphics.PreferredBackBufferHeight = 800;

            // Frame rate is 30 fps by default for Windows Phone.
            TargetElapsedTime = TimeSpan.FromTicks(333333);

            // Extend battery life under lock.
            InactiveSleepTime = TimeSpan.FromSeconds(1);
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            check = true;
            line = Content.Load<Texture2D>("black");
            box = Content.Load<Texture2D>("football");

            _world = new World(new Vector2(0, 6f));

            _debugView = new DebugViewXNA(_world);
           
            
            
             _debugView.RemoveFlags(FarseerPhysics.DebugViewFlags.Controllers);
             _debugView.RemoveFlags(FarseerPhysics.DebugViewFlags.Joint);
         

             _debugView.DefaultShapeColor = Color.LightPink;
             _debugView.SleepingShapeColor = Color.MediumPurple;

            _debugView.LoadContent(GraphicsDevice, Content);

            box1 = BodyFactory.CreateCircle(_world, 0.5f, 1f);
            box1.Position = new Vector2(1f, 2f);
            box1.BodyType = BodyType.Dynamic;

            origin = new Vector2(box.Width / 2f, box.Height / 2f);

            fixture = FixtureFactory.AttachCircle(0.5f, 1f, box1);
            
        }


        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            _world.Step(Math.Min((float)gameTime.ElapsedGameTime.TotalSeconds, (1f / 30f)));
            
            if (check)
            {

                FarseerPhysics.Common.Vertices a, b;
                Vector2 force = box1.GetLocalPoint(box1.Position);
                box1.ApplyLinearImpulse(force + new Vector2(1,1));
                CuttingTools.Cut(_world, new Vector2(0.5f, 2.1f), new Vector2(1.5f, 2.2f), 0.001f);
                CuttingTools.Cut(_world, new Vector2(50, 210), new Vector2(150, 215), 0.001f);
                CuttingTools.SplitShape(fixture, new Vector2(0.5f, 2.1f), new Vector2(1.5f, 2.2f), 0.001f, out a, out b);
                CuttingTools.SplitShape(fixture, new Vector2(50, 210), new Vector2(150, 215), 0.001f, out a, out b);

                check = false;
            }
            
            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

           

            spriteBatch.Begin();

            DrawLineSegment(spriteBatch, new Vector2(50, 210), new Vector2(200, 215), Color.Black, 1);

            spriteBatch.End();

            var projection = Matrix.CreateOrthographicOffCenter(0f, 4.8f, 8f, 0f, 0f, 1f);
            //Matrix projection = Matrix.Identity;
            Mat22 arg = new Mat22();
            Matrix view = Matrix.Identity;
            Vector2 p = box1.Position;
            Transform transform = new Transform(ref p, ref arg);
            transform.SetIdentity();

            _debugView.RenderDebugData(ref projection, ref view);

          /* _debugView.BeginCustomDraw(ref projection, ref view);
           _debugView.DrawCircle(box1.Position, 0.5f, Color.Black);
            _debugView.DrawShape(fixture, transform, Color.Black);

            _debugView.EndCustomDraw();*/

            base.Draw(gameTime);
        }

        public static void DrawLineSegment(SpriteBatch spriteBatch, Vector2 point1, Vector2 point2, Color color, int lineWidth)
        {

            float angle = (float)Math.Atan2((point2 - point1).Y, (point2 - point1).X);
            float length = Vector2.Distance(point1, point2);

            spriteBatch.Draw(line, point1, null, color, angle, Vector2.Zero, new Vector2(length, lineWidth), SpriteEffects.None, 0f);

        }
    }
}
Mar 22, 2013 at 8:09 PM
someone?