Piston effect in Processing simulation

0

I am trying to set up a simulation of a 2D box containing a number of particles (circles of radius r) colliding elastically with each other and the wall. I then tried to add in a piston (a line object with some strokeweight) which should also collide with the particles.

The line is drawn from width to width and the y position of it is controlled by the mouseY value. I want this feature in my code in order to have it behave like a piston.

Particle (circle) Collision with the walls and each other work very well. But line-circle collision seems ‘mushy’.

I copied the collision mechanism from

which works for me in a limited capacity. When I do not move the line the particles seem to bounce off as expected. Problem starts when I move the line up or down. The particles do not seem to bounce off of the line. Worse, some particles actually shoot through and come to the other side of the line.

I have used the following code inside my particle class for collision detection. lineCircle is the function as written in the Jeff Thompson blog (link provided above)

public void pistoncollide(piston line){
    float decay = 1;
    if (lineCircle(line.getx1(),line.gety1(),line.getx2(),line.gety2(),this.getX(),this.getY(),this.getsize()/2.0) == true)
    {
      this.setDY(-Math.abs(this.getDY())/decay);
      this.setDX(this.getDX());

    
      }
    }
  }

Additionally, I would want the particles to ‘exchange’ velocities with my piston (line object). For example, when the particles collide with each other they exchange velocities. I want my particles to be ‘pushed’ by the piston.]

I am unsure what other part of my code will be necessary here to dissect the problem, but I will add if asked. Please help.

1 Like

i don’t have an answer for you but have you seen iforce2d Combustion engine it uses box2d (shiffman has a bunch of videos on using it with processing) which makes the whole situation much easier but still a complicated project. There is source and a video of the results on the page which are super interesting and may be of help. best of luck.

1 Like