Advertisement
DaveVoyles

casting void to bool

Jun 1st, 2012
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.87 KB | None | 0 0
  1. // Long story short: Ball gets stuck against the ceiling / floor sometimes and oscillates //quickly. Was hoping this new code would correct the problem, but the line:
  2.  
  3. //bool collided = CheckWallHit(); draws the error:
  4.  
  5. //"cannot convert type 'void' to target type 'bool'"
  6.  
  7. //So how can I make this work?
  8.  
  9. /////////// New Code
  10.  
  11.         public void UpdatePosition()
  12.         {
  13.             size.X = (int)position.X;
  14.             size.Y = (int)position.Y;
  15.             oldPos.X = position.X;
  16.             oldPos.Y = position.Y;
  17.             position.X += speed * (float)Math.Cos(direction);
  18.             position.Y += speed * (float)Math.Sin(direction);
  19.             bool collided = CheckWallHit();
  20.             if (collided)
  21.             {
  22.                 position.X = oldPos.X + speed * (float)Math.Cos(direction);
  23.                 position.Y = oldPos.Y + speed * (float)Math.Sin(direction);
  24.             }
  25.         }
  26.  
  27. ///////////// Old Code
  28.  
  29.         public void UpdatePosition()
  30.         {
  31.             size.X = (int)position.X;
  32.             size.Y = (int)position.Y;
  33.             position.X += speed * (float)Math.Cos(direction);
  34.             position.Y += speed * (float)Math.Sin(direction);
  35.             CheckWallHit();
  36.         }
  37.  
  38.  
  39. /////////// Check Wall Hit
  40.  
  41.      // Checks for collision with the ceiling or floor.
  42.         // 2*Math.pi = 360 degrees
  43.         // TODO: Change collision so that ball bounces from wall after getting caught
  44.         private void CheckWallHit()
  45.         {
  46.             while (direction > 2 * Math.PI)
  47.             {
  48.                 direction -= 2 * Math.PI;
  49.             }
  50.  
  51.             while (direction < 0)
  52.             {
  53.                 direction += 2 * Math.PI;
  54.             }
  55.  
  56.             if (position.Y <= 0 || (position.Y > resetPos.Y * 2 - size.Height))
  57.             {
  58.                 direction = 2 * Math.PI - direction;
  59.             }
  60.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement