love2D无法弄清楚如何使两个圆基本碰撞不会出错 [英] love2D cannot figure out how to make basic collision with two circles does not give error

查看:70
本文介绍了love2D无法弄清楚如何使两个圆基本碰撞不会出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试做到这一点,以便当我的红色圆圈碰到我的白色圆圈时,当我运行代码时,红色圆圈将向后移动一步,我尝试的碰撞将不起作用.它恰好穿过白色圆圈.这是代码

I am trying to make it so that when my red circle touches my white circle the red circle will move back a step when i run the code the collision I tried does not work. It just goes right through the white circle. Here is the code

win = love.window.setMode(600, 600)
Xpos = 300
TX = 50

function love.draw()
    love.graphics.setColor(1, 1, 1)
    love.graphics.circle("fill", Xpos, 200, 25)
    love.graphics.setColor(1, 0, 0)
    love.graphics.circle("fill", TX, 200, 60)

    if Xpos == TX then 
        Xpos = Xpos + 0.1
    end

    if TX >= Xpos then
        TX = TX - 35
    end

    if love.keyboard.isDown("right") then
        TX = TX + 5
    end
end

推荐答案

您做到了,所以圆心碰撞"了.你需要类似的东西

You made it so the centers of the circles 'collide'. You'll need something like

if (TX + 25 + 60) >= Xpos then    --25 and 60 being the radiuses of the circles
    TX = TX - 35
end

此外.在您的代码中,以下内容仅执行一次:

Additionally. In your code the following will execute only once:

if Xpos == TX then 
   Xpos = Xpos + 0.1
end

这是因为 Xpos 300 TX 50 .在每次按住右箭头的迭代中, TX 会增加 5 .这样, TX 在某个时候达到了 300 .现在 Xpos 变为 300.1 ,并且 TX == Xpos 将不再为真,因为 TX 5 ,因此永远不会具有值 300.1 .在我更新的代码中,它根本不会触发,因为圆心永远不会相交.

This is because Xpos is 300, TX is 50. On each iteration with right arrow held the TX increases by 5. This way TX reaches 300 at some point. Now Xpos becomes 300.1 and TX == Xpos will never again be true, because TX is moving in increments of 5 and as such will never have the value 300.1. In my updated code it will not trigger at all because the centers of the circles will never intersect.

如果您想检查碰撞的时刻,您应该使用碰撞检测本身:

If you wan to check for the moment of collision, you should use the collision detection itself:

if (TX + 25 + 60) >= Xpos then    --25 and 60 being the radiuses of the circles
    TX = TX - 35
    --add code here
end

此外,您的代码不是最理想的,圆的速度将受到每秒帧数的影响(某些情况下可能需要它,但是在游戏中,您不希望如此),您应该分开对 love.update

Furthermore, your code is suboptimal and the speed of the circle will be affected by frames per second (some situations might require it, but in games, you don't want this) you should separate the movement and collision detection to love.update

function love.update(dt)
    --first move the circle,
    --then check for collisions to avoid visible intersections
    if love.keyboard.isDown("right") then
        TX = TX + 150 * dt    --move the circle by 150 pixels every second
    end
    if (TX + 25 + 60) >= Xpos then
        TX = TX - 35
    end
end

最终代码将如下所示:

win = love.window.setMode(600, 600)

Xpos = 300
Xpos_radius = 25
TX = 50
TX_radius = 60

function love.update(dt)
    if love.keyboard.isDown("right") then
        TX = TX + 150 * dt
    end
    if (TX + Xpos_radius + TX_radius) >= Xpos then
        TX = TX - 35
        --Xpos = Xpos + 1 --if you want to slowly bump the white ball away
    end
end

function love.draw()
    love.graphics.setColor(1, 1, 1)
    love.graphics.circle("fill", Xpos, 200, Xpos_radius)
    love.graphics.setColor(1, 0, 0)
    love.graphics.circle("fill", TX, 200, TX_radius)
end

这篇关于love2D无法弄清楚如何使两个圆基本碰撞不会出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆