【Love2d从青铜到王者】第十二篇:Love2d之碰撞检测(Detecting collision)

x33g5p2x  于2022-05-06 转载在 其他  
字(3.7k)|赞(0)|评价(0)|浏览(585)

系列文章目录

前言

🍇一、图像(Images)

1️⃣.图像(Images)

  • 假设我们正在做一个游戏,在这个游戏中你可以击落怪物。怪物被子弹打中就应该死。所以我们需要检查的是:怪物是在和子弹相撞吗?
  • 我们要创造一个碰撞检查功能。我们将检查矩形之间的冲突。这被称为AABB碰撞。所以我们需要知道,两个矩形什么时候碰撞?
  • 我用三个例子创建了一个图像:

  • 如果你还没有开启程序员的大脑,现在是时候了。第三个例子中发生了什么,而第一个和第二个例子中没有发生?
  • “它们在碰撞”
  • 是的,但是你得说得更具体些。我们需要计算机可以使用的信息。
  • 看一下矩形的位置。在第一个例子中,红色没有与蓝色碰撞,因为红色太靠左了。如果红色稍微偏右一点,他们就会接触。具体有多远?嗯,红色的右边是比蓝色的左边更靠近右边。这对于例3来说是正确的
  • 但对于例2也是如此。我们需要更多的条件来确定是否有碰撞。例子2表明我们不能向右走太远。我们到底能走多远?红色要向左移动多少才会发生碰撞? 当红色的左边是比蓝色的右边更靠近左边
  • 所以我们有两个条件,足以保证有碰撞吗?
  • 不,看看下面的图片:

  • 这种情况符合我们的条件。红色的右边比蓝色的左边更靠右。红色的左侧比蓝色的右侧更靠左。然而,没有碰撞。那是因为红色太高了。它需要向下移动。多远?直到红色的底边比蓝色的顶边更靠近底边
  • 但是如果我们把它移得太低,就不会再有碰撞了。红色能下移多远,还能和蓝色碰撞?只要红色的顶边比蓝色的底边更靠近顶边
  • 现在我们有四个条件。这三个例子的四个条件都成立吗?

  • 红色的右边比蓝色的左边更靠近右边。
  • 红色的左边比蓝色的右边更靠近左边。
  • 红色的底边比蓝色的顶边更靠近底边。
  • 红色的顶边比蓝色的底边更靠近顶边。
  • 是的,他们是!现在我们需要将这些信息转化为一个函数。
  • 首先让我们创建两个矩形。
  1. function love.load()
  2. --创建两个矩形
  3. r1={
  4. x=10,
  5. y=100,
  6. width=100,
  7. height=100
  8. }
  9. r2={
  10. x=250,
  11. y=120,
  12. width=150,
  13. height=120
  14. }
  15. end
  16. function love.update(dt)
  17. --移动一个矩形
  18. r1.x=r1.x+100*dt
  19. end
  20. function love.draw()
  21. love.graphics.rectangle("line",r1.x,r1.y,r1.width,r1.height)
  22. love.graphics.rectangle("line",r2.x,r2.y,r2.width,r2.height)
  23. end

  • 现在我们创建一个名为 **checkCollision()**的新函数,用两个矩形作为参数。
  1. function checkCCollision(a,b)
  2. --对于局部变量,通常使用下划线而不是驼峰式大小写
  3. local a_left=a.x
  4. local a_right=a.x+a.width
  5. local a_top=a.y
  6. local a_bottom=a.y+a.height
  7. local b_left=b.x
  8. local b_right=b.x+b.width
  9. local b_top=b.y
  10. local b_bottom=b.y+b.height
  11. end
  • 现在我们有了每个矩形的四条边,我们可以用它们把我们的条件放在一个if语句中。

  1. function checkCollision(a, b)
  2. --With locals it's common usage to use underscores instead of camelCasing
  3. local a_left = a.x
  4. local a_right = a.x + a.width
  5. local a_top = a.y
  6. local a_bottom = a.y + a.height
  7. local b_left = b.x
  8. local b_right = b.x + b.width
  9. local b_top = b.y
  10. local b_bottom = b.y + b.height
  11. --If Red's right side is further to the right than Blue's left side.
  12. if a_right > b_left
  13. --and Red's left side is further to the left than Blue's right side.
  14. and a_left < b_right
  15. --and Red's bottom side is further to the bottom than Blue's top side.
  16. and a_bottom > b_top
  17. --and Red's top side is further to the top than Blue's bottom side then..
  18. and a_top < b_bottom then
  19. --There is collision!
  20. return true
  21. else
  22. --If one of these statements is false, return false.
  23. return false
  24. end
  • 注意,if条件本身是一个布尔值。 checkCollision()返回 true当if条件为true,反之亦然。
  1. function checkCollision(a, b)
  2. --With locals it's common usage to use underscores instead of camelCasing
  3. local a_left = a.x
  4. local a_right = a.x + a.width
  5. local a_top = a.y
  6. local a_bottom = a.y + a.height
  7. local b_left = b.x
  8. local b_right = b.x + b.width
  9. local b_top = b.y
  10. local b_bottom = b.y + b.height
  11. --Directly return this boolean value without using if-statement
  12. return a_right > b_left
  13. and a_left < b_right
  14. and a_bottom > b_top
  15. and a_top < b_bottom
  16. end
  • 好吧,我们有我们的功能。我们来试试吧!我们绘制矩形填充或线性的基础上。
  1. function love.load()
  2. --创建两个矩形
  3. r1={
  4. x=10,
  5. y=100,
  6. width=100,
  7. height=100
  8. }
  9. r2={
  10. x=250,
  11. y=120,
  12. width=150,
  13. height=120
  14. }
  15. end
  16. function love.update(dt)
  17. --移动一个矩形
  18. r1.x=r1.x+100*dt
  19. end
  20. function checkCCollision(a,b)
  21. --对于局部变量,通常使用下划线而不是驼峰式大小写
  22. local a_left=a.x
  23. local a_right=a.x+a.width
  24. local a_top=a.y
  25. local a_bottom=a.y+a.height
  26. local b_left=b.x
  27. local b_right=b.x+b.width
  28. local b_top=b.y
  29. local b_bottom=b.y+b.height
  30. if
  31. a_left<b_right and
  32. a_right>b_left and
  33. a_top<b_bottom and
  34. a_bottom>b_top
  35. then
  36. return true
  37. else
  38. return false
  39. end
  40. end
  41. function love.draw()
  42. --我们创建了一个名为mode的局部变量
  43. local mode
  44. if checkCCollision(r1,r2)
  45. then
  46. --如果有冲突,画出填充的矩形为填充模式
  47. mode="fill"
  48. else
  49. --否则,画出填充的矩形为线性模式
  50. mode="line"
  51. end
  52. love.graphics.rectangle(mode,r1.x,r1.y,r1.width,r1.height)
  53. love.graphics.rectangle(mode,r2.x,r2.y,r2.width,r2.height)
  54. end

  • 有用!现在你知道如何检测两个矩形之间的冲突。

🍈二、总结

  • 两个矩形之间的碰撞可以用四个条件来检查。
  • 其中A和B是矩形:
  • 红色的右边比蓝色的左边更靠近右边。
  • 红色的左边比蓝色的右边更靠近左边。
  • 红色的底边比蓝色的顶边更靠近底边。
  • 红色的顶边比蓝色的底边更靠近顶边。

🍋总结

以上就是今天要讲的内容,本文仅仅简单介绍了Love2d之碰撞检测(Detecting collision),介绍了碰撞检测(Detecting collision)的如何使用,与博主的lua语言文章结合更好的理解love2d的编码,如果你是一名独立游戏开发者,或者一位对游戏开发有着深厚兴趣,但是又对于unity3d,ue4等这些对于新手而言不太友好的引擎而头疼的开发者;那么现在,你可以试试Love2D。Love2D是一款基于Lua编写的轻量级游戏框架,尽管官方称呼其为引擎,但实际上它只能称得上是一个框架,因为他并没有一套全面完整的解决方案。不过,这款框架上手及其容易,是学习游戏开发的初学者入门的一个良好选择。

创作打卡挑战赛

赢取流量/现金/CSDN周边激励大奖

相关文章