ofxBox2dRevoluteJoint

Hello all,
big thanks to Vanderlin for the great box2d wrapper. below is a class I added because I needed revolute joints as well as distance joints for something I was working on. thought it might be useful for others.
cheers,
nay.

  
  
#pragma once  
//based on Todd Vanderlin's ofxBox2dJoint class  
  
class ofxBox2dRevoluteJoint {  
	  
public:  
	  
	b2World * world;  
	b2Joint * joint;  
	bool alive;  
	b2RevoluteJointDef jd;  
	  
	ofxBox2dRevoluteJoint() {  
		world = NULL;  
		alive = false;  
	}  
	  
	void setWorld(b2World * w) {  
		if(w == NULL) {  
			ofLog(OF_LOG_NOTICE, "- box2d world needed -");	  
			return;  
		}  
		world = w;  
		alive = true;  
	}  
	  
	void destroyJoint() {  
		world->DestroyJoint(joint);  
		joint = NULL;  
	}  
	  
	void addJoint(b2Body * a, b2Body * b, float stiffness=3.0f, float damping=0.6f, int Xoffset=0, int Yoffset=0 ) {  
		  
		if(world == NULL) {  
			ofLog(OF_LOG_NOTICE, "- no joint added - box2d world needed -");	  
			return;  
		}  
  
		b2Vec2 anchor = a->GetWorldCenter();  
		jd.Initialize(a, b, anchor);  
		//need to override anchor set in Initialize() if you don't want the anchor to be in b2Body a's center  
		//(due to reasons I can't figure out!)  
		jd.localAnchor1.Set(Xoffset / OFX_BOX2D_SCALE, Yoffset / OFX_BOX2D_SCALE);  
		  
		jd.collideConnected = false;  
		jd.lowerAngle     = -0.25 * TWO_PI;  
		jd.upperAngle     = 0.25 * TWO_PI;  
		jd.enableLimit    = true;  
		jd.maxMotorTorque = 10.0;  
		jd.motorSpeed     = 0.0;  
		jd.enableMotor    = true;  
		  
		joint = world->CreateJoint(&jd);  
	}  
	  
	void draw(int alpha=200) {  
		  
		if(!alive) return;  
		  
		ofEnableAlphaBlending();  
		ofSetColor(255, 0, 255, alpha);  
		  
		b2Vec2 p1 = joint->GetAnchor1();  
		b2Vec2 p2 = joint->GetAnchor2();  
		  
		p1 *= OFX_BOX2D_SCALE;  
		p2 *= OFX_BOX2D_SCALE;  
		ofLine(p1.x, p1.y, p2.x, p2.y);	     
		ofDisableAlphaBlending();  
	}  
};  
  

Thanks so much for this.
I’m currently making a big project with box2d and this is really going to help me.
cheers, :o
Lucas

Hell Nay.

Do you think you could post an example in which you are using your special class?
I just starting to use box2d and need a little help getting things off the ground.

Thanks a bunch,
Stephan.

Cheers to nay for posting this, however the following worked better for me (of 0.8.4):

b2Vec2 anchor = b2Vec2(_xOffset / OFX_BOX2D_SCALE + _a->GetWorldCenter().x, _yOffset / OFX_BOX2D_SCALE + _a->GetWorldCenter().y);
jd.Initialize(_a, _b, anchor);

For some reason, nay’s original solution for changing the anchor point resulted in the joint length inexplicably resizing after the first world step.