1
\$\begingroup\$

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?

Here's an illustration I've made:
enter image description here

  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.
\$\endgroup\$
  • \$\begingroup\$ the walls would stand still while this is happening ? or would they curve to hug the ball ? \$\endgroup\$ – dimitris93 Jan 23 '15 at 17:21
  • \$\begingroup\$ 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" \$\endgroup\$ – BWG Jan 23 '15 at 17:27
  • \$\begingroup\$ @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. \$\endgroup\$ – src091 Jan 23 '15 at 17:35
  • \$\begingroup\$ @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 :) \$\endgroup\$ – src091 Jan 23 '15 at 17:35
  • \$\begingroup\$ @Anton I'll make an answer. \$\endgroup\$ – BWG Jan 23 '15 at 17:36
1
\$\begingroup\$

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 {

    float getRadius() {
        //...
    }

    vec2 getPosition() {
        //...
    }

    @Override //override the base function
    vec2 getExertedForce(vec2 point) {
        if(distance(point, getPosition() > getRadius()) {
            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.

\$\endgroup\$
  • \$\begingroup\$ 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)? \$\endgroup\$ – src091 Jan 23 '15 at 19:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.