【问题标题】:LibGDX Box2D RayCast fatal errorLibGDX Box2D RayCast 致命错误
【发布时间】:2015-05-22 15:45:28
【问题描述】:

我正在处理 raycast,但我遇到了一个大问题。这是错误消息。

#
# A fatal error has been detected by the Java Runtime Environment:
#
# SIGSEGV (0xb) at pc=0x00007f2a67b5345b, pid=19347, tid=139820316944128
#
# JRE version: OpenJDK Runtime Environment (7.0_79-b14) (build 1.7.0_79-b14)
# Java VM: OpenJDK 64-Bit Server VM (24.79-b02 mixed mode linux-amd64 compressed oops)
# Derivative: IcedTea 2.5.5
# Distribution: Custom build (Wed Apr 15 12:39:15 UTC 2015)
# Problematic frame:
# C [libgdx-box2d64.so+0x3a45b] void b2DynamicTree::RayCast<b2WorldRayCastWrapper>(b2WorldRayCastWrapper*, b2RayCastInput const&) const+0x3ab
#
# Failed to write core dump. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again

你可以在这里查看日志文件http://paste.ubuntu.com/11287130/ 这是我的回调和 splitObj。

private final RayCastCallback callback = new RayCastCallback() {
        @Override
        public float reportRayFixture(Fixture fixture, Vector2 point, Vector2 normal, float fraction) {
            //We can just slice the field and field is ChainShape
        if (fixture.getShape() instanceof ChainShape) {

            Body body = fixture.getBody();
            List<Vector2> pointVec = rayCastMap.get(body);

            if (pointVec == null) {
                pointVec = new ArrayList<Vector2>();
                rayCastMap.put(body, pointVec);
            }

            if (!pointVec.isEmpty() && !pointVec.get(0).equals(point)) {
                pointVec.add(point.cpy());
                splitObj(body, pointVec);
            } else {
                pointVec.add(point.cpy());
            }
        }
        return 1;
    }
};
private void splitObj(Body sliceBody, List<Vector2> splitedPoints) {

        Vector2 a = splitedPoints.get(0);
        Vector2 b = splitedPoints.get(1);

        Array<Vector2> shape1Vertices = new Array<Vector2>();
        shape1Vertices.addAll(a, b);
        Array<Vector2> shape2Vertices = new Array<Vector2>();
        shape2Vertices.addAll(a, b);

        for (Vector2 vec : field.getVertices()) {
            float determinant = det(a, b, vec);
            if (determinant > 0) {
                if (!shape1Vertices.contains(vec, false))
                    shape1Vertices.add(vec);

            } else if (determinant < 0) {
                if (!shape2Vertices.contains(vec, false))
                    shape2Vertices.add(vec);
            }
        }

        GeometryUtils.arrangeClockwise(shape1Vertices);
        GeometryUtils.arrangeClockwise(shape2Vertices);

        FloatArray shape1 = arrayToFloatArray(shape1Vertices);
        FloatArray shape2 = arrayToFloatArray(shape2Vertices);
        FloatArray newShape;

        float shape1Area = calculateArea(shape1);
        float shape2Area = calculateArea(shape2);

        box2dWorld.destroyBody(sliceBody);
        ball.box2dBall.setActive(false);

        float splicedArea;

        if (det(a, b, ball.getPosition()) > 0) {
            splicedArea = shape2Area * 100 / (shape1Area + shape2Area);
            newShape = shape1;
        } else {
            splicedArea = shape1Area * 100 / (shape1Area + shape2Area);
            newShape = shape2;
        }

        field.setIsSliced(true);
        // setting the properties of the two newly created shapes
        BodyDef bodyDef = new BodyDef();
        bodyDef.type = BodyDef.BodyType.StaticBody;
        bodyDef.position.set(sliceBody.getPosition());
        FixtureDef fixtureDef = new FixtureDef();

        // creating the first shape
        PolygonShape polygonShape = new PolygonShape();
        polygonShape.set(newShape.toArray());
        fixtureDef.shape = polygonShape;

        sliceBody = box2dWorld.createBody(bodyDef);
        sliceBody.createFixture(fixtureDef);
}

我想切片一个对象(ChainShape),并且有一个可以在这个chainshape内移动的球。我听touchDown,touchDragged和touchUp输入。

touchDown 获取 raycast 的第一个点, touchDragged 获取该行的第二个点, touchUp 像这样调用 raycast。

box2dWorld.rayCast(callback, p2, p1);

当我调用它时,有时它会给出这个致命错误。我的问题是什么?我能做什么?

【问题讨论】:

    标签: java libgdx box2d game-physics


    【解决方案1】:

    通常,如果您在使用 box2d 时看到 SIGSEGV Java Runtime Environment 错误,则它与使用已损坏的主体/关节等有关。

    看看你的代码在这里你破坏了一个身体:

    box2dWorld.destroyBody(sliceBody);
    

    然后在几行之后,您尝试再次使用已经破坏的主体(!):

    bodyDef.position.set(sliceBody.getPosition());
    

    你不能再使用被破坏的尸体了!只有在确定不再使用尸体后,才能尝试销毁它们。

    【讨论】:

    • 是的,你是对的,但我认为我的问题不是因为它,我已将 bodyDef.position.set(sliceBody.getPosition()) 更改为 bodyDef.position.set(0, 0),但我仍然有同样的错误。
    • 如果你注释掉box2dWorld.destroyBody(sliceBody);这行,测试是否仍然发生错误这可能是你不能在回调中销毁一个body的原因,因为你只能在世界当前不做的时候删除body它的时间步长。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-30
    • 1970-01-01
    • 1970-01-01
    • 2014-07-02
    • 2013-07-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多