Objective-C 绘制一条路径并检测它何时闭合(形成闭合形状)

发布于 2024-10-19 23:38:22 字数 252 浏览 1 评论 0原文

我对游戏编程相当陌生(但不是编程),我想创建一艘在屏幕上留下痕迹的太空飞船。现在我的问题是提出一个解决方案,如何检测从船留下的痕迹是否形成闭合形状 - 例如。如果船在某个物体周围留下了痕迹,那么可以说该物体就会被捕获在其痕迹内。

我正在考虑的方向是在屏幕上不可见的图像上绘制路径的路径,然后时不时地尝试用某种颜色填充它,然后检查填充是否被捕获在路径路径内。然而,这似乎是一个很大的开销。

有什么想法如何做到这一点吗?我正在使用 cocos2d 如果有帮助的话

I'm fairly new to game programming (but not to programming) and I want to create a space ship which leaves a trail on the screen. Now my problem is to come up with a solution how to detect if the trail left from the ship forms a closed shape - eg. if the ship left a trail around an object, the object is caught inside its trail so to speak.

The direction I'm thinking is to draw the path of the trail on an image not visible on the screen and every now and then try to fill it with certain color and then check if fill is caught within the trail path. However it seems like a lot of overhead.

Any ideas how to do that? I'm using cocos2d if that's of any help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

时常饿 2024-10-26 23:38:22

在游戏编程中,您通常需要更多地进行数学思考而不是视觉思考。

首先,你的船会在屏幕上不断留下痕迹吗?如果是,那么就更容易知道形状何时闭合:您只需记住您的船开始留下轨迹的坐标,然后等待轨迹再次接近该坐标(例如在半径 10 内)像素,否则用户将需要非常准确地击中完全相同的像素来闭合形状)。

轨迹的视觉表示仅供用户使用,您永远不会用它来计算任何东西。您要做的就是在内存中保存船舶轨迹所遵循的路径:一个多边形,它只不过是它所遵循的坐标列表。

然后,在知道形状是闭合的之后,您必须确定对象是否在多边形内部。 Objective-C或cocos2d(我对此不太了解)可能已经包含一个内置函数来知道一个点是否在多边形内部。在java中,有Polygon类,这使得这变得非常容易。如果你找不到任何可以自己做的事情,SO 上已经有关于这个主题的很好的答案,这是一个很好的答案:如何确定 2D 点是否在多边形内?

In game programming you often need to think more mathematically than visually.

First does your ship continuously leaves a trail on the screen? If yes, then it will be easier to know when the shape closes : you just have to remember the coordinate where your ship started to leave a trail, then wait for the trail to approach this coordinate another time (for example within a radius of 10 pixels, or else the user will need to be really accurate to hit exactly the same pixel to close the shape).

The visual representation of the trail is only here for the user, you'll never use it to compute anything. What you will do is to keep in memory the path followed by the ship's trail : a polygon, which is nothing else than the list of coordinates it followed.

Then after you know that your shape is closed, you have to determine if an object is inside your polygon or not. It's possible that objective-c or cocos2d (I don't know much about it) already contains a built-in function to know if a point is inside a polygon. In java there is the Polygon class which makes this really easy. If you don't find anything you can do it yourself, there are already great answers about this subject on SO, here is a nice one : How can I determine whether a 2D Point is within a Polygon?

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