title: How to bounce an object inside circular bounds?
body: <p>In my game i have circular bounds and inside the bounds i have an object that is supposed to bounce around, but for some reason it does not bounce. My code is shown below.</p>
  
  <pre><code>public void checkBouncer(Bouncer b){
      float x = b.getPosition().x;
      float y = b.getPosition().y;
      float cx = Constants.CENTER_X - b.getWidth() / 2;
      float cy = Constants.CENTER_Y - b.getHeight() / 2;
      float dx = Math.abs(x - cx);
      float dy = Math.abs(y - cy);
      float r = (Constants.PARAMETER_RADIUS - 45) - b.getHeight();
  
      // check collision between bounds and bouncer
      if ((dx * dx) + (dy * dy) &gt;= (r * r)) {
          // original velocity vector
          Vector2 v1 = new Vector2(b.getVelocity().x, b.getVelocity().y);
  
          // normal vector
          Vector2 n = new Vector2(
                  Constants.CENTER_X - b.getPosition().x,
                  Constants.CENTER_Y - b.getPosition().y
          );
  
          // normalize
          if (n.len() &gt; 0) n = n.nor();
  
          // dot product
          float dot = v1.x * n.x + v1.y * n.y;
  
          // reflected vector values
          v2.x = v1.x - 2 * dot * n.x;
          v2.y = v1.y - 2 * dot * n.y;
      }
      // set new velocity
      b.setVelocity(v2);
  }
  </code></pre>
  
  <p>The object that i want to bounce starts out by moving in the direction it is facing, the update method is shown below:</p>
  
  <pre><code>public void update(float delta) {
      direction.x = (float) Math.cos(Math.toRadians(angle));
      direction.y = (float) Math.sin(Math.toRadians(angle));
  
      if (direction.len() &gt; 0) {
          direction = direction.nor();
      }
  
      velocity.x = direction.x * speed;
      velocity.y = direction.y * speed;
  
      position.x += velocity.x * delta;
      position.y += velocity.y * delta;
  
      circle.set(position.x + width / 2, position.y + height / 2, width / 2);
  }
  </code></pre>
  
  <p>But if i invert the angle on collision its kind of working but it's jittery and does not always bounce back on collision. The code for that is here:</p>
  
  <pre><code>if ((dx * dx) + (dy * dy) &gt;= (r * r)) {
          b.setAngle(-b.getAngle());
  }
  </code></pre>
  
  <p>Please i need help.</p>