Creating a wheel with 'prongs' - OrientedBox problem

Hello all,

I'm currently trying to use box2D to create a physics object which looks like a round cog with multiple 'prongs' sticking out at regular intervals (every 30 degrees to be precise) and having a bit of bother. If you're confused as to what I mean by this, I'm trying to create something that looks roughly like this:

http://images.andale.com/f2/115/106/356 ... py_cog.jpg

but with 12 'prongs' which are simply 6 rectangles attached to the central circle but 'sticking out' a little.

What I'm achieving, however, is a circle with a single rectangle (although I've checked and 6 are being created) directly through its centre. I believe all the rectangles are being created on top of each other, but for some reason the 'rotation' value is not being taken into consideration.

Here is the code I am using:

private function CreateCogBody(aWorld:b2World, aPosition:Point):b2Body
{
//Set the position & define the body
var cogBodyDef:b2BodyDef = new b2BodyDef();
cogBodyDef.position.Set(aPosition.x / GameProperties.ratio, aPosition.y / GameProperties.ratio);

var shapeDefs:Array = new Array();

//---------Create the centre circle---------
var cogShapeDef:b2CircleDef = new b2CircleDef();
cogShapeDef.radius = centreRadius / GameProperties.ratio;
cogShapeDef.density = 1;
cogShapeDef.friction = 0.5;
cogShapeDef.restitution = 0.3;
shapeDefs.push(cogShapeDef);

var prongShapeDef:b2PolygonDef = new b2PolygonDef();
for(var i:uint = 0; i < noOfProngs/2; i++)
{
prongShapeDef.SetAsOrientedBox((prongThickness/2)/GameProperties.ratio, ((centreRadius + prongLength))/GameProperties.ratio, new b2Vec2(0,0), (30*i)*(Math.PI/180));
prongShapeDef.density = 1;
prongShapeDef.friction = 0.5;
prongShapeDef.restitution = 0.3;
shapeDefs.push(prongShapeDef);
}

//Create the body
var cogBody:b2Body = aWorld.CreateBody(cogBodyDef);
//Create the shapes
for each(var aShape:b2ShapeDef in shapeDefs)
{
cogBody.CreateShape(aShape);
}
cogBody.SetMassFromShapes();

return cogBody;

}

Can anyone see why the rotation value is not working here? I am increasing it by 30 degrees (then converting to radians) each time and yet it seems to be doing nothing :/ Any help here is appreciated, if you feel that I am going about this all the wrong way and there's a much simpler way to do things then please just say so!

Thanks in advance.
On each for loop, you are redefining the "cogShapeDef" variable.

Try this (just moved your code around to create the body earlier and add shapes as they are defined:)

private function CreateCogBody(aWorld:b2World, aPosition:Point):b2Body
{
//Set the position & define the body
var cogBodyDef:b2BodyDef = new b2BodyDef();
cogBodyDef.position.Set(aPosition.x / GameProperties.ratio, aPosition.y / GameProperties.ratio);

//Create the body
var cogBody:b2Body = aWorld.CreateBody(cogBodyDef);

//var shapeDefs:Array = new Array();

//---------Create the centre circle---------
var cogShapeDef:b2CircleDef = new b2CircleDef();
cogShapeDef.radius = centreRadius / GameProperties.ratio;
cogShapeDef.density = 1;
cogShapeDef.friction = 0.5;
cogShapeDef.restitution = 0.3;
//shapeDefs.push(cogShapeDef);
cogBody.CreateShape(cogShapeDef);

var prongShapeDef:b2PolygonDef = new b2PolygonDef();
for(var i:uint = 0; i < noOfProngs/2; i++)
{
prongShapeDef.SetAsOrientedBox((prongThickness/2)/GameProperties.ratio, ((centreRadius + prongLength))/GameProperties.ratio, new b2Vec2(0,0), (30*i)*(Math.PI/180));
prongShapeDef.density = 1;
prongShapeDef.friction = 0.5;
prongShapeDef.restitution = 0.3;
//shapeDefs.push(prongShapeDef);
cogBody.CreateShape(prongShapeDef);
}


//Create the shapes
//for each(var aShape:b2ShapeDef in shapeDefs)
//{
//cogBody.CreateShape(aShape);
//}
cogBody.SetMassFromShapes();

return cogBody;

}
Yep that'll do it! Thank you so much, can't believe that was what was causing my problem - I've had this issue with arrays before but I keep forgetting that in some cases they just hold references. 😳 Silly me lol

I've been tearing my hair out over this one all afternoon, so you've saved me from going completely bald 😆

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