生成 2D 游戏世界的技术

发布于 2024-08-23 05:32:41 字数 386 浏览 5 评论 0原文

我想使用 Irrlicht 引擎用 C++ 制作 2D 游戏。在这个游戏中,你将在某种洞穴中控制一艘小船。这个洞穴将自动创建(游戏将有随机级别),看起来像这样:

Cave

假设我已经有了洞穴内部多边形的点(白色部分)。我应该如何在屏幕上渲染这个形状并将其用于碰撞检测?根据我在不同站点阅读的内容,我应该使用三角测量算法,使用洞穴内部的多边形(白色部分)来制作洞穴墙壁(黑色部分)的网格。然后,我还可以使用这些网格进行碰撞检测。这真的是最好的方法吗?你知道 Irrlicht 是否有一些内置功能可以帮助我实现这一目标吗?

任何建议将不胜感激。

I want to make a 2D game in C++ using the Irrlicht engine. In this game, you will control a tiny ship in a cave of some sort. This cave will be created automatically (the game will have random levels) and will look like this:

Cave

Suppose I already have the the points of the polygon of the inside of the cave (the white part). How should I render this shape on the screen and use it for collision detection? From what I've read around different sites, I should use a triangulation algorithm to make meshes of the walls of the cave (the black part) using the polygon of the inside of the cave (the white part). Then, I can also use these meshes for collision detection. Is this really the best way to do it? Do you know if Irrlicht has some built-in functions that can help me achieve this?

Any advice will be apreciated.

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

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

发布评论

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

评论(3

李不 2024-08-30 05:32:41

描述如何使用给定的 3D 引擎渲染任意多边形形状是一个相当漫长的过程。可以说,几乎所有 3D 渲染都是根据三角形完成的,如果您没有使用工具来生成已经由三角形组成的模型,那么您将需要根据现有的任何数据来生成三角形。是的,对黑色空间或白色空间进行三角测量可能是最好的方法。然后,您可以从中构建网格或顶点列表,并以这种方式渲染这些三角形。然后,列表中的三角形也会加倍以进行碰撞检测。

我怀疑 Irrlicht 有任何三角测量的东西,因为它是针对你的游戏设计的,而不是大多数人会采用的通用方法。 (通常他们会有一个工具,允许并排生成游戏几何图形和导航几何图形。)考虑到您在那里的形状,看起来这可能相当棘手。

Describing how to get an arbitrary polygonal shape to render using a given 3D engine is quite a lengthy process. Suffice to say that pretty much all 3D rendering is done in terms of triangles, and if you didn't use a tool to generate a model that is already composed of triangles, you'll need to generate triangles from whatever data you have there. Triangulating either the black space or the white space is probably the best way to do it, yes. Then you can build up a mesh or vertex list from that, and render those triangles that way. The triangles in the list then also double up for collision detection purposes.

I doubt Irrlicht has anything for triangulation as it's quite specific to your game design and not a general approach most people would take. (Typically they would have a tool which permits generation of the game geometry and the navigation geometry side by side.) It looks like it might be quite tricky given the shapes you have there.

余生共白头 2024-08-30 05:32:41

一种选择是直接使用贴图(图像蒙版)来测试碰撞。

例如,

if map_points[sprite.x sprite.y] is black then
   collision detected

假设您的对象是图像,并且它们不是真正的多边形。

如果您使用真实的多边形,您可以为每个对象形状提供一个“点样本”,
并检查样品是否发生碰撞。

One option is to use the map (image mask) directly to test for collision.

For example,

if map_points[sprite.x sprite.y] is black then
   collision detected

assuming that your objects are images and they aren't real polygons.

In case you use real polygons you can have a "points sample" for every object shape,
and check the sample for collisions.

ぺ禁宫浮华殁 2024-08-30 05:32:41

要检查某个点是在多边形内部还是外部,您只需计算交叉点即可。您知道 (0,0) 在多边形之外。现在从那里画一条线到您的测试点 (X,Y)。如果这条线穿过奇数条多边形边(例如 1 条),则它位于多边形内部。如果该线穿过偶数条边(例如 0 或 2 条),则点 (X,Y) 位于多边形外部。在纸上运行这个算法一次以说服自己是很有用的。

To check whether a point is inside or outside your polygon, you can simply count crossings. You know (0,0) is outside your polygon. Now draw a line from there to your test point (X,Y). If this line crosses an odd number of polygon edges (e.g. 1), it's inside the polygon . If the line crosses an even number of edges (e.g. 0 or 2), the point (X,Y) is outside the polygon. It's useful to run this algorithm on paper once to convince yourself.

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