title: Reversing the direction of an object after a collision
body: <p>Edit2: See below for new problem.</p>
  
  <p>I'm creating a Windows Phone 7 game and have run into some difficulty. What I'm trying to do is use touchscreen coordinates converted to radians to fire an object upwards at an angle. That part works fine. </p>
  
  <p>However, I cannot figure out how to make the object bounce off the side walls. I had it working in an earlier implementation, and it was simply a matter of negating the Y value when colliding with a wall. Unfortunately, it's not so easy this time. When the object hits the wall, it bounces off the wall for a short distance before changing direction towards the wall again.   </p>
  
  <p>How can I structure my code so it stops getting pushed into the wall?</p>
  
  <p>Thank you.</p>
  
  <pre><code>//Move bubble
  public void MoveBubble(Bubble b, double radians)
  {
      if (b.stop == false)
      {
          {
              //Move bubble 'up'
              Y = (float)(speed * Math.Cos(radians));
              X = (float)(speed * Math.Sin(radians));
      }
  
      b.bubblePosX += X;
      b.bubblePosY += Y;
  
      //Problem area
      if(b.bubblePosY &lt;= 0)
          Y-=Y;
  
      if(b.bubblePosY &gt;= 350)
          Y-=Y;
  }
  
  
  //Calculate Angle
  private void CalculateAngle(int touchX, int touchY)
  {
      radians = (Math.Atan2(touchX - bubbleStart.X, touchY - bubbleStart.Y));
  }
  </code></pre>
  
  <p>Edit2: Just wanted to add the latest code. Now the ball is bouncing off the walls perfectly, but I'm having an issue with the direction bool being initialized to true. Sometimes, when firing the object, the angle is reversed. How can set the initial bool to the appropriate true/false value?</p>
  
  <pre><code>    //Move bubble
      public void MoveBubble(Bubble bubble, Vector2 distance)
      {
          if (bubble.stop == false)
          {
              float velocityX = speed * distance.X;
              float velocityY = speed * distance.Y;
  
              bubble.X += velocityX;
  
              if (direction == true)
                  bubble.Y += velocityY;
  
              if (direction == false)
                  bubble.Y -= velocityY;
  
              if (bubble.Y &lt;= 0)
                  direction = !direction;
  
              if (bubble.Y &gt;= 350)
                  direction = !direction;
          }
      }
  </code></pre>