altering the center of mass?

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.

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);
It sounds like maybe your castle isn't built to realistically stand up. If the turret bits are so far to the side so that their center of mass is over the edge, coudn't you put a heavy block on their other end to stop them from going over?
http://www.youtube.com/watch?v=b6HvIDt1VSU
Pause the video right at the very beginning and note the outside edges of the pyramid, you will see that this kind of counterweighting is no problem.
the turrets' middles are right on the edge of the tower. wouldn't that give itself a sort of equilibrium?
hm... if I was designing a building that I really wanted not to fall down, I don't think I would put the CoM of the outside wall blocks right on the edge of the ones below, sounds like a recipe for instability. Perhaps you could move them just a little inwards? It shouldn't take too much, just as long as their CoM is not too close to that precarious position.
I imagine that a real turret isn't balanced on a stack of bricks, its nailed, welded, or attached in some manner. You might want to consider jointing a protruding turret to some bricks around the castle wall.
I believe, by using joint limits, you can set the joints to break when and if the castle collapses, just like a real turret's welds break.

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