Guest User

bat and ball collision

a guest
Jul 28th, 2012
35
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     public class Ball
  2. {
  3.     ....
  4.         private Rectangle ballRect;
  5.     ....
  6.  
  7.   public Ball(args)
  8.     {  
  9.    
  10.     ....
  11.     ballRect = new Rectangle(0, 0, texture.Width, texture.Height);
  12.     ....   
  13.     }
  14.  
  15.    /// <summary>
  16.         /// Updates position of the ball. Used in Update() for GameplayScreen.
  17.         /// </summary>
  18.         public void UpdatePosition(GameTime gameTime)
  19.         {
  20.         ballRect.X = (int)ballPosition.X;
  21.             ballRect.Y = (int)ballPosition.Y;
  22.             oldBallPos.X = ballPosition.X;
  23.             oldBallPos.Y = ballPosition.Y;
  24.        
  25.             ballPosition.X += speed * ((float)Math.Cos(direction));
  26.  
  27.             ballPosition.Y += speed * ((float)Math.Sin(direction));
  28.             bool collided = CheckWallHit();
  29.  
  30.             // Stops the issue where ball was oscillating on the ceiling or floor
  31.             if (collided)
  32.             {
  33.                 ballPosition.X = oldBallPos.X + speed * (float)Math.Cos(direction);
  34.                 ballPosition.Y = oldBallPos.Y + speed * (float)Math.Sin(direction);
  35.             }      
  36.         }
  37. }
  38.  
  39. //////////////////////////////////////////////////////////////////////////////
  40.  
  41. public class bat
  42. {
  43.     ....
  44.         public Rectangle batRect;
  45.     ....
  46.  
  47.     public bat(args)
  48.     {
  49.     ....
  50.         batRect = new Rectangle(0, 0, leftBat.Width, leftBat.Height);
  51.     ....
  52.     }
  53.  
  54.        /// <summary>
  55.         /// Updates the position of the AI bat, in order to track the ball
  56.         /// </summary>
  57.         public virtual void UpdatePosition(Ball ball, GameTime gameTime)
  58.         {
  59.             ...
  60.         batRect.X = (int)BatPosition.X;
  61.             batRect.Y = (int)BatPosition.Y;
  62.         ....
  63.     }
  64. }
RAW Paste Data