box2d polygon shape

hello forum.

i am trying out the box2d addon and it is great.

i would like to enclose a bunch of circles inside a big shape.
currently i can use the “draw a line strip” option do draw such a shape.

but how can i make a curve shape like the one in vectorGraphicExample.

output.beginShape();

output.curveVertex(rx, ry);

output.endShape();

one work around could be to somehow extra all the vertex points from this shape and feed it in to the lineStrip of box2d.

thanks for any advice,
stephan.

Hi

I am not sure I understood you correctly, but you want the outer shape to be curved and the inner circles to react physically to it, so you’d need a curved box2d shape?

I don’t think box2d supports curves but only polygonal shapes with straight lines. Because they are easier to calculate and therefore more CPU friendly. But you could try use a simplified polygonal shape for the calculations but not draw it. Instead you draw the complex version with beziers or ofCurvedVertex at the same position.

hope that makes sense…
greets
ph

Thanks for your input.

I thought of that too but I can’t figure out how to convert a curvedVertex in to a simplified polygon shape.
If i could get points on the curve in equal intervals, then i could used those to draw short straight lines to make the shape i want.

also, i am not sure how to use the box2d draw polygon method.

thanks for any help,
stephan.

I don’t know how you calculate your curve. But if it is a bezier you could get points on it with this function:

  
  
float bezierPoint(float x0, float x1, float x2, float x3, float t) {  
		float   ax, bx, cx;  
		float   ay, by, cy;  
		float   t2, t3;  
		float   x;  
		  
		// polynomial coefficients  
		cx = 3.0f * (x1 - x0);  
		bx = 3.0f * (x2 - x1) - cx;  
		ax = x3 - x0 - cx - bx;  
		  
		t2 = t * t;  
		t3 = t2 * t;  
		x = (ax * t3) + (bx * t2) + (cx * t) + x0;  
		  
		return x;  
	}  
  

x0 = x or y coordinate of first point
x1 = x or y coordinate of first handler
x2 = x or y coordinate of second handler
x3 = x or y coordinate of second point
t = interpolation value between 0 and 1

With a curved vertex I don’t have any code ready…

But I’m not shure if you need that much of a resolution for you lowpoly box2d polygon. I’d try it with the x/y coordinates you already have of your curve first?

I used the box2dPolygon the following (untested):

  
  
vector<ofPoint> points;  
¨  
//add some points here  
  
ofxBox2dPolygon* poly = new ofxBox2dPolygon;  
for(int j=0;j<points.size();j++){  
	poly>addVertex(points[j]);  
}  
poly->setPhysics(3., .5, .3);  
poly->createShape(box2d.getWorld(), ofRandom(0, ofGetWidth()), ofRandom(0, ofGetHeight()));  
  
//and then anywhere  
poly->draw();  
  

But you could also override the draw method of a poly by extending ofxBox2dPolygon to have it draw a curve instead of edgy polgons (untested)

  
  
class customPoly: public ofxBox2dPolygon {  
public:  
	ofxCvBlob blob;  
	ofPoint pos;  
	  
	public void draw(){  
		if(body != NULL ) {  
            ofBeginShape();  
            for(int i=0; i<vertices.size(); i++) {              
                ofCurveVertex(vertices[i].x, vertices[i].y);  
		   }  
		   ofEndShape();  
        }  
	}  
};  
  
//somwehere in setup or so...  
  
ofxBox2dPolygon* poly = new customPoly;  
for(int j=0;j<points.size();j++){  
	poly>addVertex(points[j]);  
}  
poly->setPhysics(3., .5, .3);  
poly->createShape(box2d.getWorld(), ofRandom(0, ofGetWidth()), ofRandom(0, ofGetHeight()));  
  
//and then anywhere  
poly->draw();  
  

greets
ph

Hello again.

So i tried to implement your example code.

i always get an error “EXC_BAD_ACCESS”

here in b2Body.h

  
inline b2Shape* b2Body::GetShapeList()  
{  
	return m_shapeList;  
}  

if i comment out polyShape.draw(); i don’t get the error, but also not polygon :slight_smile:

here is my project folder:
http://www.lozano-hemmer.com/ss/ofBox2d-poly.zip

???

thanks,
stephan.

Hi

Your example works on my system. But it looks like I may have another version of ofxBox2d. I hade to change line 49 in testApp.cpp to

  
polyShape.createShape(box2d.getWorld(), 0.0f, 0.0f);  

instead of init(…)

Did you download the newest version of ofxBox2d vis svn over at google code ( http://code.google.com/p/vanderlin/source/checkout ) ?

cheers.ph

Hello Philip.

Yes, true i did not use the newest version of box2d.
Now it draws the polygon as a filled shape.

I guess what i am wondering now is how can i use this polygon to contain the circles.
I would like the polygon to be the boundaries of the world. Everything has to stay inside.

Similar to the bos2d example, where you can draw a lineShape.
But with the line shape i notice objects fall through if the surface is concave.

Habe ich eigentlich schon danke gesagt? :slight_smile:

cheers,
stephan.

update:

ok. i figured it out.

in the example i can draw lineshapes. those lineshapes can be used to keep objects (circles) contained.
but i had to make sure i am drawing this lineshape counter clock wise, otherwise objects will fall right through the lineshape.

thanks again.
stephan