如何计算游戏板上的孔位置?

发布于 2024-08-21 17:02:10 字数 1351 浏览 2 评论 0原文

我正在使用 Python->PyGame->Albow 制作游戏,并遇到了棋盘生成问题。不过,我将尝试以与语言无关的方式解释该问题。我相信这与Python无关。

我将游戏板生成分为几个部分。

第一部分生成板孔。

孔包含在列表/数组中。每个孔对象都有与其周围其他孔相关的角度映射,每个孔也链接回它。 (有点像 HTML DOM 兄弟姐妹,区别在于任何角度都是可能的)

孔类似于:

hole = {
    empty: True,
    links: {
        90: <other hole>,
        270: <another hole>,
        etc...
    }
}

第二部分,计算孔位置。 代码是这样的。

def calculate_position(hole):
    for linked_hole in hole.links:
        if linked_hole.position == None:
            #calculate linked hole's position relative to this hole
            linked_hole.position = [position relative to first hole]
            calculate_position(linked_hole)

first_hole.position = (0, 0) #x, y
calculate_position( first_hole )

第三部分,画板。

找到窗户的高度,扩大孔的位置(在第二步中计算)以适合窗户。画出一切。

我相信问题出在第二步中,我正在计算每个洞相对于前一个洞的情况。舍入误差加起来,板子会变成斜视形状,孔离起始孔越远,板子就越大。仅当制作非矩形板时才会发生这种情况,因为否则不会出现舍入错误。

我使用简单的三角学通过将角度转换为弧度并使用内置的 sin/cos 函数来计算孔的相对位置。

关于解决方案的任何想法,或者如果我对问题有误解,都是有用的:)

PS:如果它有帮助,我会发布源代码,但感觉它会让事情变得混乱< /em>

感谢所有的答案。

那些说四舍五入可能不会成为问题的人说得对。考虑到这一点,我再次查看了代码。我很尴尬地说我在板生成的第一部分中生成了错误的角度,渲染部分是正确的。

我已将诺曼的答案标记为正确,因为它解释了如何使用向量的线性组合来解决问题。

I'm making a game with Python->PyGame->Albow and ran into a problem with board generation. However I'll try to explain the problem in a language agnostic way. I believe it's not related to python.

I've split the game board generation into several parts.

Part one generates the board holes.

Holes are contained in a list/array. Each hole object has a mapping of angles relating to other holes which are surrounding it, each of those holes also links back to it. (Sort of like HTML DOM siblings, the difference being any angle is possible)

A hole is something like:

hole = {
    empty: True,
    links: {
        90: <other hole>,
        270: <another hole>,
        etc...
    }
}

Part two, calculate hole positions.
The code is something like this.

def calculate_position(hole):
    for linked_hole in hole.links:
        if linked_hole.position == None:
            #calculate linked hole's position relative to this hole
            linked_hole.position = [position relative to first hole]
            calculate_position(linked_hole)

first_hole.position = (0, 0) #x, y
calculate_position( first_hole )

Part three, draw board.

Find the window height, expand the positions of holes (calculated in step two) to fit the window. Draw everything.

I believe that the problem is in step two I am calculating every hole relative to a previous hole. Rounding errors add up and the board goes squint shaped the further away from the starting hole the holes are and the bigger the board is. This only happens when making boards that aren't rectangular because otherwise there aren't rounding errors.

I am using simple trigonometry to calculate the relative positions of holes by converting the angle into radians and using built in sin/cos functions.

Any idea as to a solution or if I'm mistaken as to the problem is useful :)

PS: I will post the source code if it would help however feel it will clutter things up

Thanks for all the answers.

The people who said rounding probably wasn't going to be an issue were spot on. I had another look through the code with that in mind. I'm embarrassed to say I was generating the wrong angles in the first part of the board generation, the rendering part was correct.

I've marked Norman's answer as correct because it explains how to use a linear combination of vectors to solve the problem.

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

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

发布评论

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

评论(3

烙印 2024-08-28 17:02:10

如果孔位置存储为整数,我毫不怀疑舍入误差累积得足够快,足以杀死你。如果孔位置存储为浮点,并且每次计算时最后一个位置 (ULP) 都有一个单位的误差,我不太确定误差累积的速度有多快,但如果误差在每一步加倍,那么你最多有 53 个链接,即使是双精度浮点也会出错。

如果你想绝对准确,我会将每个位置表示为向量的线性组合。您可以用角度来表示每个向量,并且只有几个角度,因此您可以将孔的位置表示为

走六个 30 度台阶、两个 90 度台阶和四个 180 度台阶

数字 6、2 和 4 将精确,一旦您将所有位置计算为向量,您就可以然后执行三角函数将坐标一次性转换为 (x, y) 坐标。如果你担心速度,你可以缓存每个角度的反正切,它甚至会很快。

如果这个描述太简洁,请告诉我。

If hole positions are stored as integers, I don't doubt rounding error accumulates quickly enough to kill you. If hole positions are stored as floating point, and if you have an error of one unit in the last place (ULP) at each computation, I'm not quite sure how quickly error accumulates—but if error doubles at each step, then you have at most 53 links before even double-precision floating point would go wrong.

If you want to be rock-solid accurate, I would represent each position as a linear combination of vectors. You can represent each vector by its angle, and you have just a few angles, so you can represent the position of a hole as something like

Take six 30-degree steps and two 90-degree steps and four 180-degree steps

The numbers six, two, and four will be exact, and once you've computed all positions as vectors, you can then do the trig to convert to (x, y) coordinates all at one go. If you're worried about speed you can cache the arctangent of each angle and it will even be fast.

If this description is too terse, let me know.

╭⌒浅淡时光〆 2024-08-28 17:02:10

一旦我们意识到这些点将被转换为像素坐标(又称整数),关于准确性的问题就变得相对重要。累积 0.5 的误差,然后砰!您落后一个像素。

因此,要么精度存在巨大问题并且舍入误差增长得非常非常快,要么问题的根源在其他地方。我正在特别考虑这一点:

扩展孔的位置(在第二步中计算)以适合窗口

在我看到屏幕之前,我会假设“眯眼”意味着“椭圆形的某种东西”;听起来正是这一步中的错误可能产生的结果。

The bit about accuracy becomes relatively important as soon as we realize these points are going to be converted to pixel coordinates, a.k.a. integers. Accumulate an error of 0.5 and bam! You're one pixel off.

So, either there is a huge problem with accuracy and rounding errors are climbing very very fast, or the source of the issue is elsewhere. I'm looking at this in step in particular:

expand the positions of holes (calculated in step two) to fit the window

Until I see a screenie, I'll assume "squint" means 'oval-kinda-sorta-thing'; sounds exactly what a bug in this step could produce.

南冥有猫 2024-08-28 17:02:10

我不想成为提出这一建议的人,但是,从中心开始。另外,您应该查看您的代码并仔细检查是否存在不幸的转换。也就是说,如果一个洞的结果为“138.2, 150.8”,您需要保留小数部分,直到计算出下一个洞。

I hate to be the one to suggest this, but, start in the the center. Also, you should look at your code and double check for an unfortunate conversion. The is, if a hole ends up at "138.2, 150.8", you need to keep the fractional parts until you have computed the next hole.

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