Box2d + oF006


I have been using Box2d (Todd’s version found in this post) and a snapshot of oF [from early January]. So far everything seems to run as expected.

Today I moved the same project to oF006 [current version from the svn]. The project is compiling fine, and works. But the ‘scaling’ from Box2D is not reacting the same. All the bodies are now smaller, but their properties stayed the same.

Any idea to what I should look ?


Found why. It’s due to a change in the method ofEllipse. The Width and Height of the created ellipse are multiplied by 0.5 when the vertex array is created.

Everything is good now :slight_smile:

ahh yeah the 006 version the more correct as previously the ellipse radius was base don width and height - where is it should have been half width and half height.

Now if you do:

	ofSetColor(255, 0, 0);  
	ofEllipse(ofGetWidth()/2, ofGetHeight()/2, ofGetWidth(), ofGetHeight());  

you get an ellipse which covers the whole screen.

Were can i download Box2D for 0.06??

thx :slight_smile:

I get the lib from there. what you have to do is just build the library file ( it’s has vs,cb,osx version).

It works for me.

I have a better version up on google code grab it here:

It has a hacked version of box2d so that you can draw lines and complex polygons. I will try to make an example and put it up there.


cool, ive been following your work with box2D and its very interesting.
an example would be sweet.

I have a better version up on google code grab it here:

It has a hacked version of box2d so that you can draw lines and complex polygons. I will try to make an example and put it up there.


hi vanderlin do you have examples for yor other projects ofxTimer and ofxFileStream

Thx really good Job


here is a simple example

looks good todd… and thanks for the example ascorbin !

can I ask if it’s cool to use .cpp and .h files for the core parts of ofxBox2d ?

I know that it’s quicker to program just one time, like java in an h file.

but the reason is simple – it’s much, much easier to read and since there’s no documentation the “h” file kind of works for an instruction manual. also, it’s potentially a quicker compile, since there is less copying of text around (h files should be thin for that reason alone).

at one point I found a crazy script that does this – splits an h file definition into .h and c++. I can’t seem to find it, but I remember trying to get theo into it too since he loves h file programing :slight_smile:

take care!

I agree it does look better in a .h and .cpp I can make the change. heheheh

He Vanderlin!

Thanks a lot for this!

i found some things:
in - addForce(ofPoint pt, ofPoint amt)
the OFX_BOX2D_SCALE is missing and if body is null app crashes

getAngle is nice to have
float getAngle() {
float a;
if(body != NULL) {
a = body->GetAngle();
return a;

greetings ascorbin

Ahh i just fixed it. I also posted a new example with joints forces and custom particles. Its in the SVN grab it there.…-e%3Dclosed

cool ! I’ve been experimenting and it seems that keeping the body def around (instead of just using it in setup) means that various aspects of an object can be adjusted. for example, I wanted to make a joint that I can expand and contract:

b2World *			world;  
	b2Joint *			joint;  
	b2DistanceJointDef	jd;   // [zl] keeping the body def around...  
	ofxBox2dJoint() {  
		world = NULL;  
		joint = NULL;  
	void setWorld(b2World * w) {  
		if(w == NULL) {  
			ofLog(OF_LOG_NOTICE, "- box2d world needed -");	  
		world = w;  
	void setLength(float length){  
		jd.length = length / OFX_BOX2D_SCALE;  
		joint = world->CreateJoint(&jd);  
	float getLength(){  
		return jd.length * OFX_BOX2D_SCALE;  

there doesn’t seem to be a better way to alter an object like a joint besides removing it and re-adding it. for shapes, I’ve foind a way to remove the shape from the body and add a new shape, which means I can make rectangles that expand and contract.

take care!

edit - ps, the new example is great! I learned alot…

here are some more intersting features ie a contact listener, which gives you collision point informations:

class MyContactListener : public b2ContactListener  
		void Add(const b2ContactPoint* point)  
			//cout << "contact at: "+ofToString(p.x, 2) + " "+ofToString(p.y, 2) 			<< endl;  
		void Persist(const b2ContactPoint* point)  
			//cout << "persist: " 			<< endl;  
		void Remove(const b2ContactPoint* point)  
			//cout << "remove: " 			<< endl;  
		void Result(const b2ContactResult* point)  
			//cout << "result: " 			<< endl;  

in the .h-File:

MyContactListener	contacter;  

integrate in .cpp with:

myApp->welt.getWorld() -> SetContactListener(&contacter);  

b2ContactPoint has:

struct b2ContactPoint  
	b2Shape* shape1;		///< the first shape  
	b2Shape* shape2;		///< the second shape  
	b2Vec2 position;		///< position in world coordinates  
	b2Vec2 velocity;		///< velocity of point on body2 relative to point on body1 (pre-solver)  
	b2Vec2 normal;			///< points from shape1 to shape2  
	float32 separation;		///< the separation is negative when shapes are touching  
	float32 friction;		///< the combined friction coefficient  
	float32 restitution;	///< the combined restitution coefficient  
	b2ContactID id;			///< the contact id identifies the features in contact  

greetings ascorbin

another thing. you can attach a ‘user-data’ class to a body or shape with custom functions.

bakterium* myActor = new bakterium();  
myActor -> setType("bak1");  
zelle ->body->SetUserData(myActor);  

class bakterium {  
	string type;  
	bool alive;  
	bakterium() {  
		type = "";  
		alive = true;  
	void setType(string t) {  
		type = t;  
	bool isAlive() {  
		return alive;  
	string getType() {  
		return type;  

greetings ascorbin

Ascorbin hi and thx for your code…

I can obtain the speed of the objets when they have a colliosion?

thx and take care…

one more function for vanderlins ofxbox2dJoint.h:

float32 getReactionForce() {  
	b2Vec2 F = joint->GetReactionForce(1.0f);  
	return =  F.LengthSquared();  

this is cool, because you can define a maxForce for joints and then they ‘crack’. I had fun with that. I mean its all here -> with docs and forum and wiki and stuff, but not so ‘of-ish’ so i thought it could be interesting for you.

greetings ascorbin

@ zach I know its a really annoying thing about box2d drives me crazy. I wanted to just change the radius of a shape and it was a major hack. I will add that to the baseShape class and post to the svn.

@ ascorbin
That looks great ill put it in.

other useful things:

a) having non ofGetWidth() / ofGetHeight() for the default ofxBox2d allocation (I was doing box2d into a pbuffer, for example) and altering based on that the AABB points.

b) revoluteJoints are nuts, it was fun to play with. maybe a base joint class?

c) there seemed to be missing some “dead = false” in the start up of objects, to that killing them and restarting meant dead was still true.

d) growing shapes is kind of tricky too, because if you do it close to the edge you can get a nasty crazy crash – I had to play with the AABB stuff a bit.

e) i’ve found simpler then growing shapes, just attaching a new larger object to a box with a stiff revoluteJoint. it’s ghetto but seemed to work

I’ll try to keep track of changes as I go and merge back in.

take care!!