Search code examples
c#visual-studiocollision-detectionpictureboxpong

If(picturebox.Right == panel.Right) not working but if(picturebox.Left == panel.Left) works


I'm not sure why but anytime I'm working with collision it seems like that everything goes well for the top and left properties of a control, but for the right and bottom properties not so much. I'm not sure if its because they are read only properties or not, but someone please help.

heres the code

playground(panel) class

    namespace final_pong_game_phase_3
{
class PlayGround
{
    public Panel Panel;
    Size PlayGroundSize;
    public int Top;
    public int Bottom;
    public int Right;
    public int Left;
    public PlayGround(Panel panel,Size size, Point location)
    {
        this.Panel = panel;
        this.PlayGroundSize = size;
        this.Panel.Size = this.PlayGroundSize;
        this.Panel.BackColor = Color.Black;
        this.Panel.Location = location;
        this.Panel.SendToBack();

        this.Top = panel.Top;
        this.Bottom = panel.Bottom;
        this.Right = panel.Right;
        this.Left= panel.Left;
    }



}
} 

Ball(picturebox) Class

    namespace final_pong_game_phase_3
  {
class Ball
{
    Size BallSize;
    Point Location;
    int TopSpeedInterval;
    int LeftSpeedInterval;
    PictureBox BallGraphic;
    public int Top;
    public int Left;
    public int Bottom;
    public int Right;

    public Ball(Point location, int topspeedinterval,int leftspeedinterval, Size ballsize, PictureBox ballgraphic)
    {
        this.Location = location;
        this.TopSpeedInterval = topspeedinterval;
        this.LeftSpeedInterval = leftspeedinterval;
        this.BallSize = ballsize;
        this.BallGraphic = ballgraphic;

        this.BallGraphic.Location = this.Location;
        this.BallGraphic.BackColor = Color.White;
        this.BallGraphic.Size = this.BallSize;
        this.BallGraphic.BringToFront();

        this.Top = BallGraphic.Top;
        this.Left = BallGraphic.Left;
        this.Bottom = ballgraphic.Bottom;

    } 

    public void start()
    {
        Top -= TopSpeedInterval;
        Left -= LeftSpeedInterval;

        BallGraphic.Top = Top;
        BallGraphic.Left = Left;
        Bottom = BallGraphic.Bottom;
        Right = BallGraphic.Right;


    } 

    public void SwitchTopDirection()
    {
        TopSpeedInterval *=-1; 

    } 

    public void SwitchLeftDirection()
    {
        LeftSpeedInterval *= -1;
    } 

    public void Hit()
    {
        TopSpeedInterval+= 5;
        LeftSpeedInterval+= 5;
    }
}
 } 

GameWorld(controls and monitors the game everything in the method of the class happens every time the timer ticks) Class

    //ball to wall collision 
        if (playground.Top == ball.Top)
        {
            ball.SwitchTopDirection();
            Console.Beep(1000, 100);
        }

        if (playground.Left == ball.Left)
        {
            Console.Beep(1500, 700);
            MessageBox.Show("Player 2 Wins!!!");

        }

        if (playground.Right == ball.Right)
        {
            MessageBox.Show("Player 2 Wins!!!");
        }

        if (ball.Bottom >= playground.Bottom)
        {
            ball.SwitchTopDirection();
            Console.Beep(1000, 100);
        }

Solution

  • This would probably do the trick for you

    if (Math.Abs(playground.Right - ball.Right)<=7)
    {
        MessageBox.Show("Player 2 Wins!!!");
    }
    

    What we are doing in this code is that we are checking the range of +-7 of both Playground and Ball it collides.