How to do image collision in C# for Windows Phone 8 using Windows Phone App?

戏子无情 提交于 2019-12-11 10:18:14

问题


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;
         }
      }

回答1:


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.




回答2:


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.




回答3:


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");
}


来源:https://stackoverflow.com/questions/17333291/how-to-do-image-collision-in-c-sharp-for-windows-phone-8-using-windows-phone-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!