1

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.

densely packed gray circles with black outlines on a white background. the circles on the right side are overlapping as if gravitating towards a single larger circle

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);
 }
}
George Profenza
  • 50,687
  • 19
  • 144
  • 218
김혁기
  • 11
  • 1
  • Hi and welcome :) It's great you posted a screenshot of the behaviour and the Box class. Unfortunately I can not figure out the exact source of the problem from the `Box` class alone. Am I correct in assuming the larger circle on the right is a body that can be dragged with a mouse ? (Does it have other behaviours such as attracting other bodies ?). I recommend trying to tweak the density, friction, restitution and maybe even mass properties for the large circle first, then also the smaller circles and hopefully the the constraints will ensure less overlap. – George Profenza Jun 04 '22 at 10:47
  • Thank you for helping me :) It is a program that simulates balls in a rectangular box which is shaked by external force. The big circle doesn't attract other circles or controlled by a mouse, and I don't think that it is a problem caused by larger circle since overlapping occurs without the larger circle too.(See the right, upper corner of the picture) I've tried changing constants such as density or restitution, but I couldn't see notable changes. Is there any way to 'disable' overlapping at all? – 김혁기 Jun 04 '22 at 13:30
  • I think I understand. As far as I know there isn't a single property you can set to false, but a combination as I previously mentioned. I might be wrong and hopefully other users can advise. Your description does remind me of the [Liquid Fun Wave Machine](https://google.github.io/liquidfun/). There is a [LiquidFun Processing library](https://github.com/diwi/LiquidFunProcessing) which includes a [Wave Machine example](https://github.com/diwi/LiquidFunProcessing/blob/master/examples/liquidfun_WaveMachine/liquidfun_WaveMachine.java). Bare in mind it may use the same jbox2d java library... – George Profenza Jun 04 '22 at 18:57
  • ...behind the scenes and this may cause a library conflict in Processing. If that's the case you can temporarily uninstall one library while you're testing the other. – George Profenza Jun 04 '22 at 18:58

1 Answers1

0

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.

world.step(timeInterval, 16,6);

etc

londonBadger
  • 611
  • 2
  • 5
  • 5
  • Thank you for the answer, sorry for reading the comment too late- I completely forgot that I wrote this post. I am currently using PBox2d, a processing port of c++ box2d. But I can't find a function or parameter to change the iteration. Is there any way to change iteration in PBox2d too? – 김혁기 Aug 31 '22 at 05:55
  • I think PBox2d got renamed to Box2d-for-Processing, if this is the case, then there is a custom step function ```public void step( int velocityIterations, int positionIterations) {``` in ```Box2DProcessing``` – londonBadger Sep 02 '22 at 01:02