title: Libgdx: How to move objects on X axis without overlapping?
body: <p>I have a rectangle array holding multiple objects, moving back and forth on X axis.</p>
  
  <pre><code>Iterator&lt;Rectangle&gt; iter = array.iterator();
  
  while ( iter.hasNext() ) {
      Rectangle obj = iter.next();
      array.get(i).x += speed * Gdx.graphics.getDeltaTime() ;
  
      if (obj.x + obj.width &gt; 800 || obj.x &lt; 0) {
          speed = -speed;
      }
  }
  </code></pre>
  
  <p>When the speed gets bigger, you'll start noticing the first object in the array overlapping with the other objects and pushing them apart. How to fix that?</p>
  
  <p>Basically each object has</p>
  
  <pre><code>Rectangle obj = new Rectangle();
  obj.x = xpos;
  obj.y = ypos;
  obj.width = width;
  obj.height = height;
  xpos += width + 4;
  </code></pre>
  
  <p>And has a texture, image, a sqaure, a rectange, a triangle... And each object is generated at an X position <code>xpos</code> different than the other. All they do is keep moving on the X axis, from x=0 till 800 and back.</p>
  
  <p>What happens is that when the first object gets to 0, it tries to increase its speed again and overlapping with other objects, and then time after time, all objects keep overlapping and get further apart from each other. I want the distance between the objects to stay constant at any speed.</p>