Box2D碰撞错误 [英] Box2D collisions error

查看:130
本文介绍了Box2D碰撞错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在libgx中的Java中使用box2d物理引擎,并且遇到了碰撞问题。
问题是身体停止移动,并且在平坦区域上会出现碰撞点。
我的工作方式是制作多个实体,每个实体代表一个区块,这些区块并排放置。
在这里查看碰撞点:

I am currently using box2d physics engine in java in libgx, and i am facing problems with collisions. The problem is that the body stops moving, and a collision point appears while on a flat area. The way im working is i am making multiple bodies, each representing a block, and those blocks are side-by-side. Look at the collision point here:

此处的物体不应碰撞,因为它们的Y坐标相等,并且它们的边也相等。

the body should not collide here for the Y coordinate of the bodies is equal, and their side is equal as well.

float PPM = 100;
float side = 45;
     for (float i = start; i < end; i++) {
        /**
         * perlin noise for terrain
         */
        def.type = BodyDef.BodyType.StaticBody;
        float place = Maths.map(noise.noise(off, off), 0, 1, -10, 50);
        if (place < 10) {
            place = 0;
        } else if (place < 20) {
            place = side;
        } else if (place < 30) {
            place = side * 2;
        } else if (place < 40) {
            place = side * 3;
        } else if (place <= 50) {
            place =  side * 4;
        }


        place += side / 2;

        float posx = (i * side);
        float posy = place;
        float size = side / 2f;
        def.position.set(posx / PPM, posy / PPM);
        b = world.createBody(def);
        shape.setAsBox(size / PPM, size / PPM);
        fdef.shape = shape;
        fdef.isSensor = false;
        b.createFixture(fdef);
        off += 0.01;
        toSetLast = posx + side;

        GrassBlock grass = new GrassBlock(b, new 
        Vector2(b.getPosition().x * PPM, b.getPosition().y * PPM));
        //dirt under grass
        for (float j = (place / side) - 1; j >= -1; j--) {
            posy = j * side;

            def.position.set(posx / PPM, posy / PPM);
            b = world.createBody(def);
            shape.setAsBox(size / PPM, size / PPM);
            fdef.isSensor = false;
            fdef.shape = shape;
            b.createFixture(fdef);
            DirtBlock dirt = new DirtBlock(b, new 
            Vector2(b.getPosition().x * PPM, b.getPosition().y * PPM));
            addBlock(dirt.getLocation(), dirt);
        }
        addBlock(grass.getLocation(), grass);

    }


推荐答案

我相信您正在体验的是Box2D库的一个已知缺点。

I believe what you're experiencing is a known short-coming of the Box2D library.

请参见什么可能导致Box2D突然停止?,该问题的另一种说法是。请参阅 Box2D C ++教程-幽灵顶点,以了解发生了什么以及可以做什么。确实可以缓解问题...

See What could cause a sudden stop in Box2D? for another write-up of this problem. See Box2D C++ tutorials - Ghost vertices for an explanation of what's going on and what you can do to mitigate the problem...

基本上,为纹理使用 ground 而不是多边形(或在多边形上方)使用链形),因此您的移动块实际上在链的顶部移动。假定您正在使用的java box2d实现的版本具有链状形状,并提供幽灵顶点支持。

Basically, use a chain-shape for the ground like texture instead of polygons (or over the polygons) so your moving block actually moves on top of the chain. That assumes the version of the java box2d implementation you're using has the chain shape in it that provides "ghost vertices" support.

详细信息请参见内幕。 ..

从用户的角度来看,链形提供了平滑效果,在引擎盖下,它是边缘形习惯了平滑。边缘形状通过两个特殊的额外顶点来实现(除了期望的两个边缘)。当库评估链形状和多边形(或圆形)之间的接触时,它通过识别可能发生接触的链段来做到这一点。然后,将其链段转换为边,并将其额外顶点设置为链中的相邻顶点。然后评估该平移的边与多边形(或圆形)之间的接触。这种情况会随着链条形状自动发生,并且链条形状中没有任何选项可用来获得这种行为。

While from the user's perspective chain shapes provide smoothing, under the hood, it's edge shapes that get used to do the smoothing. Edge shapes do this with two special extra vertices (in addition to the expected two of an edge). When the library is evaluating a contact between a chain shape and a polygon (or a circle), it does that by identifying the segment of the chain that the contact could be occurring with. It then translates that chain segment into an edge with its extra vertices set to the adjacent vertices in the chain. And then the contact is evaluated between that translated edge and the polygon (or circle). This occurs automatically with chain shapes and there's no option in chain shapes that is used to gain this behavior.

用户通过缝合可以达到与链条形状相同的结果将链的所有线段的边缘形状组合在一起,并将所有边缘的额外顶点设置为逻辑上相邻的顶点。但是,这要比链形使用更多的内存,并且要求用户编写更多的代码来执行(至少对于具有两个以上顶点的链而言)。

A user could achieve the same results as achieved with chain shapes by stitching together edge shapes for all the segments of the chain and setting all of the edges' extra vertices to the logically adjacent vertices. That uses significantly more memory than the chain shape however and requires the user to write more code to do (at least for chains having more than two vertices).

这篇关于Box2D碰撞错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆