We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpOther Libraries › JBox2D and collisions
Page Index Toggle Pages: 1
JBox2D and collisions (Read 1248 times)
JBox2D and collisions
Feb 16th, 2010, 3:27pm
 
Is there a way to turn off collisions on specific objects while keeping the other physics simulations, ie velocity, gravity, and joint constraints?

I'm making an avatar with arms, and in order to have its arms bend, i had to create an "elbow" body to connect to the main body and hand with 2 distance joints. I couldn't use a revolute joint because it does not allow both ends of the joint to rotate freely. So it would be nice to turn off collisions on the hidden elbow body so it doesn't get stuck behind things, and everything gets weird.

I thought about using the revolute joint the other way, so the non-rotating side was on the hand, but the hand is not actually a physics body, it is controlled with the mouse, and I can't have the hand colliding with anything either.

Any thoughts would be appreciated.

Re: JBox2D and collisions
Reply #1 - Feb 16th, 2010, 3:49pm
 
So I just answered my own question, and I figured I'd post it in case someone else has this problem.

You have to define a groupIndex for every ShapeDef you want to *not* collide with each other and set it to a negative number. ie:

Code:
BodyDef bd = new BodyDef();
bd.position = box2d.screenToWorld(x,y);
Body body = box2d.createBody(bd);

PolygonDef sd = new PolygonDef();
sd.setAsBox(box2d.scaleScreenToWorld(w/2), box2d.scaleScreenToWorld(h/2));
sd.density = 1.0f;
sd.friction = 0.3f;
sd.restitution = 0.5f;

// Here is where you set the groupIndex
sd.filter.groupIndex = -1;

body.createShape(sd);
body.setMassFromShapes();


You must do this for every ShapeDef that you define so all bodies that you want to never collide are in the same negative-numbered groupIndex.

By the way, I am using PBox2D to interface with JBox2D.
Re: JBox2D and collisions
Reply #2 - Feb 16th, 2010, 8:53pm
 
Alternatively, categories & masks can be used.

When defining a ShapeDef or its various subclasses (PolygonDef, CircleDef, etc.) you can define a "categoryBit" to identify a group of shapes with the "maskBits" field.

If you want no collisions on an object whatsoever, set its maskBits to 0x0000; the default, aka "collide with everything," is 0xffff:

Code:
// No collisions
shapeDef.filter.maskBits = 0x0000;

// Collide with all (except for those with collisions turned off)
shapeDef.filter.maskBits = 0xffff;


If you want a specific group to collide only with another specific group, define each groups' categoryBits separately, then point each groups' maskBits to the others' category:

Code:
// Categories are defined for each shape in each group
group1shapeDef.filter.categoryBits = 0x0001;
group2shapeDef.filter.categoryBits = 0x0002;

// This will only allow the two groups to collide with each other and nothing else
group1shapeDef.filter.maskBits = 0x0002;
group2shapeDef.filter.maskBits = 0x0001;

// This will allow the two groups to collide with everything EXCEPT the other group
group1shapeDef.filter.maskBits = 0xffff & ~0x0002;
group2shapeDef.filter.maskBits = 0xffff & ~0x0001;


I hope this helps other people understand this better. The original Box2D manual doesn't explain it very well. Thanks to this post at the box2d forums for helping me understand it:

http://www.box2d.org/forum/viewtopic.php?f=3&t=197
Re: JBox2D and collisions
Reply #3 - Feb 17th, 2010, 2:14am
 
Thanks for sharing this, as it highlights advanced and, as you said, relatively obscure points of the library.
Page Index Toggle Pages: 1