【问题标题】:Love2D rotating image in a given time until it reaches an angleLove2D 在给定时间内旋转图像直到达到一个角度
【发布时间】:2013-11-05 05:54:14
【问题描述】:

我正在制作一个小游戏。这是一款2D游戏,主要特点是改变重力方向。我设法改变了方向,所以现在玩家“下降”到重力的方向。但现在我希望玩家在例如“稳定”。 2 秒。我实现了基本旋转,但这会立即改变播放器的角度,我希望它能够将步骤“切片”成更小的部分,这样旋转会“更平滑”。例如。我想以从增量时间计算的小步长从 180° 更改为 0°,这与我输入的数字有关,这将是“持续时间”

我对弧度不太熟悉,所以我不能使用它。

重力方向可以通过world.gravitydir变量设置,可以是1,2,3,4。 1 是正常重力“下” 2,4 是“左”和“右”,3 是“上” 我还有一些“开发命令”可以使用箭头键手动更改重力方向

这是我尝试将播放器从颠倒平稳地旋转到正常状态。

function rotatePlayer(dt)
deg1 = player.rot -- player's current rotation
step = 0 
        deg2 = math.rad(0) -- desired rotation

        step = (math.deg(deg1) - math.deg(deg2))*dt

            for i = deg1, deg2 do
                player.rot = player.rot - math.rad(step)
                step = step - dt
            end
end

playerrotation函数在gravity.lua,dev控制器,播放器绘制函数在player.lua

来源:http://www.mediafire.com/download/3xto995yz638n0n/notitle.love

【问题讨论】:

  • 你有什么问题?
  • 问题是:怎么做? >
  • 我很清楚这一点,您的播放器正在即时旋转,您希望它在特定时间范围内发生吗?
  • 是的,确切地说,是为了平滑地旋转播放器。

标签: image lua rotation smooth love2d


【解决方案1】:

我注意到您的代码存在一些问题。

deg2 = math.rad(0) -- desired rotation

0 弧度等于 0 度,所以看起来deg2 总是为零,因此

for i = deg1, deg2 do

只会在deg1 等于或小于零时运行,这可能意味着它没有按您的预期运行。

其次,任何不需要离开其作用域的变量都应该本地化。这是 Lua 的最佳实践。你的函数可以用本地人重写,例如:

function rotatePlayer(dt)
    local deg1 = player.rot -- player's current rotation
    local step = 0 -- ! Any reference to a "step" variable within the scope of this function will always refer to this particular variable.
    local deg2 = 0 -- desired rotation

    step = (math.deg(deg1) - math.deg(deg2))*dt
    for i = deg1, deg2 do
       player.rot = player.rot - math.rad(step)
       step = step - dt
    end
end

以下行相当于将玩家旋转的度数乘以增量dt,因为之前确定的事实是deg2 始终为零。

    step = (math.deg(deg1) - math.deg(deg2))*dt

以下行等价于将玩家的原始旋转度数乘以增量 (step) 并从玩家当前旋转中减去弧度版本,然后从 @987654331 中减去增量dt @value,在我可能添加的单个游戏帧内执行的循环内。我不确定您是否知道循环在一帧内运行。

        player.rot = player.rot - math.rad(step)
        step = step - dt

我不确定您对这些操作的意图是什么,但也许您可以告诉我更多信息。


为了实现更流畅的动画,您需要降低速率并调整旋转的执行方式。我已将您的函数重写如下,并希望它作为示例阐明如何更好地编写:

function rotatePlayer(dt) -- dt is seconds since last update
    local current = player.rot -- player's current rotation in radians
    local target = 0 -- desired angle in radians
    local difference = target - current

    if difference == 0 then -- nothing to be done here
        return
    end

    local rate = math.pi / 4 -- radians turned per second (adjust as necessary)
    local change = rate * dt -- r/s * delta(change in time)

    if difference < 0 then
        -- make change negative, since difference is as well
        change = change * -1

        -- If (current + change > target), settle for the lesser difference.
        -- This keeps us from "overshooting" the player's rotation
        -- towards a particular target.
        change = math.max(change, difference)
    else
        change = math.min(change, difference)
    end

    player.rot = current + change
end

如果这能解决您的问题,如果您还有其他问题,请告诉我。

【讨论】:

  • 是的,这给了我基本的想法,而且它比我所做的方法更干燥、更清洁(不是我在这里发布的方法)但是,它的过冲,除非我将重力设置为正常,我只更改了“目标”变量,它现在不是本地的,它的值是这样的:if world.gravitydir == 1 then target = 0 end -- down if world.gravitydir == 3 then target = 180 end -- up if world.gravitydir == 2 then target = 90 end -- left if world.gravitydir == 4 then target = 270 end --right
  • 哦,实际上,我忘了我应该将值设置为弧度,现在它没有超调 :D 但现在我注意到从 0° 到 270° 它以积极的方式而不是减去它,但我认为,我可以自己解决这个问题:D
【解决方案2】:

我已经对 pastebin Stackoverflow Love2d game 上的 player.lua 进行了更改。 它以固定速率而不是固定时间旋转播放器。后者只需将 rottime 设为常数即可。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-29
    • 2012-06-04
    • 1970-01-01
    • 2012-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-19
    相关资源
    最近更新 更多