【问题标题】:LOVE2D, Desync movement of follower-objectLOVE2D,跟随者对象的不同步移动
【发布时间】:2017-12-06 17:51:47
【问题描述】:

我的代码有问题。 (Love2D 框架) 我正在制作小的追随者对象,它实时跟随坐标。 问题是移动速度正在变化,具体取决于矢量。 就像向前移动比向左上方移动要快一些。 有人可以帮助我吗?请标记我的错误。 代码:

    function love.load()
    player = love.graphics.newImage("player.png")
    local f = love.graphics.newFont(20)
    love.graphics.setFont(f)
    love.graphics.setBackgroundColor(75,75,75)
    x = 100
    y = 100
    x2 = 600
    y2 = 600
    speed = 300
    speed2 = 100
end

function love.draw()
    love.graphics.draw(player, x, y)
    love.graphics.draw(player, x2, y2)
end

function love.update(dt)
print(x, y, x2, y2)
   if love.keyboard.isDown("right") then
      x = x + (speed * dt)
   end
   if love.keyboard.isDown("left") then
      x = x - (speed * dt)
   end

   if love.keyboard.isDown("down") then
      y = y + (speed * dt)
   end
   if love.keyboard.isDown("up") then
      y = y - (speed * dt)
   end

   if x < x2 and y < y2 then
    x2 = x2 - (speed2 * dt)
    y2 = y2 - (speed2 * dt)
   end

   if x > x2 and y < y2 then
    x2 = x2 + (speed2 * dt)
    y2 = y2 - (speed2 * dt)
   end

   if x > x2 and y > y2 then
    x2 = x2 + (speed2 * dt)
    y2 = y2 + (speed2 * dt)
   end

   if x < x2 and y > y2 then
    x2 = x2 - (speed2 * dt)
    y2 = y2 + (speed2 * dt)
   end
end

【问题讨论】:

    标签: lua synchronization game-physics love2d


    【解决方案1】:

    这个

       if x < x2 and y < y2 then
        x2 = x2 - (speed2 * dt)
        y2 = y2 - (speed2 * dt)
       end
    
       if x > x2 and y < y2 then
        x2 = x2 + (speed2 * dt)
        y2 = y2 - (speed2 * dt)
       end
    
       if x > x2 and y > y2 then
        x2 = x2 + (speed2 * dt)
        y2 = y2 + (speed2 * dt)
       end
    
       if x < x2 and y > y2 then
        x2 = x2 - (speed2 * dt)
        y2 = y2 + (speed2 * dt)
       end
    

    有一堆问题。

    导致您的速度差异的原因是:如果(x&lt;x2),但x&gt;(x2+speed2*dt),您将运行第一个分支(if x &lt; x2 and y &lt; y2 then …)。这将更改值,以便您也将点击第二个分支 (if x &gt; x2 and y &lt; y2 then …),这意味着您在 y 方向上移动两次。更改为if x &lt; x2 and y &lt; y2 then … elseif x &gt; x2 and y &lt; y2 then …,这样您就不会掉入其他分支,或者进行数学运算并避开整个if-chain。

    你可能想要也可能不想要并且目前拥有的一件事是它要么朝某个方向走,要么不朝某个方向走。这意味着跟随者可以行进的方向有 8 个。 (轴对齐或对角线 - 您拥有的 4 种情况加上 dx 或 dy (大约)为零的情况。)

    如果你想让它“直接向你”移动,那么跟随者应该能够向任何方向移动。

    您必须使用沿 x 和 y 方向的相对距离。有many common choices of distance definitions,但您可能想要euclidean distance。 (该范数/距离定义下的"shape" of the unit circle 决定了它在任一方向上的移动速度,只有欧几里得距离“在所有方向上都相同”。)

    所以将上面的块替换为

       local dx, dy = (x2-x), (y2-y)    -- difference in both directions
       local norm = 1/(dx^2+dy^2)^(1/2) -- length of the distance
       dx, dy = dx*norm, dy*norm        -- relative contributions of x/y parts
       x2, y2 = x2 - dx*speed*dt, y2 - dy*speed*dt
    

    你会得到距离的“正常”概念,它会得到移动速度的正常概念,当你向玩家移动时,它会得到相同的速度。

    【讨论】:

      猜你喜欢
      • 2018-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多