如何使用 Windows Phone 应用程序在 Windows Phone 8 的 C# 中进行图像碰撞? [英] How to do image collision in C# for Windows Phone 8 using Windows Phone App?

查看:25
本文介绍了如何使用 Windows Phone 应用程序在 Windows Phone 8 的 C# 中进行图像碰撞?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 2 张图片(bar 和 greenBall1).栏可以上下移动取决于用户的反应.同时,greenBall1 在屏幕上移动.如果两个图像相互接触,我想进行图像碰撞,greenBall1 将改变其速度.我的 greenBall1 代码如下.

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

推荐答案

创建一个类,该类将为您提供两个矩形是否会碰撞的价值

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

然后你可以使用

if(Intersects(r1,r2))
{
  MessageBox.Show("Collison Detected");
}

这篇关于如何使用 Windows Phone 应用程序在 Windows Phone 8 的 C# 中进行图像碰撞?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆