游戏开发 - 在跳跃期间遇到AABB碰撞检测问题。
2015-12-15 17:48:45
收藏:0
阅读:98
评论:1
我的碰撞检测通过获取矩形之间的交集并反转效果来工作。这是在每帧期间发生的。它运行得很好,除非玩家坐在角落里并跳跃。偶尔情况下,垂直交集大于水平交集,这使我的玩家沿着平台的侧面滑落。有什么建议吗?
该段代码用于检测玩家和平台之间的碰撞,并根据碰撞位置反转效果。其中可能出现的缺陷是在玩家站在平台角落并进行跳跃时,垂直交集可能大于水平交集,从而导致玩家沿着平台的侧面滑落。
点赞
评论区的留言会收到邮件通知哦~
推荐文章
- 如何将两个不同的lua文件合成一个 东西有点长 大佬请耐心看完 我是小白研究几天了都没搞定
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?
- addEventListener 返回 nil Lua
- Lua中获取用户配置主目录的跨平台方法
很难从短片中确定,但我认为问题是由于检查最小交叉点而不是检查对象速度方向的交叉点导致的。您可以尝试这样做,以取代
smallestBoundary循环:local boundary = nil if (player.velocity.y > 0) then boundary = topBoundary elseif (player.velocity.y < 0) then boundary = bottomBoundary elseif (player.velocity.x > 0) then boundary = rightBoundary elseif (player.velocity.x < 0) then boundary = leftBoundary end当然,这不是最为强健的方法。您还可以尝试将这个方法和另一个方法相结合,用以下内容替换
smallestBoundary循环:local yMod = math.abs(player.velocity.y) local xMod = math.abs(player.velocity.x) local topMod = player.velocity.y > 0 and yMod or 1 local bottomMod = player.velocity.y < 0 and yMod or 1 local rightMod = player.velocity.x > 0 and xMod or 1 local leftMod = player.velocity.x < 0 and xMod or 1 local boundaries = { bottom = (bottomMod / MAX_VELOCITY) * bottomBoundary, top = (topMod / MAX_VELOCITY) * topBoundary, right = (rightMod / MAX_VELOCITY) * rightBoundary, left = (leftMod / MAX_VELOCITY) * leftBoundary } for direction, boundary in pairs(boundaries) do if not smallestBoundary then smallestBoundary = boundary smallestDist = direction end if smallestBoundary > boundary then smallestBoundary = boundary smallestDist = direction end end这将做到现在正在做的事情,但将根据它们的速度在该方向上调整界限的大小(仅在比较上下文中)。因此,如果您向
x移动并具有-5,并且向y移动并具有-10,那么在y平面中的向下碰撞将具有左侧碰撞的两倍重量。当然,还有另一种选择,即在每个碰撞平面中调整玩家:if bottomBoundary > 0 then player.desiredPos:add(diffX, -bottomBoundary) player.velocity.y = 0 player.onGround = true end if topBoundary > 0 then player.velocity.y = 250 player.desiredPos:add(0, topBoundary) end if rightBoundary > 0 then player.desiredPos:add(-rightBoundary, 0) end if leftBoundary > 0 then player.desiredPos:add(leftBoundary, 0) end这最后一种方法是最有意义的,但是您似乎无法均匀处理所有方向上的碰撞,因此可能不适合您的架构。
请记住,我不熟悉您正在使用的框架,因此此代码可能不会立即起作用。此外,此帖子假定
+y为上,-y为下,+x为右,-x为左。