碰撞检测自定义草图形状,表示为点列表

发布于 2024-09-06 16:43:11 字数 407 浏览 9 评论 0原文

我有一组由用户绘制的点。他们将围绕一些物体进行绘画。

我需要以某种方式将这组点变成一个形状,这样我就可以找到检测碰撞的区域。

图像将澄清:

以形状表示的点集 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

总攻大人 2024-09-13 16:43:11

您可以将您的形状视为多个形状的联合,每个形状都是一个简单的闭合多边形。
按以下方式检查每个对象是否位于任何多边形内:
所有点均由线连接 - 每条线都有一个定义它的方程。
对于每个对象 - 为穿过该对象的直线建立一个方程。
现在 - 对于每个对象方程,您需要检查有多少条线(点之间的线)与该对象方程相交 - 但仅计算两个点之间的交点(而不是在线之外的其余部分)两个点)并且仅位于对象一侧的交点(选择一侧 - 无关紧要)。
如果计数为偶数,则物体位于形状外部,否则位于形状内部。

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.

等数载,海棠开 2024-09-13 16:43:11

只是我要说的任何事情的先兆,我在这个领域没有经验,这就是我解决问题的方式。

许多游戏为此使用的一种策略称为“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.

与酒说心事 2024-09-13 16:43:11

好吧,多亏了另一个论坛上的一些帮助,我才成功。

我使用 GraphicsPath 类来完成所有艰苦的工作。

这就是我的方法最终的样子:

public bool IsColliding(Vector2 point)
{
    GraphicsPath gp = new GraphicsPath();

    Vector2 prevPoint = points[0];
    for (int i = 1; i < points.Count; i++)
    {
        Vector2 currentPoint = points[i];

        gp.AddLine(prevPoint.X, prevPoint.Y, currentPoint.X, currentPoint.Y);

        prevPoint = currentPoint;
    }
    gp.CloseFigure();   //closing line segment

    return gp.IsVisible(point.X, point.Y);
}

感谢你们俩的建议

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:

public bool IsColliding(Vector2 point)
{
    GraphicsPath gp = new GraphicsPath();

    Vector2 prevPoint = points[0];
    for (int i = 1; i < points.Count; i++)
    {
        Vector2 currentPoint = points[i];

        gp.AddLine(prevPoint.X, prevPoint.Y, currentPoint.X, currentPoint.Y);

        prevPoint = currentPoint;
    }
    gp.CloseFigure();   //closing line segment

    return gp.IsVisible(point.X, point.Y);
}

Thanks for your suggestions both of you

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文