简体   繁体   中英

Confilicting behavior between dynamic and static bodies in Box2d

I have created Chain shapes (static bodies) all around the perimeter of the game screen whose dimensions are:

    (2f*camera.viewportWidth,2f*camera.viewportHeight).

Each chain shape has a density of 5f .

I've also created a Circle Shape (dynamic body) whose

    density = 0.5f 
    friction = 0.25f
    restitution =0.2f. 

In addition, I've created a polygon shape set as a box shape (dynamic body) which has the same density, restitution and friction as the circle shape.

The world's gravity is (0,-5.8f) .

All shapes render appropriately. However, the box shape just keeps falling right through the bottom chain shape which is located at the bottom of the screen. The circle shape doesn't go through, but the box shape does go through. I don't want this to happen. The size of the box shape is

    (1.96f*camera.viewportWidth, 1.96f*camera.viewportHeight). 

The position of this body(box shape) is set to

    (0.02f*camera.viewportWidth, 0.02f*camera.viewportHeight). 

I don't know why the box shape just keeps falling through and is not stopped by the bottom chain shape, just like the circle shape is stopped. Can anyone provide any insight?

Also, the reason I am trying to set up my box2d world like this is to eliminate some camera lagging movement when I use camera.translate to move around the world. My idea is to move the box shape by applying linear velocities to its body. Please any thoughts would be appreciated.

You probably did not set filtering on those fixtures. Take a look at the following example

    FixtureDef f = new FixtureDef();

    f.density = density;
    f.friction = friction;
    f.restitution = restitution;

now if you want Box2D to handle collisions for you, you should tell it which fiture will collide with which and you are doing it by assigning categoryBits and maskBits like this:

    f.filter.categoryBits = categoryBits; //who am I?
    f.filter.maskBits = maskBits;         //with who I will collide?

Bits should be short type and power of two - you cannot have two bits defined as same value. You can set many maskBits by using logical sum (' | ' operator).

    public static final short BIT_F1 = 2;
    public static final short BIT_F2 = 8;
    public static final short BIT_F3 = 32;

    FixtureDef f1, f2, f3;

    //creating f1, f2, f3...

    f1.filter.categoryBits = BIT_F1;
    f2.filter.categoryBits = BIT_F2;
    f3.filter.categoryBits = BIT_F3;

    f1.filter.maskBits = BIT_F1; //now every body having f1 fixture will collide with another f1
    f2.filter.maskBits = BIT_F1; //f2 will collide with f1
    f3.filter.maskBits = (short)(BIT_F1 | BIT_F2 | BIT_F3); //f3 collides with everything

Please notice that if you want fixture to avoid any collision you can it to be a sensor like:

    f.isSensor = sensor;

Here is link to official documentation although it is not very helpful to be honest.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM