forgive me if i seem a bit naive about Box2D in general - i've only just started messing around with it.
today, i decided to try and mix a rough tile engine with Box2D and create a sort of "castle" of blocks, and have a ball come crashing down on it or into it.
my problem is that the edges, or the turrets or whatever, of the towers have their center of mass right in the, you know, center. they immediately fall off the block they are resting on, i would assume, because their center of mass isn't resting toward the inside of the castle. while i thought a centered, uh, center of mass would work fine, it apparently doesn't.
my current workaround is to set them to be asleep. while it works pretty well, there are some situations in which it just doesn't work (such as the entire castle sliding/collapsing underneath the two sleeping blocks).
is there a way to move the center of mass so that the blocks won't fall off? also, here's my source. other than the Box2D engine, nothing external is required. my stage is set to 550x400, at 40 fps.
today, i decided to try and mix a rough tile engine with Box2D and create a sort of "castle" of blocks, and have a ball come crashing down on it or into it.
my problem is that the edges, or the turrets or whatever, of the towers have their center of mass right in the, you know, center. they immediately fall off the block they are resting on, i would assume, because their center of mass isn't resting toward the inside of the castle. while i thought a centered, uh, center of mass would work fine, it apparently doesn't.
my current workaround is to set them to be asleep. while it works pretty well, there are some situations in which it just doesn't work (such as the entire castle sliding/collapsing underneath the two sleeping blocks).
is there a way to move the center of mass so that the blocks won't fall off? also, here's my source. other than the Box2D engine, nothing external is required. my stage is set to 550x400, at 40 fps.
Code:
import Box2D.Dynamics.*;
import Box2D.Collision.*;
import Box2D.Collision.Shapes.*;
import Box2D.Dynamics.Joints.*;
import Box2D.Dynamics.Contacts.*;
import Box2D.Common.*;
import Box2D.Common.Math.*;
import flash.events.*;
import flash.display.Sprite;
var q = 40;
var p = 30;
var aabb:b2AABB = new b2AABB();
aabb.lowerBound.Set(-100, -100);
aabb.upperBound.Set(100, 100);
var sleep:Boolean = true;
var grav:b2Vec2 = new b2Vec2(0, 9.8);
var m_world:b2World = new b2World(aabb, grav, sleep);
var m_iterations:int = 40;
var m_timeStep:Number = 1/q;
var cd:b2CircleDef = new b2CircleDef;
var bd:b2BodyDef = new b2BodyDef;
var pd:b2PolygonDef = new b2PolygonDef;
var b:b2Body;
//castle stuff
var castle:Array = new Array();
castle.push([0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]);
castle.push([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]);
castle.push([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]);
castle.push([0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,1,0,0,0,1,0]);
castle.push([0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,1,0,1,0,0]);
castle.push([0,0,0,0,0,0,0,1,0,1,0,0,0,1,0,0,0,1,0,1,0,0]);
castle.push([0,0,0,0,0,0,0,1,0,1,0,0,1,0,1,0,0,1,0,1,0,0]);
castle.push([0,0,0,0,0,0,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,0]);
castle.push([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]);
//
function createCircle(x:uint, y:uint, i:Boolean) {
cd = new b2CircleDef;
cd.radius = 30/p;
cd.density = 5;
cd.friction = .5;
cd.restitution = .5;
bd = new b2BodyDef;
bd.position.Set(x/p, y/p);
b = m_world.CreateDynamicBody(bd);
b.CreateShape(cd);
b.SetMassFromShapes();
b.ApplyImpulse(new b2Vec2(0, (Math.random() * 550) + 150), b.GetWorldCenter());
}
function createBox(x:uint, y:uint, sleeping:Boolean) {
bd = new b2BodyDef;
bd.position.Set(x/p, y/p);
bd.isSleeping = sleeping;
pd = new b2PolygonDef;
pd.SetAsBox(25/p, 25/p);
pd.density = 15;
pd.friction = .3;
b = m_world.CreateDynamicBody(bd);
b.CreateShape(pd);
b.SetMassFromShapes();
}
function createStaticBox(x:Number, y:Number, w:Number, h:Number) {
bd = new b2BodyDef;
bd.position.Set(x/p, y/p);
pd = new b2PolygonDef;
pd.SetAsBox((w/2)/p, (h/2)/p);
pd.friction = .5;
b = m_world.CreateStaticBody(bd);
b.CreateShape(pd);
b.SetMassFromShapes();
}
bd = new b2BodyDef;
bd.position.Set(0, 400/p);
pd.vertexCount = 3;
pd.vertices[0] = new b2Vec2(0, 0);
pd.vertices[1] = new b2Vec2(0, ((-Math.random() * 125) - 75)/p);
pd.vertices[2] = new b2Vec2(((Math.random() * 150) + 50)/p, 0);
pd.friction = 0;
b = m_world.CreateStaticBody(bd);
b.CreateShape(pd);
b.SetMassFromShapes();
function Update(e:Event):void {
m_world.Step(m_timeStep, m_iterations);
}
for (var wy = 0; wy < castle.length; wy++) {
for (var ex = 0; ex < castle[wy].length; ex++) {
switch (castle[wy][ex]) {
case 1 :
createBox(ex * 25, wy*50, true);
break;
case 2 :
createCircle(ex * 25, wy * 50, true);
break;
}
}
}
createStaticBox(0, 200, 25, 400);
createStaticBox(550, 200, 25, 400);
createStaticBox(275, 400, 550, 50);
var dbgDraw:b2DebugDraw = new b2DebugDraw();
dbgDraw.m_sprite = new Sprite();
dbgDraw.m_drawScale = 30;
dbgDraw.m_fillAlpha = 1;
dbgDraw.m_alpha = .5;
dbgDraw.m_lineThickness = 0;
dbgDraw.m_drawFlags = b2DebugDraw.e_shapeBit | b2DebugDraw.e_centerOfMassBit;
m_world.SetDebugDraw(dbgDraw);
addChild(dbgDraw.m_sprite);
addEventListener(Event.ENTER_FRAME, Update);
import Box2D.Collision.*;
import Box2D.Collision.Shapes.*;
import Box2D.Dynamics.Joints.*;
import Box2D.Dynamics.Contacts.*;
import Box2D.Common.*;
import Box2D.Common.Math.*;
import flash.events.*;
import flash.display.Sprite;
var q = 40;
var p = 30;
var aabb:b2AABB = new b2AABB();
aabb.lowerBound.Set(-100, -100);
aabb.upperBound.Set(100, 100);
var sleep:Boolean = true;
var grav:b2Vec2 = new b2Vec2(0, 9.8);
var m_world:b2World = new b2World(aabb, grav, sleep);
var m_iterations:int = 40;
var m_timeStep:Number = 1/q;
var cd:b2CircleDef = new b2CircleDef;
var bd:b2BodyDef = new b2BodyDef;
var pd:b2PolygonDef = new b2PolygonDef;
var b:b2Body;
//castle stuff
var castle:Array = new Array();
castle.push([0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]);
castle.push([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]);
castle.push([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]);
castle.push([0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,1,0,0,0,1,0]);
castle.push([0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,1,0,1,0,0]);
castle.push([0,0,0,0,0,0,0,1,0,1,0,0,0,1,0,0,0,1,0,1,0,0]);
castle.push([0,0,0,0,0,0,0,1,0,1,0,0,1,0,1,0,0,1,0,1,0,0]);
castle.push([0,0,0,0,0,0,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,0]);
castle.push([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]);
//
function createCircle(x:uint, y:uint, i:Boolean) {
cd = new b2CircleDef;
cd.radius = 30/p;
cd.density = 5;
cd.friction = .5;
cd.restitution = .5;
bd = new b2BodyDef;
bd.position.Set(x/p, y/p);
b = m_world.CreateDynamicBody(bd);
b.CreateShape(cd);
b.SetMassFromShapes();
b.ApplyImpulse(new b2Vec2(0, (Math.random() * 550) + 150), b.GetWorldCenter());
}
function createBox(x:uint, y:uint, sleeping:Boolean) {
bd = new b2BodyDef;
bd.position.Set(x/p, y/p);
bd.isSleeping = sleeping;
pd = new b2PolygonDef;
pd.SetAsBox(25/p, 25/p);
pd.density = 15;
pd.friction = .3;
b = m_world.CreateDynamicBody(bd);
b.CreateShape(pd);
b.SetMassFromShapes();
}
function createStaticBox(x:Number, y:Number, w:Number, h:Number) {
bd = new b2BodyDef;
bd.position.Set(x/p, y/p);
pd = new b2PolygonDef;
pd.SetAsBox((w/2)/p, (h/2)/p);
pd.friction = .5;
b = m_world.CreateStaticBody(bd);
b.CreateShape(pd);
b.SetMassFromShapes();
}
bd = new b2BodyDef;
bd.position.Set(0, 400/p);
pd.vertexCount = 3;
pd.vertices[0] = new b2Vec2(0, 0);
pd.vertices[1] = new b2Vec2(0, ((-Math.random() * 125) - 75)/p);
pd.vertices[2] = new b2Vec2(((Math.random() * 150) + 50)/p, 0);
pd.friction = 0;
b = m_world.CreateStaticBody(bd);
b.CreateShape(pd);
b.SetMassFromShapes();
function Update(e:Event):void {
m_world.Step(m_timeStep, m_iterations);
}
for (var wy = 0; wy < castle.length; wy++) {
for (var ex = 0; ex < castle[wy].length; ex++) {
switch (castle[wy][ex]) {
case 1 :
createBox(ex * 25, wy*50, true);
break;
case 2 :
createCircle(ex * 25, wy * 50, true);
break;
}
}
}
createStaticBox(0, 200, 25, 400);
createStaticBox(550, 200, 25, 400);
createStaticBox(275, 400, 550, 50);
var dbgDraw:b2DebugDraw = new b2DebugDraw();
dbgDraw.m_sprite = new Sprite();
dbgDraw.m_drawScale = 30;
dbgDraw.m_fillAlpha = 1;
dbgDraw.m_alpha = .5;
dbgDraw.m_lineThickness = 0;
dbgDraw.m_drawFlags = b2DebugDraw.e_shapeBit | b2DebugDraw.e_centerOfMassBit;
m_world.SetDebugDraw(dbgDraw);
addChild(dbgDraw.m_sprite);
addEventListener(Event.ENTER_FRAME, Update);