Cutting a body on a point

I currrently have the functionality to cut a selected body in half in order to create two new bodies. My question is, how would I go about cutting a body in two on an arbitrary point on the body? I can understand how I would do this if the body I was cutting was always horizontal. But what if it's at an angle?

My initial guess is using the lower and upper bounds for that body since those positions are in world space. If I know the position of the place you want to cut as well as the center of the body, how do I approach it at this point that would work for any angle the body might be in?

Thanks!
Nevermind...got it! Thanks again!
Well: share with us.. 🙂 !
gamezfactory wrote:
Well: share with us.. 🙂 !

Hehe. I will. Right now, it's just really messy. I'm planning on cleaning up the code a little bit and then I'll post the code.

[Edit] Okay...mind you, I'm sure there are better ways of doing this, but this is only my second project using Box2D and I'm still getting used to how it works. The following code relies on other functionalities that my game engine uses, but the theory behind them should be straight forward(I think). If there is a better way of doing this, please let me know as I would love to improve what I currently have:

Code:
//Box2D-Split Body
      //
      //Note: This function relies on user data that is attached to the
      //      body during the box object's instancing. 
      //      The 3 properties we draw from the data structure are: body width, body height, and block type.
      //      The first parameter is the box body that was selected by the player, and the second
      //      parameter is the position of the mouse in world space where the player wants to cut the body at.
      private function SplitBody(body:b2Body, cutPt:b2Vec2):void
      {
         var body1:b2Body;
         var body2:b2Body;
         
         //Note: The vec values for lower and upper bounds are world space positions.
         var bodyAABB:b2AABB = body.GetFixtureList().GetAABB();
         var bodyLowerBound:b2Vec2 = bodyAABB.lowerBound;
         var bodyUpperBound:b2Vec2 = bodyAABB.upperBound;
      
         if (body)
         {            
            var bodyAngle:Number = body.GetAngle();
            var bodyPos:b2Vec2 = body.GetPosition();         
            
            var m_shape1:b2PolygonShape = new b2PolygonShape();
            var m_shape2:b2PolygonShape = new b2PolygonShape();
            
            var bodyWidth:Number = body.GetUserData().m_Width;
            var bodyHeight:Number = body.GetUserData().m_Height;                     
            
            var halfBodyWidth:Number = bodyWidth / 2;
            var halfBodyHeight:Number = bodyHeight;
            
            //Cut specific calculations
            var w1:Number = cutPt.x - bodyLowerBound.x;
            var w2:Number = bodyUpperBound.x - cutPt.x;
            
            if((halfBodyWidth > 5) && (halfBodyHeight > 5))
            {
               var block1:MovieClip;
               var block2:MovieClip;
               
               var shapeWidth:Number = (halfBodyWidth / RATIO);
               var shapeHeight:Number = (halfBodyHeight / RATIO);
               
               var blockDef:b2PolygonShape = new b2PolygonShape();
               
               var fixtureDef:b2FixtureDef = new b2FixtureDef();
               fixtureDef.friction = body.GetFixtureList().GetFriction()/2;
               fixtureDef.density = body.GetFixtureList().GetDensity();
               fixtureDef.restitution = body.GetFixtureList().GetRestitution();
               
               var blockType:Number = body.GetUserData().m_BlockType;         
               
               //Since we have several block types, we will check to see what type of box
               //was selected, and instantiate two corresponding block objects for the newly cut
               //pieces.  We will also avoid making new bodies that doesn't meet the minimum
               //width requirement.
               switch(blockType)
               {
                  case mcEntity.ENTITY_TYPE_CEMENT_BLOCK:
                     if (w1 * RATIO > 5)
                     {
                        block1 = Game.inst().createGameObject("cementBlock", "");                        
                     }
                     
                     if (w2 * RATIO > 5)
                     {
                        block2 = Game.inst().createGameObject("cementBlock", "");                        
                     }
                     break;
                  case mcEntity.ENTITY_TYPE_CEMENT_BEAM:
                     if (w1 * RATIO > 5)
                     {
                        block1 = Game.inst().createGameObject("cementBeam", "");   
                     }
                     
                     if (w2 * RATIO > 5)
                     {
                        block2 = Game.inst().createGameObject("cementBeam", "");
                     }
                     break;
                  case mcEntity.ENTITY_TYPE_ICE_BLOCK:
                     if (w1 * RATIO > 5)
                     {
                        block1 = Game.inst().createGameObject("iceBlock", "");      
                     }
                     
                     if (w2 * RATIO > 5)
                     {
                        block2 = Game.inst().createGameObject("cementBeam", "");
                     }
                     break;
                  case mcEntity.ENTITY_TYPE_ICE_BEAM:
                     if (w1 * RATIO > 5)
                     {
                        block1 = Game.inst().createGameObject("iceBeam", "");   
                     }
                     
                     if (w2 * RATIO > 5)
                     {
                        block2 = Game.inst().createGameObject("iceBeam", "");
                     }
                     break;
                  case mcEntity.ENTITY_TYPE_TNT_BLOCK:
                     if (w1 * RATIO > 5)
                     {
                        block1 = Game.inst().createGameObject("tntBlock", "");   
                     }
                     
                     if (w2 * RATIO > 5)
                     {
                        block2 = Game.inst().createGameObject("tntBlock", "");
                     }
                     break;
                  case mcEntity.ENTITY_TYPE_WOOD_PLANK1:
                     if (w1 * RATIO > 5)
                     {
                        block1 = Game.inst().createGameObject("woodPlank1", "");   
                     }
                     
                     if (w2 * RATIO > 5)
                     {
                        block2 = Game.inst().createGameObject("woodPlank1", "");
                     }
                     break;
                  case mcEntity.ENTITY_TYPE_WOOD_PLANK2:
                     if (w1 * RATIO > 5)
                     {
                        block1 = Game.inst().createGameObject("woodPlank2", "");
                     }
                     
                     if (w2 * RATIO > 5)
                     {
                        block2 = Game.inst().createGameObject("woodPlank2", "");
                     }
                     break;
                  case mcEntity.ENTITY_TYPE_NULL:
                  default:
                     trace("[ERROR] Undefined Block Type");
                     break;
               }
            }      
            
            if (block1)
            {
               blockDef.SetAsOrientedBox(w1 / 2, shapeHeight / 2, new b2Vec2(0, 0), bodyAngle);
               fixtureDef.shape = blockDef;
               
               block1.x = body.GetUserData().m_Sprite.x;
               block1.y = body.GetUserData().m_Sprite.y;
               var block1Fixture:b2Fixture = block1.m_body.GetFixtureList();
               block1.m_body.DestroyFixture(block1Fixture);
               
               block1.m_body.CreateFixture(fixtureDef);   
               block1.SetSpriteDim(w1 * RATIO, halfBodyHeight);
               block1.SetPosition(cutPt.x - (w1 / 2), bodyPos.y);
            }
            
            if (block2)
            {
               blockDef.SetAsOrientedBox(w2 / 2, shapeHeight / 2, new b2Vec2(0, 0), bodyAngle);
               fixtureDef.shape = blockDef;
               
               block2.x = body.GetUserData().m_Sprite.x;
               block2.y = body.GetUserData().m_Sprite.y;      
               var block2Fixture:b2Fixture = block2.m_body.GetFixtureList();
               block2.m_body.DestroyFixture(block2Fixture);      
               
               block2.m_body.CreateFixture(fixtureDef);   
               block2.SetSpriteDim(w2 * RATIO, halfBodyHeight);
               block2.SetPosition(cutPt.x + (w2 / 2), bodyPos.y);
            }
            
            if (body.GetUserData().m_Sprite)
            {
               var bodySprite:MovieClip = body.GetUserData().m_Sprite;
               body.GetUserData().m_Sprite.parent.removeChild(bodySprite);
            }
            
            Game.entityManager.RemoveEntityFromWorld(body.GetUserData());
            _world.DestroyBody(body);
         }      
            
         splitDone = true;
      }


Again, please feel free to suggest improvements or if you have any questions, don't hesitate to ask. I'll try my best to answer your questions.
You can also check the Farseer engine, written in C#, based off of Box2D. It includes a cutting tool, as well as simplification and several other neat utilities.

-P
Paril wrote:
You can also check the Farseer engine, written in C#, based off of Box2D. It includes a cutting tool, as well as simplification and several other neat utilities.

-P

Hey Paril. Thanks! I've checked out Farseer's documentation but couldn't find anything that discusses the cutting tool. I also tried searching the discussions but had a hard time finding anything. I did see something in the documentation about being able to cut through the world affecting multiple bodies, but I couldn't find one that can only affect 1 body. This is what I found:

Code:
CuttingTools.Cut(World, start, end, 0.001f);


The example image they gave showed a line being drawn across multiple bodies along that line and then cutting it. Would you happen to have a direct link or example I can refer to? Thanks!

[Edit] I understand Farseer is an XNA version of Box2D with some additions, and it looks like the cutting tool is built into Farseer? Is there a Flash equivalent of this functionality?
It's a generic engine with ties to XNA; it also has a class-based version that does not use XNA.

You'd have to convert it to Flash, but it's supposed to be a good class. I don't know much about it, it was probably a tool submitted by another developer.

-P
Hi,
Nice Post...I want to do the same thing(Cutting a body) for iphone.can you have any samples to do that.

Thanks...
anish wrote:
Hi,
Nice Post...I want to do the same thing(Cutting a body) for iphone.can you have any samples to do that.

Thanks...

Hey anish. My initial implementation has a lot of bugs and breaks when the body is at steep angles. I had to get help from a co-worker in order to get it working properly. I'll see if I can post the final solution later on but because it's related to work, I'll have to see if I have permission to post up the complete solution.

Original thread: https://box2d.org/forum/viewtopic.php?t=6251