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.