gpt4 book ai didi

lua - 矩形碰撞系统在 love2d 中不起作用

转载 作者:行者123 更新时间:2023-12-04 19:41:35 24 4
gpt4 key购买 nike

我正在尝试在 Love2d 框架中创建用于管理简单游戏中的对象和碰撞的基础架构。所有对象都存储在一个表 (objects:activeObjects) 中,然后 objects:calculateCollisions() 函数中的循环遍历所有对象。在每次迭代中,另一个嵌套循环会检查该对象是否与同一表中的任何其他对象重叠。在 objects:calculateCollisions() 结束时,理想情况下每个对象都有一个表,其中包含对当前时间点与其重叠的所有对象的引用。然而,对象总是有空的碰撞表。

现在有两个测试对象:一个随鼠标移动,一个始终停留在右上角。对于用户来说,这两个对象在重叠时应该同时消失,但是,如前所述,collidingObjects 表始终为空。

我有三个源文件:
main.lua:
http://pastebin.com/xuGBSv2j
objects.lua(写了大部分重要的东西,也可能是问题所在):
http://pastebin.com/sahB6GF6
customObjects.lua(其中定义了两个测试对象的构造函数):

function objects:newCollidingObject(x, y)
local result = self:newActiveObject(x, y, 50, 50)
result.id = "collidingObject"
function result:collide(other)
if other.id == "collidingObject" then self.remove = true end
end
return result
end
function objects:newMovingObject(x, y)
local result = self:newCollidingObject(x, y)
function result:act()
self.x = love.mouse.getX()
self.y = love.mouse.getY()
end
return result
end

抱歉,我不能发布两个以上的超链接。

编辑:经过更多调试后,我将问题缩小到 collidesWith(obj) 函数。它似乎总是返回 false。
这是代码:

function result:collidesWith(obj)
if self.bottom < obj.top then return false end
if self.top > obj.bottom then return false end
if self.right < obj.left then return false end
if self.left > obj.right then return false end
return true
end

最佳答案

使用几个不同的测试输入在纸上浏览并追踪逻辑。你很快就会发现你的逻辑是荒谬的。你错过了一半的测试,你的一些比较“指向错误的方向”,等等。所以:

function result:collidesWith(obj)
if self.bottom <= obj.top and self.bottom >= obj.bottom then return true end
if self.top >= obj.bottom and self.top <= obj.top then return true end
if self.right >= obj.left and self.right <= obj.right then return false end
if self.left <= obj.right and self.left >= obj.left then return false end
return false
end

应该可以解决问题。

关于lua - 矩形碰撞系统在 love2d 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12336920/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com