In the Box2D manual (http://www.box2d.org/manual.html) you find a ContactListener Class. You then have to register this class via something like
world->SetContactListener(new MyContactListener());<- something like this, not tested!!!
If a new contact occurs you get informed in the Add () method of your contact listener, so you might implement a ofNotifyEvent here… or whatever you need.
Hi,
I would like to download the ofxBox2d from Vanderlins google code, but I dont find the way to download the addon. I just can read the code, but I suppose there is a easy way to download it, not copying one by one.
Thanks for the help
D.
hi guys i just download ofxbox2d and ascorbin’s example. when i compile there some errors say:
ofxbox2d\ofxbox2dpolygon.h(124) : error C2057: expected constant expression
ofxbox2d\ofxbox2dpolygon.h(124) : error C2466: cannot allocate an array of constant size 0
when i check the corresponding code, i do find this in ofxbox2dpolygon.h:
int count = (int)vertices.size();
b2Vec2 vertexArray[count];
as far as i know, C++ cannot allocate an array with a variable length.
does anyone come across the same error as me?
I’ve been playing around with this library, and it looks awesome. I want to implement the checkBounds, so objects are destroyed when out of sight. In the source code (odxBox2d.cpp) it says this in the update function:
// destroy the object if we are out of the bounds
if(bCheckBounds) {
/* need to add a bit of code to remove the bodies */
}
Anyone who knows how to destroy an object from that function?
And any way you want to share the code on GitHub so people can contribute?
hey. sorry to cross post but i have been playing with box2d and loving it, although i keep getting the following error. even when just running the example. everything seems to run fine but this floods the console. is there a way to fix this or suppress the error message? any help greatly appreciated:
I’m running Box2d, with some custom shapes in a vector.
I’m extending the class from the ofxBox2dRect object. Additionally, when I create them, I do the standard:
setPhysics()
setBounds()
setup()
and then push_back into my vector. This works great, essentially throwing them into my vector to keep track of and getting thrown into the box2D (through the setup method).
However, when I try to remove them, I crash my app OR they are removed visually, but still take up space in the box2D world (effectively causing invisible collisions).
I attempt to destroy the body manually much the way mentioned earlier in the thread.
box2d.getWorld()->DestroyBody(b);
and then remove from the vector using the erase method.
Any ideas on why this is happening, or if there’s something more specific I need to do to clean up the box2D world object?
looks like pt and amt are the other way around.
in the manual its documented as,
myBody->ApplyForce(myForce, myPoint)
it took me a few tests to work out what values ApplyForce likes…
and if you’re trying to add a force to a circle, for example - pushing a circle in a certain direction.
i found this to work the best,
b2Vec2 frc = b2Vec2( 1, -1 ); // arbitrary force value.
b2Vec2 pt = circle.body->GetWorldCenter(); // center point of body
circle.body->ApplyForce( frc, pt );
i had to work out how to raycast in box2d and have written some code for ofxBox2d to do this. ray casting is basically running a line in box2d and testing which shapes hit that line and at what points.
the method takes two points which describe the line.
maxHits is the maximum number of shapes to find hitting the line.
hitPoints is a vector for storing the ofPoint’s where the line and shapes make contact.
shapes is a vector which stores a pointer reference to the shapes the line makes contact with.
worked out a much nicer way of raycasting in box2d.
i moved the functionality into ofxBox2dBaseShape, so now all shapes can query if they collide with a ray and at what point.
the raycast method takes 2 points that describe the ray and a optional hitPoint (point of collision). it returns a bool to say if the ray has hit the shape or not.
been playing around with the contact listener example provided by ascorbin and created a tiny demo using balls which when collide change color on contact. was just curious to see how this would look as im thinking of including it in a project.
here you go,
[attachment=0:zxrhli5d]box2d_ball_contact_example.zip[/attachment:zxrhli5d]
hey, just added multi touch support to ofxBox2d for the iphone + ipad.
its basically just extending what the mouse is doing by creating a b2MouseJoint, although its now doing it for a multiple number of touches.
in ofxBox2d.h,
#define OF_MAX_TOUCH_JOINTS 5
i had to define a maximum number of touch joints to correctly track touch ids. 5 is the max num supported by ofxiPhone - if it changes in the future, this value will need to be updated.
in ofxBox2d.h,
b2MouseJoint* touchJoints[ OF_MAX_TOUCH_JOINTS ];
touchJoints array.
in ofxBox2d.h,
void grabShapeDown ( float x, float y, int id = -1 );
void grabShapeUp ( float x, float y, int id = -1 );
void grabShapeDragged ( float x, float y, int id = -1 );
added id variable to grab functions. -1 is the default and reserved for mouse. all iPhone touch ids are 0 to 4.
in ofxBox2d.cpp - init() function.
for( int i=0; i<OF_MAX_TOUCH_JOINTS; i++ )
touchJoints[ i ] = NULL;
hmmm…
just realised typing all this out is a pain in the arse
the only other changes are in,
grabShapeDown, grabShapeUp, grabShapeDragged methods.
its all in here.
[attachment=0:i3dkdsyg]ofxBox2d_multi_touch.zip[/attachment:i3dkdsyg]
what is the best way of adding code to ofxBox2d for future versions btw?