在我的平台游戏上执行 Frustum Culling 时需要帮助

Need help on performing Frustum Culling on my platform game

为了实现 Frustum Culling 算法,我正在尝试做的是从取决于玩家 x 和 y 位置的位置开始 render() 函数中的嵌套 for 循环,这样代码只会循环遍历 .tmx 文件的一小部分必须呈现的部分。 现在的问题是,如何从取决于我的玩家坐标的位置开始循环? 在此先感谢您的帮助。

import pygame
import pytmx
pygame.init()

class Map():
    def __init__(self,filename):
        tm=pytmx.load_pygame(filename,pixelalpha=True)
        self.width=tm.width * tm.tilewidth
        self.height=tm.height*tm.tileheight
        self.tmxdata=tm

    def render(self,surface):
        ti=self.tmxdata.get_tile_image_by_gid
        for layer in self.tmxdata.visible_layers:
            if isinstance(layer,pytmx.TiledTileLayer):
                for x,y,gid in layer:
                    tile = ti(gid)
                    if tile
                        surface.blit(tile,(x*self.tmxdata.tilewidth,y*self.tmxdata.tileheight))
    def make_map(self):
        temp_surface=pygame.Surface((self.width,self.height))
        self.render(temp_surface)
        return temp_surface

如果瓦片地图大小是恒定的(即每个瓦片具有相同的宽度和高度),您可以计算世界上某个点的当前瓦片地图坐标。

假设我们有 20 像素宽和 20 像素高的图块,并且您的播放器位于位置 (432, 36)。然后我们可以通过整数除法(除法和截断/"remove the decimals")找到he/she所在的瓦片。

tile_x = player_x // tile_width
tile_y = player_y // tile_height

在我们的例子中,玩家将站在 x 轴 (432 // 20 = 21) 的 21 方块上,以及 y 轴 (36 // 20 = 1) 的方块 1 上。

从那里你可以定义你希望在玩家周围有多大的区域可见。如果您想在每个方向(包括玩家站立的方向)渲染 1 个图块,则必须遍历 x = 20 to 22y = 0 to 2.