[英]How do you iterate over the list of bodies from Box2D in emscripten?
我在这里使用Box2D的emscripten端口: https : //github.com/kripken/box2d.js
效果很好,但与Emscripten交互时遇到了一些麻烦。
具体来说,我在这样的循环中执行模型显示同步:
function step() {
world.Step(1/60);
var body = this.world.GetBodyList();
while(body != null) {
readGeometry(body, body.data);
body = body.GetNext();
}
}
...但这似乎不起作用。 尽管C ++代码在body对象的链接列表的末尾返回NULL,但body.GetNext()(cpp中的返回类型为b2Body *)决不是原生javascript null。
我也尝试过:
body != Box2D.NULL
但是,那也不是真的。 我猜Emscripten返回一个包装的指针,我必须对其进行一些特定的操作以测试是否为空。
检查返回的对象,我可以看到其中的空值的“指针”值为零,并且可以使它适用于:
function step() {
world.Step(1/60);
var body = this.world.GetBodyList();
while(body.a != 0) { // <--------------- This hack
readGeometry(body, body.data);
body = body.GetNext();
}
}
因此,显然可以测试NULL值,但是我找不到有关如何执行此操作的文档。
尝试这个
function step() {
world.Step(1/60);
var body = this.world.GetBodyList();
while(Box2D.getPointer(body)) { // <-- will equal 0 for a Box2D.NULL object
readGeometry(body, body.data);
body = body.GetNext();
}
}
我知道这个问题确实很老,但是最近我遇到了这个问题,并在github上找到了解决方案。
接受的答案不起作用,但是这样做:
var next = World.m_bodyList;
var current;
while (next != null) {
current = next; next = next.m_next;
if(current.m_userData){
var current_body = {};
current_body.x = current.m_xf.position.x;
current_body.y = current.m_xf.position.y
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.