Physicsjs屏幕包装



我目前有一点麻烦,使对象在我的世界包装。这种方法有点效果,但物体往往会被困在边界上。我的换行代码如下:

        // Wrap our position if we are outside of the world bounds
        if (this.state.pos.get(0) > 860) {
            this.state.pos.set(0, this.state.pos.get(1));
        }
        else if (this.state.pos.get(0) < 0) {
          this.state.pos.set(860, this.state.pos.get(1));
        }
        if (this.state.pos.get(1) > 640) {
          this.state.pos.set(this.state.pos.get(0), 0);
        }
        else if (this.state.pos.get(1) < 0) {
          this.state.pos.set(this.state.pos.get(0), 640);
        }

有更好的方法吗?我应该在物体的位置向量上使用平移而不是简单地设置它吗?

如果没有jsfiddle,这有点难以诊断,但这可能是由于this.state.old.pos没有设置太。如果设置了位置(only),那么速度将被计算为当前位置与之前位置之间的差(根据verlet积分)。在这种情况下,你隐含地给了物体一个巨大的速度。

我建议增加/减少一个金额,而不是设置,然后你可以对旧位置做同样的事情。

下面是一个工作示例:http://labs.minutelabs.io/Relativistic-Space-Sheep/使用相关代码行:https://github.com/minutelabsio/Relativistic-Space-Sheep/blob/master/library/js/mediators/boilerplate.js#L743

相关内容

最新更新