python - 检测用户正在查看哪个立方体?

标签 python opengl math 3d pyopengl

我正在使用 python 制作类似于 Minecraft 的游戏。我有一个用户可以四处走动和环顾四周的世界,但我不知道如何制作它,以便他们可以打破和放置方 block 。

我需要知道如何从世界上的 block 的 3D 数组中计算他们正在查看的 block ( block ,格式:[[[a,b,c ],[d,e,f],[g,h,i]],[[j,k,l],[m,n,o],[p,q,r]],[[s,t ,u],[v,w,x],[y,z,0]]]),它们的位置 (x,y,z)和头部旋转(xrotyrot)。

我也只需要在距他们所在位置一定距离的地方使用它,也许是 5 个街区。我试图找到一条线的函数并遵循它,但没有成功,我在互联网上查找,但找不到我需要的东西。

我需要能够根据他们正在查看的一侧找出他们会破坏哪个 block 或新 block 将去往何处。

我需要找到我正在看的立方体的哪一面。这是我编写的代码,但某些数学运算肯定是错误的,因为它不起作用。

def get_looking_at(xrot, yrot, xpos, ypos, zpos, blocks, reach):
    xrot, yrot = math.radians(xrot), math.radians(yrot)
    xform = sin(xrot)*cos(yrot)+xpos
    yform = sin(yrot)+ypos
    zform = -(cos(xrot)*cos(yrot))+zpos
    xforward = xform-xpos >= 0
    yforward = yform-ypos >= 0
    zforward = zform-zpos >= 0
    if xforward:
        xset = [floor(x+xpos+.5)+.5 for x in range(reach)]
    else:
        xset = [floor((-x)+xpos+.5)-.5 for x in range(reach)]
    if yforward:
        yset = [ceil(y+ypos) for y in range(reach)]
    else:
        yset = [floor((-y)+ypos) for y in range(reach)]
    if zforward:
        zset = [floor(z+zpos+.5)+.5 for z in range(reach)]
    else:
        zset = [floor((-x)+xpos+.5)-.5 for x in range(reach)]
    xint = []
    yint = []
    zint = []
    for x in xset:
        y = ((yform-ypos)*x)/(xform-xpos)
        z = ((zform-zpos)*x)/(xform-xpos)
        xint.append((x, y+ypos, z+zpos))
    for y in yset:
        x = ((xform-xpos)*y)/(yform-ypos)
        z = ((zform-zpos)*y)/(yform-ypos)
        yint.append((x+xpos, y, z+zpos))
    for z in zset:
        x = ((xform-xpos)*z)/(zform-zpos)
        y = ((yform-ypos)*z)/(zform-zpos)
        zint.append((x+xpos,y+ypos,z))
    intercepts = dict()
    for pos in xint:
        intercepts[(pos[0]-xpos)**2+(pos[1]-ypos)**2+(pos[2]-zpos)**2] = (pos[0], pos[1], pos[2], "x")
    for pos in yint:
        intercepts[(pos[0]-xpos)**2+(pos[1]-ypos)**2+(pos[2]-zpos)**2] = (pos[0], pos[1], pos[2], "y")
    for pos in zint:
        intercepts[(pos[0]-xpos)**2+(pos[1]-ypos)**2+(pos[2]-zpos)**2] = (pos[0], pos[1], pos[2], "z")
    indices = [x for x in intercepts]
    indices.sort()
    for index in indices:
        connection = intercepts[index]
        if xforward:
            x = floor(connection[0]+.5)
            xdir = "e"
        else:
            x = ceil(connection[0]-.5)
            xdir = "w"
        if yforward:
            y = floor(connection[1])
            ydir = "d"
        else:
            y = floor(connection[1])+1
            ydir = "u"
        if zforward:
            z = ceil(connection[2]-.5)
            zdir = "n"
        else:
            z = floor(connection[2]+.5)
            zdir = "s"
        print(x,y,z)
        try:
            if blocks.get_data(x, y, z) != None:
                if math.sqrt(index) <= reach:
                    if connection[3] == "x":
                        return x, y, z, xdir
                    if connection[3] == "y":
                        return x, y, z, ydir
                    if connection[3] == "z":
                        return x, y, z, zdir
                else:
                    return
            else:
                continue
        except IndexError:
            continue
    return

最佳答案

您可以在玩家周围建立一个接触球体,并使用从玩家脸部“突出”的半径。

半径 r 是用户可以看到一个 block 并且仍然能够影响它的最大距离。

使用三角形,您可以检测半径的末端是否在 block 内,等等。

关于python - 检测用户正在查看哪个立方体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54816355/

相关文章:

python - Bokeh 中的网络图形 : displaying a node's connections when hovering over the node

Python 初始化结构体

opengl - 如何使用 GPU 在 Google Compute Engine 上运行 Gazebo?

c++ - OpenGL C++-太阳系

python - Python 中的指数语法

java - 使用多种编程语言(Python Java C#)构建用于开发 Web 应用程序的基础架构

python - Tensorflow — 使用 `tf.keras.Model.add_metric` 时无法调用 `tf.distribute.MirroredStrategy`

c++ - 对 VBO 中的特定三角形使用不同的纹理

javascript - 随机放置矩形,但不在中心 x 半径范围内

arrays - 4D张量旋转的优化