Box2D对象重叠问题
对不起,我的英语不好,这不是我的母语。
我使用Pbox2D库进行了使用处理模拟(与普通Box2D几乎相同)。 它确实有效,但我注意到对象彼此重叠。 我试图通过更改对象的恢复恒定或密度来解决它,但没有任何改变。 有什么方法可以阻止这种现象或至少减少重叠?
我会向您展示一些项目的屏幕截图和源代码。
。
这是我的对象类代码。
class Box
{
Body body;
float radius;
Box(){
BodyDef bd=new BodyDef();
bd.type=BodyType.DYNAMIC;
bd.position.set(box2d.coordPixelsToWorld((float)Math.random()*900+1,(float)Math.random()*300+1));
body=box2d.createBody(bd);
CircleShape cs=new CircleShape();
radius=10;
cs.m_radius=box2d.scalarPixelsToWorld(radius);
FixtureDef fd=new FixtureDef();
fd.shape=cs;
fd.density=10;
fd.friction=0.3;
fd.restitution=0.5;
body.createFixture(fd);
}
void display()
{
Vec2 pos=box2d.getBodyPixelCoord(body);
float a = body.getAngle();
pushMatrix();
translate(pos.x,pos.y);
rotate(-a);
fill(175);
stroke(0);
rectMode(CENTER);
ellipse(0,0,2*radius,2*radius);
popMatrix();
}
void killBody()
{
box2d.destroyBody(body);
}
}
Sorry for my bad English, it is not my first language.
I am making a simulation with processing, using pbox2d library(it is almost same with normal box2d).
It does work, but I noticed that objects are overlapping to each others.
I tried to fix it by changing the restitution constant or density of objects, but nothing changed.
Is there any way to stop this phenomenon-or at least decrease overlapping?
I'll show you some screenshots and source code of my projects.
Thank you for helping me!
This is my code for object class.
class Box
{
Body body;
float radius;
Box(){
BodyDef bd=new BodyDef();
bd.type=BodyType.DYNAMIC;
bd.position.set(box2d.coordPixelsToWorld((float)Math.random()*900+1,(float)Math.random()*300+1));
body=box2d.createBody(bd);
CircleShape cs=new CircleShape();
radius=10;
cs.m_radius=box2d.scalarPixelsToWorld(radius);
FixtureDef fd=new FixtureDef();
fd.shape=cs;
fd.density=10;
fd.friction=0.3;
fd.restitution=0.5;
body.createFixture(fd);
}
void display()
{
Vec2 pos=box2d.getBodyPixelCoord(body);
float a = body.getAngle();
pushMatrix();
translate(pos.x,pos.y);
rotate(-a);
fill(175);
stroke(0);
rectMode(CENTER);
ellipse(0,0,2*radius,2*radius);
popMatrix();
}
void killBody()
{
box2d.destroyBody(body);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
HI Box2D是一个近似迭代的求解器,无法准确计算。因此,在您的示例中,我认为没有足够的迭代来允许多个身体进行互动。
约束求解器中有两个阶段:速度阶段和位置阶段。在速度阶段,求解器计算身体正确移动所需的冲动。在位置阶段,求解器调节身体的位置,以减少重叠和关节脱离。每个阶段都有自己的迭代计数。此外,如果错误小
。 box2d.org/documentation/md__d_1__git_hub_box2d_docs_hello.hello.html
因此,您可能会看到更多迭代的改进,这将解决这一复杂的重叠。默认值是速度的8个迭代,位置为3个。
当您的世界步骤时,两者都会有更高的价值。请注意,位置迭代可能会尽早结束,但速度迭代不会。我认为您需要更多的速度迭代才能防止嵌入,因为更多的位置迭代并不是右中间重叠的大圆圈的修复。
ETC
Hi Box2d is an approximate iterative solver, it can't calculate exactly. So in your example, I think, that there aren't enough iterations to allow the multiple bodies to interact.
There are two phases in the constraint solver: a velocity phase and a position phase. In the velocity phase the solver computes the impulses necessary for the bodies to move correctly. In the position phase the solver adjusts the positions of the bodies to reduce overlap and joint detachment. Each phase has its own iteration count. In addition, the position phase may exit iterations early if the errors are small.
https://box2d.org/documentation/md__d_1__git_hub_box2d_docs_hello.html
So probably you can see an improvement with more iterations, which will solve this complex overlap. Default is 8 iterations for velocity and 3 for positions.
Play around with higher values for both when you world step. Note, position iterations can end early but velocity iterations won't. I think you need more velocity iterations to prevent the embedding because more position iterations won't be a fix for the big circle overlapped on the middle right.
etc