如何在Love2d中使用包围盒?
2016-11-11 5:41:6
收藏:0
阅读:79
评论:1
目前我使用了非常笨重的代码来检测简单对象之间的碰撞。我听说过包围盒,但是找不到任何关于如何使用它的教程,所以我想问一下如何使用它。以下是我目前检测碰撞的方式:
function platform.collision()
if player.x + player.width / 2 <= platform.x + platform.width and
player.x + player.width / 2 >= platform.x and
player.y + player.height <= platform.y + platform.height and
player.y + player.height >= platform.y then
点赞
评论区的留言会收到邮件通知哦~
推荐文章
- Lua 虚拟机加密load(string.dump(function)) 后执行失败问题如何解决
- 我想创建一个 Nginx 规则,禁止访问
- 如何将两个不同的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 代码?

MDN 上有一篇简洁的关于2D 碰撞检测的文章。作为 MDN,示例是用
javascript写的,但很容易翻译成任何语言,包括 Lua。让我们来看看:
他们的示例被翻译成了 Lua:
local rect1 = { x = 5, y = 5, width = 50, height = 50 } local rect2 = { x = 20, y = 10, width = 10, height = 10 } if rect1.x < rect2.x + rect2.width and rect1.x + rect1.width > rect2.x and rect1.y < rect2.y + rect2.height and rect1.height + rect1.y > rect2.y then -- 碰撞检测到! end -- 填充值 => if 5 < 30 and 55 > 20 and 5 < 20 and 55 > 10 then -- 碰撞检测到! end一个 JavaScript 实时示例 很好地展示了这一点。
这里有一个可以放入
main.lua中并进行调试的快速(但不完美)的 Love2D 示例。local function rect (x, y, w, h, color) return { x = x, y = y, width = w, height = h, color = color } end local function draw_rect (rect) love.graphics.setColor(unpack(rect.color)) love.graphics.rectangle('fill', rect.x, rect.y, rect.width, rect.height) end local function collides (one, two) return ( one.x < two.x + two.width and one.x + one.width > two.x and one.y < two.y + two.height and one.y + one.height > two.y ) end local kp = love.keyboard.isDown local red = { 255, 0, 0, 255 } local green = { 0, 255, 0, 255 } local blue = { 0, 0, 255, 255 } local dim1 = rect(5, 5, 50, 50, red) local dim2 = rect(20, 10, 60, 40, green) function love.update () if kp('up') then dim2.y = dim2.y - 1 end if kp('down') then dim2.y = dim2.y + 1 end if kp('left') then dim2.x = dim2.x - 1 end if kp('right') then dim2.x = dim2.x + 1 end dim2.color = collides(dim1, dim2) and green or blue end function love.draw () draw_rect(dim1) draw_rect(dim2) end