1

I have 2 images(bar and greenBall1). bar can be move up and down depends on the user response. While, greenBall1 is moving around the screen. I want to do an image collision if both the images touch each other, greenBall1 will change its velocity. The codes that I have for greenBall1 are as below.

 private void OnUpdate(object sender, object e)
 {
         Canvas.SetLeft(this.GreenBall1, this.greenBallVelocityX + Canvas.GetLeft(this.GreenBall1));
         Canvas.SetTop(this.GreenBall1, this.greenBallVelocityY + Canvas.GetTop(this.GreenBall1));



        var greenBallPositionX1 = Canvas.GetLeft(this.GreenBall1);
         var greenBallPositionY1 = Canvas.GetTop(this.GreenBall1);



        var maximumGreenBallX = ActualWidth - this.GreenBall1.ActualWidth;
         var maximumGreenBallY = 400 - this.GreenBall1.ActualHeight; //Improvise: Instead of 360, get maximum height of canvas



        if (greenBallPositionX1 > maximumGreenBallX || greenBallPositionX1 < 0)
         {
             this.greenBallVelocityX *= -1;
         }



        if (greenBallPositionY1 > maximumGreenBallY || greenBallPositionY1 < 0)
         {
             this.greenBallVelocityY *= -1;
         }
      }
Gwenda.T
  • 31
  • 3
  • Do you want to be doing circle collision? http://stackoverflow.com/questions/1736734/circle-circle-collision – Dan Teesdale Jun 27 '13 at 01:53
  • Nope, I wish to do a rectangle collision. Basically I'm like trying to make it when the ball hits my rectangle bar it'll bounce off, right now the ball will just go through my bar. – Gwenda.T Jun 27 '13 at 02:01
  • is bar restricted to move up and down like Pong? – Dan Teesdale Jun 27 '13 at 02:05
  • Yup the bar will be able to move up and down just like Pong! – Gwenda.T Jun 27 '13 at 02:07
  • possible duplicate of [How to do intersection with condition in C#?](http://stackoverflow.com/questions/17667424/how-to-do-intersection-with-condition-in-c) – Shawn Kendrot Jul 17 '13 at 04:08

3 Answers3

0

I don't see a reference to the bar object in your code. But the detection of the collision is much easier than the physics of handling the collision. There are several schools of thought to something as simple as Pong collision, and the way you choose to handle it depends on the gameplay you want.

Here is an easy way to detect and handle the collision, simply by negating the X velocity just as you are handling the wall collisions:

if(greenBallPositionX1 < leftBar.X || greenBallPositionX1 > rightBar.X)
{
    this.greenBallVelocityX *= -1;
}

Keep in mind you might also have to take into account the width of the bar or the ball depending on where the coordinate is in relation to the image. For example:

if(greenBallPositionX1 < (leftBar.X + leftBar.Width) || greenBallPositionX1 > rightBar.X)
{
    this.greenBallVelocityX *= -1;
}

You may also want to at this point move the ball away from the paddle one step to avoid the collision being detected more than once.

Hopefully this answers what you were asking, but if you were looking for a more complex reaction to the collision detection, then you may want to check out the following discussion on Pong type collisions here.

Community
  • 1
  • 1
Dan Teesdale
  • 1,823
  • 15
  • 26
  • I intend to enable the bar to move up and down. But as of now, I haven't do it yet. May I know what should I declare X as? I am facing an error saying that 'System.Windows.Controls' does not contain a definition for 'X' and no extension method 'X' accepting a first arguments of type. – Gwenda.T Jun 27 '13 at 02:24
  • @Gwenda.T Sorry I was using X as if the object leftBar had an X position property. With the way you have the code now you could instead use the same pattern if you wished to: "var leftBarPositionX = Canvas.GetLeft(this.LeftBar);" – Dan Teesdale Jun 27 '13 at 02:27
  • @Gwenda.T Are you able to debug, and set a breakpoint to see if the ball position ever goes past the bar position, and the velocity is set? Do the collisions with the wall work currently? – Dan Teesdale Jun 27 '13 at 02:41
  • 1
    What I did was to declare leftBarPositionX. For the if statement, I used if(greenBallPositionX1<(leftBarPositionX + bar.Width)) { greenBallVelocityX *= 1; } – Gwenda.T Jun 27 '13 at 02:41
  • @Gwenda.T Should be *= -1. – Dan Teesdale Jun 27 '13 at 02:43
  • Sorry, I have put -1. It is not working. I did a debug. The ball position goes past the bar position – Gwenda.T Jun 27 '13 at 02:46
  • @Gwenda.T If that is the case, -and- the velocity is also being negated, then you will have to move the ball away from the paddle to avoid the collision being detected more than once. – Dan Teesdale Jun 27 '13 at 02:51
  • I am sorry @Dan, I do not understand what you meant by avoid the collision being detected more than once. – Gwenda.T Jun 27 '13 at 02:54
  • @Gwenda.T Continuing this discussion would be a good candidate for the chat, but you don't have enough rep yet to join it. I'd suggest rephrasing the question and posting again if you still don't find the solution you're looking for. These comments could go on for a long time before finding the answer. Perhaps try asking again with the additional code added. – Dan Teesdale Jun 27 '13 at 03:09
0

i think this might help you ....

.

            Rectangle ballRect = new Rectangle((int)ballposition.X, (int)ballposition.Y, ballsprite.Width, ballsprite.Height);

            Rectangle handRect = new Rectangle((int)paddlePosition.X, (int)paddlePosition.Y, paddleSprite.Width, paddleSprite.Height/2);

            if (ballRect.Intersects(handRect))
            {
                // Increase ball speed
                ballSpeed.Y += 50;

                if (ballSpeed.X < 0)
                    ballSpeed.X -= 50;

                else
                    ballSpeed.X += 50;

                // Send ball back up the screen

                ballSpeed.Y *= -1;
            }

in this whenever the ball will collide with hand it will increase its speed and change its direction i think that is the think you are looking for as it is also a rectangular collision.

Sandeep Chauhan
  • 1,313
  • 1
  • 10
  • 23
  • I do not want to use XNA. Do you have any other idea? – Gwenda.T Jun 27 '13 at 06:45
  • why dont you want to use XNA ?? just asking as it will be the best way to do as collision is quite easy to detect in XNA .. – Sandeep Chauhan Jun 27 '13 at 06:47
  • I was told not to use it. XNA is compatible, however Microsoft no longer supports it. – Gwenda.T Jun 27 '13 at 06:53
  • XNA is no longer supported as it is not compatible for multiresolution but you can use monogame as it uses XNA framework and is supported... it will be a good option for you.. – Sandeep Chauhan Jun 27 '13 at 06:54
  • I understand that it is a good option and probably the best way to do it. But I was told not to use it from my lecturer. – Gwenda.T Jun 27 '13 at 06:58
0

make a class which will give you value that wether two rects will collide or not

    public bool Intersects(Rect r1,Rect r2)
    {
      r1.Intersect(r2);

      if(r1.IsEmpty)
      {
        return false;
      }
      else 
      {
        return true;
      }
    }

then you can use

if(Intersects(r1,r2))
{
  MessageBox.Show("Collison Detected");
}
Sandeep Chauhan
  • 1,313
  • 1
  • 10
  • 23