0
votes

I've had to completely revamp this question as I don't think I was explicit enough about my problem.

I'm attempting to learn the ropes of Box2D Web. I started having problems when I wanted to learn how to put multiple shapes in one rigid body (to form responsive concave bodies). One of the assumptions I made was that this kind of feature would only really be useful if I could change the positions of the shapes (so that I can be in control of what the overall rigid body looked like). An example would be creating an 'L' body with two rectangle shapes, one of which was positioned below and to-the-right of the first shape.

I've gotten that far in so-far-as I've found the SetAsOrientedBox method where you can pass the box its position in the 3rd argument (center).

All well and good. But when I tried to create two circle shapes in one rigid body, I found undesirable behaviour. My instinct was to use the SetLocalPosition method (found in the b2CircleShape class). This seems to work to an extent. In the debug draw, the body responds physically as it should do, but visually (within the debug) it doesn't seem to be drawing the shapes in their position. It simply draws the circle shapes at the centre position. I'm aware that this is probably a problem with Box2D's debug draw logic - but it seems strange to me that there is no online-patter regarding this issue. One would think that creating two circle shapes at different positions in the body's coordinate space would be a popular and well-documented phenomina. Clearly not.

Below is the code I'm using to create the bodies. Assume that the world has been passed to this scope effectively:

// first circle shape and def

var fix_def1 = new b2FixtureDef;

fix_def1.density = 1.0;
fix_def1.friction = 0.5;
fix_def1.restitution = .65;
fix_def1.bullet = false;

var shape1 = new b2CircleShape();

fix_def1.shape = shape1;
fix_def1.shape.SetLocalPosition(new b2Vec2(-.5, -.5));
fix_def1.shape.SetRadius(.3);

// second circle def and shape

var fix_def2 = new b2FixtureDef;

fix_def2.density = 1.0;
fix_def2.friction = 0.5;
fix_def2.restitution = .65;
fix_def2.bullet = false;

var shape2 = new b2CircleShape();

fix_def2.shape = shape2;
fix_def2.shape.SetLocalPosition(new b2Vec2(.5, .5));
fix_def2.shape.SetRadius(.3);

// creating the body

var body_def = new b2BodyDef();

body_def.type = b2Body.b2_dynamicBody;

body_def.position.Set(5, 1);

var b = world.CreateBody( body_def );

b.CreateFixture(fix_def1);
b.CreateFixture(fix_def2);

Please note that I'm using Box2D Web ( http://code.google.com/p/box2dweb/ ) with the HTML5 canvas.

1
I tried this with the current svn source code and it seemed to work fine. I just added a polygon as a ground shape to drop the two circles on. Are you using a recent version of Box2DWeb?iforce2d
Thanks for being the only person to comment... The version I'm using is here (kirven.co.uk/html5/ball/box2d/Box2dWeb-2.1.a.3.js). It's a bit of a mess but here is the URL (kirven.co.uk/html5/ball/test_environment.html). The main JS file is being loaded in (bootstrap.js) and the method I'm focusing on is the 'createBall' method. As stated, I'm just starting out with Box2D - so don't judge me on the unorganised code! Looking forward to an opinion. :-)shennan
How do you start with a very simple example. Lets say one that shows a simple world and one square. I am having trouble getting them to show the way that I would like them to. In fact I am not even certain that world has been created correctly. This seems like it should be easy but so far has been a real trouble maker.Doug Hauf

1 Answers

1
votes

It looks like you are not actually using the standard debug draw at all, but a function that you have written yourself - which explains the lack of online-patter about it (pastebin for posterity).

Take a look in the box2dweb source and look at these functions for a working reference:    b2World.prototype.DrawDebugData
   b2World.prototype.DrawShape
   b2DebugDraw.prototype.DrawSolidCircle

You can use the canvas context 'arc' function to avoid the need for calculating points with sin/cos and then drawing individual lines to make a circle. It also lets the browser use the most efficient way it knows of to render the curve, eg. hardware support on some browsers.

Since it seems like you want to do custom rendering, another pitfall to watch out for is the different call signatures for DrawCircle and DrawSolidCircle. The second of these takes a parameter for the axis direction, so if you mistakenly use the three parameter version Javascript will silently use the color parameter for the axis, leaving you with an undefined color parameter. Hours of fun!

DrawCircle(center, radius, color)
DrawSolidCircle(center, radius, axis, color)