【问题标题】:Why there is a phantom contact points in my Box2d libgdx game为什么我的 Box2d libgdx 游戏中有幻像接触点
【发布时间】:2020-03-13 16:43:33
【问题描述】:

我正在使用libgdxbox2d 编写游戏。当一个物体接触另一个物体时,经常会出现幻像接触点。这就是我绘制接触点的方式:

private void renderContactPoints() {
    for (Contact contact : world.getContactList())
        for (Vector2 point : contact.getWorldManifold().getPoints())
            renderer.getBatch().draw(tex, point.x * PPM, point.y * PPM, 20, 20);

}

问题截图

【问题讨论】:

  • 幻象接触点是哪些接触点?

标签: java libgdx box2d


【解决方案1】:

为了提高性能,Box2DlibGDX 重复使用数组来避免重新分配,因此你不能迭代 getContactListgetPoints 的完整结果,因为你会得到结果太多。

而是先查询项目数:

private void renderContactPoints() {
    for(int i = 0; i < world.getContactCount(); ++i) {
        Contact contact = world.getContactList().get(i);
        for (int j = 0; j < contact.getWorldManifold().getNumberOfContactPoints(); ++j) {
            Vector2 point = contact.getWorldManifold().getPoints()[j];
            renderer.getBatch().draw(tex, point.x * PPM, point.y * PPM, 20, 20);
        }
    }
}    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    相关资源
    最近更新 更多