计算网格上的实体位置(笛卡尔坐标)(实体和地砖的大小不同)

问题描述 投票:0回答:1

[我正在尝试编写一个函数,该函数知道[地面瓦片大小]与[玩家瓦片大小]不同而知道哪个瓦片正在踩我的玩家。

我想避免不惜一切代价遍历整个地图

不幸的是,我没有那么高的数学水平,这意味着我不知道如何做到这一点。

我正在像任何经典RPG一样使用2D经典笛卡尔坐标系。

我试图计算我的播放器所在的第一个磁贴,然后我迷路了。我无法做更多。

我做了类似i = math.floor(entity.x / tile size的操作)与j相同

则索引=(i + 1)+ j * nbTilesPerRow

实际结果:

没什么。瓦片仍然未被检测到,即使我从上述公式中获得的[瓦片索引]。
function Map:ClickedTile( mouseX, mouseY )
    local i = math.floor( mouseX / self.tile.size )
    local j = math.floor( mouseY / self.tile.size )

    local index = i + j * self.tile.width

    return index
end

此代码有效,并为我提供了我单击的图块索引。

我想复制此内容以找出播放器当前正在与之碰撞的所有图块,同时避免循环遍历整个地图。

样本数据:

tile size = 32 * 32
player tile size = 64 * 64
map size : 25 * 19

感谢您阅读我。

[我正在尝试编写一个函数,告诉我知道[地面瓦片大小]与[玩家瓦片大小]不同,我要避免在哪个瓦片上踩我的玩家,我想避免在整个地图上循环播放...] [

如果我理解正确,您希望将地图的图块放在正方形{x = entity.x,y = entity.y,size = 64}

让所有职位:

function getPosition(map, square) local xpos = {} for i = square.x, square.x + square.size, map.tile.size do table.insert(xpos, math.floor(i / map.tile.size)) end local ypos = {} for i = square.y, square.y + square.size, map.tile.size do table.insert(ypos, math.floor(i / map.tile.size)) end return xpos, ypos end

在地图上正方形相交的位置上,您拥有所有{x,y}的位置(例如,对于位置square = {x = 33,y = 33,size = 64},您会得到xpos = {1,2,3})

然后您必须将x,y坐标转换为索引:

function positionToIndex(positions, mapWidth) local indexes = {} for position in ipairs(positions) do local index = i + j * mapWidth table.insert(indexes, index) end return indexes end local xpos, ypos = getPosition(map, entity) local position = listOfCoordinate(xpos, ypos) -- should be straighforward local intersectionIndexes = positionToIndex(position, map.tile.size)

您需要解决一些缺陷:

    当玩家正好在图块上时,会发生for循环是包含边界({x = 0,y = 0,size = 64},他是否触摸图块0,1,2或0,1吗?)
  • 这些数学用于0索引数组
  • 通过宽度/高度更改大小以适用于矩形
lua 2d tile love2d
1个回答
0
投票
如果我理解正确,您希望将地图的图块放在正方形{x = entity.x,y = entity.y,size = 64}

让所有职位:

© www.soinside.com 2019 - 2024. All rights reserved.