# Permeable objects in Box2d

In Box2d, can I make a static object to be permeable by dynamic objects? Can I simulate something like water in a way that it lets rigid bodies to dive into it but at the same time pushes them off over time?

1. Three circles in steady state rest inside my "game world" with some gravity.
2. I push the top circle down, two circles on bottom start sinking into walls since wall objects are permeable.
3. Walls slowly push circles back into the world once the top circle stopped affecting them.
• the walls would stand still while this is happening ? or would they curve to hug the ball ? – dimitris93 Jan 23 '15 at 17:21
• I don't know if this is possible with plain Box2D, but it'd be trivial to make a custom class to do this. Just have a getExertedForce(point), which you apply to every object in the scene, for every object which is "permeable" – BWG Jan 23 '15 at 17:27
• @Shiro I'm sorry I don't quite understand what do you mean by "curve to hug the ball". The walls are supposed to be static objects and stand still, yes, just be permeable to some extent and push back all the objects that dive into them. – src091 Jan 23 '15 at 17:35
• @BWG Could you please elaborate on this. I don't quite understand how to use your idea and googling getExertedForce yields just two unrelated results :) – src091 Jan 23 '15 at 17:35
• @Anton I'll make an answer. – BWG Jan 23 '15 at 17:36

Imagine that you have a class that represents a permeable object (in pseudocode):

class permeableObject {
b2RigidBody body; //this holds the actual box2D rigid body

//this is a virtual function; it will be override by derived classes
vec2 getExertedForce(vec2 point);

//...
//functions such as constructor, setBody, etc
}


Then, you implement custom derived classes that have specific functions:

class permeableCircle extends permeableObject {

//...
}

vec2 getPosition() {
//...
}

@Override //override the base function
vec2 getExertedForce(vec2 point) {
return vec2(0,0); //no force is exerted, the object isn't inside the circle
} else {
//get a force that pops the object out of the circle
return (point - getPosition()) / getRadius();
}
}
}


So this is a super basic example, and I haven't done Java in a long time so forgive any syntax errors. Basically, your "permeable objects" have a function which returns the "bouyancy" vector at a specified point. In my circle example, if an object falls into a circle, for example, it will get pushed out if you apply the force returned by getExertedForce.

Note that you will probably want to use box2D's built in collision detection, not your own, AND that box2D has sensor objects (I think), which do not collide with objects, only detects collision.

• So basically an idea is that I use sensors on walls, once I get collision events it's up to me generally how to react to them (change circle velocity so it starts to be pushed off the wall for example)? – src091 Jan 23 '15 at 19:04