Bouncing problem in very basical particle system

Hi all, i'm not very expert about particle systems, but i have i think a very basic problem. I was trying to do a simple bounching point system, based on the speed inversion in case they touch one of the borders or go over a certain position (in this case the half of the screen). But, some of those, instead of just bounching, start like to "sink". I cant' explain to myself the reason, someone could help me? Thanks

Point[] myPoint;
float dragFactor = 0.99;


void setup()
{
  size(900, 600);
  myPoint = new Point[1000];

  for (int i = 0; i< 1000; i++)
  {
    myPoint[i] = new Point(random(0, width), random(0, height/2), 0, 0.1);
  }
}

void draw()
{
  background(200);
  line(0, height/2, width, height/2);
  for (int i = 0; i< myPoint.length; i++)
  {

    myPoint[i].update();
    myPoint[i].display();
  }
}

class Point
{
  PVector position = new PVector();
  float speed;
  float acceleration;

  Point(float myX, float myY, float mySpeed, float myAcc)
  {
    position.set(myX, myY);
    speed = mySpeed;
    acceleration = myAcc;
  }


  void update()
  {
    if (position.y >= height/2)
    {
      speed = -speed;
    }
    speed += acceleration;
    speed *= dragFactor;

    position.set(position.x, position.y+speed);
  }

  void display()
  {
    ellipse(position.x, position.y, 3, 3);
  }
}
Tagged:

Answers

  • edited January 2015

    bouncing

    I changed 3 things:

    • speed = - abs(speed); takes care that it goes up always. Otherwise when it is still in the reverse zone, it could stutter when you use * -1

    • position.y = height/2; pinning it down to where you want it

    • when speed is small:

      // when small stop it
      if (abs(speed)<.04)
      speed = 0;

    • this can stop the thing. As far as I know you get rounding errors in your number when using * 0.99 and it never gets 0 really (cause you also add something). To avoid it I set it to 0.

    Best, Chrisir ;-)

    Point[] myPoint;
    
    
    void setup()
    {
      size(900, 600);
      myPoint = new Point[1000];
    
      for (int i = 0; i< 1000; i++)
      {
        myPoint[i] = new Point(random(0, width), random(0, height/2), 0, 0.1);
      }
    }
    
    void draw()
    {
      background(200);
      line(0, height/2, width, height/2);
      for (int i = 0; i< myPoint.length; i++)
      {
    
        myPoint[i].update();
        myPoint[i].display();
      }
    }
    
    // ==========================================
    
    class Point
    {
      float dragFactor = 0.99;
    
      PVector position = new PVector();
      float speed;
      float acceleration;
    
      Point(float myX, float myY, 
      float mySpeed, 
      float myAcc)
      {
        position.set(myX, myY);
        speed = mySpeed;
        acceleration = myAcc;
      }
    
      void update()
      {
        if (position.y >= height/2)
        {
          speed = - abs(speed);
          position.y  = height/2;
        }
        speed += acceleration;
        speed *= dragFactor;
        // when small stop it 
        if (abs(speed)<.04) 
          speed = 0; 
    
        position.set(position.x, position.y+speed);
      }
    
      void display()
      {
        ellipse(position.x, position.y, 3, 3);
      }
    } // class
    //
    
  • Thanks, bot for code and english ;-)

  • bouncing

    Point[] myPoint;
    
    
    
    void setup()
    {
      size(900, 600);
      myPoint = new Point[1000];
    
      for (int i = 0; i< 1000; i++)
      {
        myPoint[i] = new Point(random(0, width), random(0, height/2), 0, 0.1);
      }
    }
    
    void draw()
    {
      background(200);
      line(0, height/2, width, height/2);
      for (int i = 0; i< myPoint.length; i++)
      {
    
        myPoint[i].update();
        myPoint[i].display();
      }
    }
    
    // ============================================
    
    class Point
    {
      float dragFactor = 0.99;
    
      PVector position = new PVector();
      float speed;
      float acceleration;
    
      Point(float myX, float myY, 
      float mySpeed, 
      float myAcc)
      {
        position.set(myX, myY);
        speed = mySpeed;
        acceleration = myAcc;
      }
    
      void update()
      {
        if (position.y >= height/2)
        {
          speed = - abs(speed);
          position.y  = height/2;
        }
        speed += acceleration;
        speed *= dragFactor;
        // when small stop it 
        if (abs(speed)<.04) 
          speed = 0; 
    
        position.set(position.x, position.y+speed);
      }
    
      void display()
      {
        ellipse(position.x, position.y, 3, 3);
      }
    } // class
    //
    
Sign In or Register to comment.