欢迎光临
我们一直在努力

[源码解读]Silverlight 4 中对不规则对象进行碰撞检测(在游戏中常使用的是否碰撞怪物边界等原理)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace HitTest
{
public partial class Page : UserControl
{
public Page()
{
InitializeComponent();


}

private void UserControl_MouseMove(object sender, MouseEventArgs e)
{
Point pt
= e.GetPosition(cnvHitTest);

ship.SetValue(Canvas.LeftProperty, pt.X);
ship.SetValue(Canvas.TopProperty, pt.Y);

// lets get pointers to the actual UI elements we care about:
// 获得飞船的Path坐标
Path shipShell = ship.FindName("ShipShell") as Path;
// 获得陨石的path坐标
Path cnvAsteroid = asteroid.FindName("asteroidBig") as Path;

// 检测碰撞
// ship 飞船的用户控件(矩形大范围)
// shipShell 飞船的外壳Path坐标
// asteroid 陨石的用户控件(矩形大范围)
// cnvAsteroid 陨石的外壳Path坐标
if (CheckCollision(ship, shipShell, asteroid, cnvAsteroid))
txtStatus.Text
= "Collision!";
else
txtStatus.Text
= "no collision";
}

private bool CheckCollision(FrameworkElement control1, FrameworkElement controlElem1, FrameworkElement control2, FrameworkElement controlElem2)
{
// first see if sprite rectangles collide
// 用户控件使用的是Canvas,我们现在要把用户控件的Canvas转换成矩形表示
// UserControlBounds()用于转换为矩形来表示编辑
// rect1 为飞船矩形
// rect2 为陨石矩形
// control1 为飞船用户控件(矩形)
// control2 为运行用户控件(矩形)
Rect rect1 = UserControlBounds(control1);
Rect rect2
= UserControlBounds(control2);

// Intersect(Rect) 查找当前矩形和指定矩形的交集,并将结果存储为当前矩形。
rect1.Intersect(rect2);
if (rect1 == Rect.Empty) // 为空为矩形没有交集,那么飞船和陨石没有碰撞
{
// no collision - GET OUT!
return false;
}
else // 不为空,有交集,返回交集,但不表示飞船就和陨石有碰撞,需要进行更细致的判断
{
bool bCollision = false; // 是否碰撞
Point ptCheck = new Point(); // 点检测

// now we do a more accurate pixel hit test
// 进行精确的点测试
// 以行为单位,循环扫描矩形内的每个点
// 在这里rect1为交集的矩形
for (int x = Convert.ToInt32(rect1.X); x < Convert.ToInt32(rect1.X + rect1.Width); x++)
{
// 遍历行中的每个点
for (int y = Convert.ToInt32(rect1.Y); y < Convert.ToInt32(rect1.Y + rect1.Height); y++)
{
// 行增加

//设置检测点的坐标
ptCheck.X = x;
ptCheck.Y
= y;

// 用FindElementsInHostCoordinates方法找出飞船用户控件中在点ptCheck上的element元素
List<UIElement> hits = System.Windows.Media.VisualTreeHelper.FindElementsInHostCoordinates(ptCheck, control1) as List<UIElement>;
// controlElem1 为实际的飞船
if (hits.Contains(controlElem1)) // hits里面装的是飞船用户控件中所有在点ptCheck中的元素,看实际的飞船是否在其中
{
// we have a hit on the first control elem, now see if the second elem has a similar hit
// 我们检测的飞船,还需要看点是否也在陨石中
// 同上面获取hits的方法,找出陨石控件中在点ptCheck上的element元素
List<UIElement> hits2 = System.Windows.Media.VisualTreeHelper.FindElementsInHostCoordinates(ptCheck, control2) as List<UIElement>;
// controlElem2 为实际的陨石
if (hits2.Contains(controlElem2)) // hits2里面装的是陨石用户控件中所有在点ptCheck中的元素,看实际的陨石是否在其中
{
// 够满足条件,是碰撞
bCollision = true;
break;
}
}
}
if (bCollision) break;
}
return bCollision;
}


}




public Rect UserControlBounds(FrameworkElement control)
{
Point ptTopLeft
= new Point(Convert.ToDouble(control.GetValue(Canvas.LeftProperty)), Convert.ToDouble(control.GetValue(Canvas.TopProperty)));
Point ptBottomRight
= new Point(Convert.ToDouble(control.GetValue(Canvas.LeftProperty)) + control.Width, Convert.ToDouble(control.GetValue(Canvas.TopProperty)) + control.Height);

return new Rect(ptTopLeft, ptBottomRight);
}


}
}

  • 海报
海报图正在生成中...
赞(0) 打赏
声明:
1、本博客不从事任何主机及服务器租赁业务,不参与任何交易,也绝非中介。博客内容仅记录博主个人感兴趣的服务器测评结果及一些服务器相关的优惠活动,信息均摘自网络或来自服务商主动提供;所以对本博客提及的内容不作直接、间接、法定、约定的保证,博客内容也不具备任何参考价值及引导作用,访问者需自行甄别。
2、访问本博客请务必遵守有关互联网的相关法律、规定与规则;不能利用本博客所提及的内容从事任何违法、违规操作;否则造成的一切后果由访问者自行承担。
3、未成年人及不能独立承担法律责任的个人及群体请勿访问本博客。
4、一旦您访问本博客,即表示您已经知晓并接受了以上声明通告。
文章名称:《[源码解读]Silverlight 4 中对不规则对象进行碰撞检测(在游戏中常使用的是否碰撞怪物边界等原理)》
文章链接:https://www.456zj.com/41033.html
本站资源仅供个人学习交流,请于下载后24小时内删除,不允许用于商业用途,否则法律问题自行承担。

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址