垂直速度不一致,在8和其他数值之间移动?

我最近在我的代码中添加了一个实体框架(有一半的工作完成),添加后我发现玩家的y速度似乎在1和0之间波动。我完全不知道为什么会发生这种情况,我认为这与我放入的当前物理有关,但不确定发生了什么。以下是代码:

Collisions.Lua

-- btw不是我的代码,只需要一些东西才能使AABB正常工作
function isColliding(a,b)
      if ((b.X >= a.X + a.Width) or
         (b.X + b.Width <= a.X) or
         (b.Y >= a.Y + a.Height) or
         (b.Y + b.Height <= a.Y)) then
           return false
      else return true
           end
   end

-- ‘A’必须是可以移动的实体
-- ‘B’可以是静态物体
-- 两者都需要高度,宽度,X和Y值
function IfColliding(a, b)

    if isColliding(a,b) then

        if a.Y + a.Height < b.Y then
            a.Y = b.Y - a.Height
            a.YVel = 0
        elseif a.Y - a.Height > b.Y - b.Height then
            a.Y = b.Y + b.Height
            a.YVel = 0
        end

    end

end

entitybase.lua

local entitybase = {}

lg = love.graphics

    -- 设置健康值
    entitybase.Health = 100

    -- 设置加速度
    entitybase.Accel = 3000

    -- 设置Y速度
    entitybase.Yvel = 0.0

    -- 设置X速度
    entitybase.Xvel = 0.0

    -- 设置Y位置
    entitybase.Y = 0

    -- 设置X位置
    entitybase.X = 0

    -- 设置高度
    entitybase.Height = 64

    -- 设置宽度
    entitybase.Width = 64

    -- 设置摩擦力
    entitybase.Friction = 0

    -- 设置跳跃
    entitybase.Jump = 350

    -- 设置质量
    entitybase.Mass = 50

    -- 设置检测半径
    entitybase.Radius = 100

    -- 设置玩家能否跳跃的布尔值
    entitybase.canJump = true

    -- 设置图像默认值
    entitybase.img = nil

------------------------------------------------
-- 确保运动工作的一些物理学 --
------------------------------------------------

function entitybase.physics(dt)

    -- 设置实体的X位置
    entitybase.X = entitybase.X + entitybase.Xvel * dt

    -- 设置实体的Y位置
    entitybase.Y = entitybase.Y + entitybase.Yvel * dt

    -- 设置实体的Y速度
    entitybase.Yvel = entitybase.Yvel + (gravity  * entitybase.Mass) * dt

    -- 设置实体的X速度
    entitybase.Xvel = entitybase.Xvel * (1 - math.min(dt * entitybase.Friction, 1))

end

-- 实体跳跃功能
function entitybase:DoJump()

    -- 如果实体的Y速度=0,则从跳跃值中减去Yvel
    if entitybase.Yvel == 0 then
        entitybase.Yvel = entitybase.Yvel - entitybase.Jump
    end

end

-- 更新跳跃
function entitybase:JumpUpdate(dt)

    if entitybase.Yvel ~= 0 then
        entitybase.Y = entitybase.Y + entitybase.Yvel * dt
        entitybase.Yvel = entitybase.Yvel - gravity * dt
    end

end

function entitybase:update(dt)

    entitybase:JumpUpdate(dt)
    entitybase.physics(dt)
    entitybase.move(dt)

end

return entitybase

抱歉,我把所有这些都放在了一起,我不知道需要展示什么,因为我不完全知道是什么导致了问题,我重新看了一下,似乎是与碰撞有关,但不确定。无论如何,感谢那些想要解决我的问题的人,如果需要解释或理解任何内容,我会尽力解释清楚。

点赞
用户4923624
用户4923624

修复你的时间步长

并且不要在物理计算中使用 dt

Fix your timestep and don't use dt for physics calculations.

2015-05-21 07:37:52