Advertisement
DaveVoyles

Bat collision rec

Jul 28th, 2012
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     public class Ball
  2.     {
  3.         private Rectangle ballRect;  
  4.         private AIBat rightBat;
  5.         private Bat leftBat;
  6.         private Rectangle rectangle3;
  7.  
  8.         /// <summary>
  9.         /// Constructor for the ball
  10.         /// </summary>
  11.         public Ball(ContentManager contentManager, Vector2 ScreenSize, Bat bat, AIBat aiBat)
  12.         {
  13.             speed = 15f;
  14.             texture = contentManager.Load<Texture2D>(@"gfx/balls/redBall");
  15.             direction = 0;
  16.             ballRect = new Rectangle(0, 0, texture.Width, texture.Height);
  17.             resetBallPos = new Vector2(ScreenSize.X / 2 + origin.X, ScreenSize.Y / 2 + origin.Y);
  18.             ballPosition = resetBallPos;
  19.             rand = new Random();
  20.             isVisible = true;
  21.             origin = new Vector2(texture.Width / 2 , texture.Height / 2);
  22.             leftBat = bat; // Creates a new instance of leftBat so that I can access Position.X/Y for LeftBatPatcicles()
  23.             rightBat = aiBat;// Creates a new instance of leftBat so that I can access Position.X/Y for RightBatPatcicles()
  24.             Rectangle rectangle3 = new Rectangle();
  25.         }
  26.  
  27.      
  28.         public void Draw(SpriteBatch spriteBatch)
  29.         {
  30.             if (isVisible)
  31.             {
  32.                 // Draws the rotaing ball
  33.                 spriteBatch.Draw(texture, ballPosition, ballRect, Color.White,
  34.                                   RotationAngle, origin, 1.0f, SpriteEffects.None, 0);
  35.                 // Draws the short particle
  36.            //     shortParticle.Draw(spriteBatch);
  37.                 spriteBatch.Draw(texture, rectangle3, Color.Yellow);
  38.  
  39.             }
  40.         }
  41.  
  42.         /// <summary>
  43.         /// Updates position of the ball. Used in Update() for GameplayScreen.
  44.         /// </summary>
  45.         public void UpdatePosition(GameTime gameTime)
  46.         {
  47.             ballRect.X = (int)ballPosition.X;
  48.             ballRect.Y = (int)ballPosition.Y;
  49.             oldBallPos.X = ballPosition.X;
  50.             oldBallPos.Y = ballPosition.Y;
  51.        
  52.             ballPosition.X += speed * ((float)Math.Cos(direction));
  53.  
  54.             ballPosition.Y += speed * ((float)Math.Sin(direction));
  55.             bool collided = CheckWallHit();
  56.  
  57.             // Stops the issue where ball was oscillating on the ceiling or floor
  58.             if (collided)
  59.             {
  60.                 ballPosition.X = oldBallPos.X + speed * (float)Math.Cos(direction);
  61.                 ballPosition.Y = oldBallPos.Y + speed * (float)Math.Sin(direction);
  62.             }
  63.            
  64.             // When the ball and bat collide, draw the rectangle where they intersect
  65.             BatCollisionRect();          
  66.  
  67.             // The time since Update was called last.
  68.             float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
  69.  
  70.             // Rotation for the ball
  71.             RotationAngle += elapsed;
  72.             float circle = MathHelper.Pi * 2;
  73.             RotationAngle = RotationAngle % circle;        
  74.         }
  75.  
  76.        
  77.         /// <summary>
  78.         /// Used to determine the location where the particles will initialize when the ball and bat collide
  79.         /// </summary>
  80.         public void BatCollisionRect()
  81.         {
  82.             if (ballRect.Intersects(leftBat.batRect))
  83.             {            
  84.              rectangle3 = Rectangle.Intersect(ballRect, leftBat.batRect);
  85.             }
  86.         }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement