Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 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:
- //bool collided = CheckWallHit(); draws the error:
- //"cannot convert type 'void' to target type 'bool'"
- //So how can I make this work?
- /////////// New Code
- public void UpdatePosition()
- {
- size.X = (int)position.X;
- size.Y = (int)position.Y;
- oldPos.X = position.X;
- oldPos.Y = position.Y;
- position.X += speed * (float)Math.Cos(direction);
- position.Y += speed * (float)Math.Sin(direction);
- bool collided = CheckWallHit();
- if (collided)
- {
- position.X = oldPos.X + speed * (float)Math.Cos(direction);
- position.Y = oldPos.Y + speed * (float)Math.Sin(direction);
- }
- }
- ///////////// Old Code
- public void UpdatePosition()
- {
- size.X = (int)position.X;
- size.Y = (int)position.Y;
- position.X += speed * (float)Math.Cos(direction);
- position.Y += speed * (float)Math.Sin(direction);
- CheckWallHit();
- }
- /////////// Check Wall Hit
- // Checks for collision with the ceiling or floor.
- // 2*Math.pi = 360 degrees
- // TODO: Change collision so that ball bounces from wall after getting caught
- private void CheckWallHit()
- {
- while (direction > 2 * Math.PI)
- {
- direction -= 2 * Math.PI;
- }
- while (direction < 0)
- {
- direction += 2 * Math.PI;
- }
- if (position.Y <= 0 || (position.Y > resetPos.Y * 2 - size.Height))
- {
- direction = 2 * Math.PI - direction;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement