检测非矩形形状的点击 [英] Detecting clicks in a non-rectangular shape

查看:34
本文介绍了检测非矩形形状的点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 XNA 4.0 还很陌生,但有一个我似乎无法弄清楚的问题.到目前为止,我一直通过创建矩形并检查 rectangle.contains 方法来检测鼠标点击,但是如果我不想检查矩形内部怎么办?

I am pretty new to XNA 4.0 and have a problem I can't seem to figure out. So far I have been detecting mouse clicks by creating rectangles and checking the rectangle.contains method, but what if I don't want to check inside a rectangle?

在我的地图的一部分中,我有一个三角形按钮,我想让用户点击它,但如果它实际上在三角形内,我只想将其计为一次点击.如果我在三角形周围创建一个矩形,如果您知道我的意思,用户将能够在纹理区域外单击.

In one part of my map I have a triangle button that I want to let the user click, but I want to only count it as a click if it is actually inside the triangle. If I create a rectangle around the triangle the user will be able to click outside the texture area, if you know what I mean.

我该怎么做?

推荐答案

您将不得不使用 Point在多边形算法中用于更复杂的形状:

You will have to use a Point in Polygon Algorithm for more complex shapes:

检查凸多边形(您的三角形)比检查凹多边形更简单(请参阅第一篇链接文章).

The check for convex polygons (your triangle) is simpler than for concave ones (see first linked article).

如果您必须进行大量检查并达到性能限制,请考虑使用某种层次结构,四叉树LOD系统.例如,您可以为非常复杂的多边形计算一个额外的边界矩形,如果点位于矩形内,则只对多边形进行昂贵的检查.

If you have to do lots of checks and are hitting performance limits, consider using some kind of hierarchy, a Quadtree or an LOD system. For example, you can calculate an additional bounding rectangle for very complex polygons and only do the expensive in-check with the polygon if the point lies inside the rectangle.

这篇关于检测非矩形形状的点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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