Box2d 多边形盒子无法准确碰撞。我使用 Box2ddebug 渲染器并多次重复使用 Polygonshape。当盒子形状位于地面物体上(静态)时,这些物体之间存在间隙。谁能帮我解决这个问题吗?
public class BoxShape extends Shape implements ShapeAccessor {
private DotShape a, b, c, d;
private Rectangle rect;
private Vector2 temp = new Vector2();
private float hw = 0, hh = 0;
public BoxShape(float x, float y, float u, float v) {
a = new DotShape(x, y, this);
b = new DotShape(u, y, this);
c = new DotShape(u, v, this);
d = new DotShape(x, v, this);
hw = u - x;
hh = y - v;
rect = new Rectangle(d.getX(), d.getY(), hw, hh);
rect.getCenter(temp);
center = new DotShape(temp.x, temp.y, this);
center.setColor(Color.MAGENTA);
bodyDef.position.set(temp.x * Assets.ws, temp.y * Assets.ws);
this.setColor(Color.BLUE);
}
@Override
public Body createBody(World world) {
Body b = world.createBody(bodyDef);
b.setUserData(new Data(getName()));
return b;
}
@Override
public Fixture createFixture(Body body) {
Assets.p.setAsBox(((rect.width / 2) * Assets.ws),
((rect.height / 2) * Assets.ws));//ws does scaling. world ranges from 0.1 to 20 meter
fixtureDef.shape = Assets.p;
return body.createFixture(fixtureDef);
}
........................
........................
然后主体和夹具的制作方法是:
public void createWorld(World world) {
for (Shape s : shapes) {
Body b=s.createBody(world);
s.createFixture(b);
}
for (JointShape j : joints) {
j.createJoint(world);
}
}
最佳答案
你的形状非常非常小吗?您可能需要缩放您的世界,以便常见形状的大小约为 1 个单位。
当灯具重叠时,它们会被推开,这样它们就不会重叠,但在许多情况下(特别是堆叠的物体)很难保持这种情况稳定。顶部的盒子由于重力而向下移动并接触下面的盒子,然后被推回上方,依此类推,永远如此。除了周围的抖动看起来很糟糕之外,这永远不会稳定并浪费计算时间,因为 body 永远无法休眠。
为了解决这个问题,灯具周围有一个微小的空间,用作缓冲区,其中两个相邻的灯具可以同意它们正在接触,但没有重叠,因此不需要进行校正 - 它们可以微笑彼此并放松。该缓冲区的大小是 b2Settings 中的一个值,虽然您可以更改它 (b2_linearSlop),但您应该在执行此操作之前真正了解自己在做什么。 (请记住,这是在 Box2D 中使用的 #define 常量,因此您需要重新编译库本身才能使更改完全生效。)我建议您缩放世界以使用更大的尺寸。
关于libgdx - box2d 盒子碰撞不准确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25195564/