Body b;
while ((b=box2d.physics.PhysicssWorld.world.getBodyList().getNext())!=null) {
Shape shape;
while ((shape=b.getShapeList().getNext())!=null) {
Log.e("name",""+b.getUserData().toString()+" "+shape+" ");
opengl.saveMatrix();
Meshes.select(b.getUserData().toString())
.translate((b.getPosition().x)*RATIO, (b.getPosition().y)*RATIO)
.rotate((int) ((int) b.getAngle()* (180 / Math.PI)), 0, 0, 1)
.draw(shape, 1,1,1);
opengl.loadMatrix();
}
}
Run Code Online (Sandbox Code Playgroud)
我想得到我的身体的形状,但我什么都得不到,只有空...为什么?
永远不要运行这一行:Log.e("name",""+ b.getUserData().toString()+""+ shape +"");
所以shape = b.getShapeList().getNext())总是为null ...
我自己刚刚开始使用Box2D.据我了解图书馆,获取身体形状的主要方法是通过他们的固定装置.从夹具中你得到一个b2Shape指针 - 但是,因为它的方法是虚拟的,你可能需要将它转换为b2PolygonShape/b2CircleShape指针才能使它有用.这是以下几行代码:
void DoStuffWithShapes(b2World *World)
{
b2Body * B = World->GetBodyList();
while(B != NULL)
{
b2Fixture* F = B->GetFixtureList();
while(F != NULL)
{
switch (F->GetType())
{
case b2Shape::e_circle:
{
b2CircleShape* circle = (b2CircleShape*) F->GetShape();
/* Do stuff with a circle shape */
}
break;
case b2Shape::e_polygon:
{
b2PolygonShape* poly = (b2PolygonShape*) F->GetShape();
/* Do stuff with a polygon shape */
}
break;
}
F = F->GetNext();
}
B = B->GetNext();
}
}
Run Code Online (Sandbox Code Playgroud)
其他一些注意事项:b2Body的.getNext()函数返回一个指针 - 这是一个链表的实现.b2Fixture :: GetNext()也是如此.在你的代码中有一些不熟悉的东西(对我来说),所以我不能肯定地说,但如果你只是通过并确保你的变量与Box2D函数的返回类型相匹配它可能会正常工作.
| 归档时间: |
|
| 查看次数: |
5528 次 |
| 最近记录: |