碰撞检测自定义草图形状,表示为点列表
我有一组由用户绘制的点。他们将围绕一些物体进行绘画。
我需要以某种方式将这组点变成一个形状,这样我就可以找到检测碰撞的区域。
图像将澄清:
以形状表示的点集 http://www.imagechicken.com/uploads /1277188630025178800.jpg 。
到目前为止,我最好的想法是迭代每个像素,确定它是在形状的“内部”还是“外部”,但这会非常慢,而且我什至不知道如何确定“内部”/ “外面”位...
有什么提示吗?我正在使用 .NET(C# 和 XNA),如果这对你有帮助的话,请帮助我!
I have a set of points, drawn by the user. They will be drawing around some objects.
I need to somehow turn this set of points into a shape, so I can find the area to detect collisions.
An image will clarify:
Set of points represented as shape http://www.imagechicken.com/uploads/1277188630025178800.jpg
.
The best idea I have had so far involves iterating over every pixel determining if it is 'inside' or 'outside' the shape, but that would be horribly slow, and I'm not even sure how to do the determining 'inside'/'outside' bit...
Any hints? I am using .NET (C# and XNA) if that helps you help me!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以将您的形状视为多个形状的联合,每个形状都是一个简单的闭合多边形。
按以下方式检查每个对象是否位于任何多边形内:
所有点均由线连接 - 每条线都有一个定义它的方程。
对于每个对象 - 为穿过该对象的直线建立一个方程。
现在 - 对于每个对象方程,您需要检查有多少条线(点之间的线)与该对象方程相交 - 但仅计算两个点之间的交点(而不是在线之外的其余部分)两个点)并且仅位于对象一侧的交点(选择一侧 - 无关紧要)。
如果计数为偶数,则物体位于形状外部,否则位于形状内部。
You can think of your shape as an union of several shapes each of which is a simple closed polygon.
the check for every object if it is inside any of the polygons in the following manner:
All dots connected by lines - each line has an equation defining it.
For every object - build an equation for a line passing through this object.
now - for each object equation you need to check how many lines (those between the dots) intersects this object equation - but count only the intersection points that are in the rage between the two dots (and not in the rest of the line outside the two dots) and only the intersection points that are in one side of the object (pick a side - doesn't matter).
If the count is even - the object is outside the shape - otherwise it is inside.
只是我要说的任何事情的先兆,我在这个领域没有经验,这就是我解决问题的方式。
许多游戏为此使用的一种策略称为“Hit Boxes”。检测一个点是否在正方形内比检测任何其他图形要容易得多。但这并不能给你一个精确的碰撞,它可能就在你想要的物体之外。
我以前见过使用碰撞“气泡”。 这里是我为您找到的链接。这解释了主机游戏《任天堂明星大乱斗》中碰撞气泡的使用。
给定一个点、距离公式和半径,您可以轻松实现碰撞气泡。
为了向前迈出一步,我做了一点研究,我看到了一个漂亮的小算法(比前两个建议更先进),“凸物体的吉尔伯特-约翰逊-科尔蒂碰撞检测算法”。 这里是给您的链接。提供的实现是用 D 编写的。如果您使用 C# 工作,翻译起来应该不会太难(我强烈建议也消化该算法)。
希望这能给您一些指导。
Just a precursor to anything I will say, I have no experience in this field, this is just how I would go about the problem.
A tactic a lot of games use for this is known as Hit Boxes. It is much easier to detect if a point is inside a square than any other figure. But this doesn't give you an exact collision, it could be right outside your desired object.
I've seen Collision 'Bubbles' used before. Here is a link I found for you. This explains the use of Collision Bubbles in the console game Super Smash Brothers.
Given a point, the distance formula, and a radius, you can easily implement collision bubbles.
To take it even one step forward, I did a little bit of research, I saw a nifty little algorithm (more advanced that the top two suggestions), the "Gilbert-Johnson-Keerthi Collision detection algorithm for convex objects." Here is a link for ya. The implementation provided is written in D. If your working in C# it shouldn't be too hard to translate (I would highly suggest digesting the algorithm too).
Hope this gives you some direction.
好吧,多亏了另一个论坛上的一些帮助,我才成功。
我使用 GraphicsPath 类来完成所有艰苦的工作。
这就是我的方法最终的样子:
感谢你们俩的建议
Well I got it working thanks to some help on another forum.
I used the
GraphicsPath
class to do all the hard work for me.This is what my method ended up looking like:
Thanks for your suggestions both of you