Y Velocity在8和其他东西之间移动是不一致的吗?

时间:2015-05-14 08:30:27

标签: lua game-physics love2d

我最近在我的代码中添加了一个实体框架(sorta half done),在我添加它之后,我注意到玩家的y速度有问题,它似乎在1到0之间波动。我完全难过为什么这个已经发生了我认为它与目前的物理学有关,但我不知道发生了什么。代码如下:

Collisions.Lua

-- btw Not my Code just needed something to get AABB to work right
 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' Must Be an entity that Can Move 
-- 'B' Can Be a Static Object
-- Both need a Height, width, X and Y value
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

    -- Set Health
    entitybase.Health = 100

    -- Set Acceleration
    entitybase.Accel = 3000

    -- Set Y velocity
    entitybase.Yvel = 0.0

    -- Set X velocity
    entitybase.Xvel = 0.0

    -- Set Y Position
    entitybase.Y = 0

    -- Set X Position
    entitybase.X = 0

    -- Set Height
    entitybase.Height = 64

    -- Set Width 
    entitybase.Width = 64

    -- Set Friction
    entitybase.Friction = 0

    -- Set Jump
    entitybase.Jump = 350

    -- Set Mass
    entitybase.Mass = 50 

    -- Set Detection Radius
    entitybase.Radius = 100

    -- Sets boolean value for if the player can jump
    entitybase.canJump = true

    -- Sets Image default
    entitybase.img = nil

------------------------------------------------
-- Some Physics To ensure that movement works --
------------------------------------------------

function entitybase.physics(dt)


    -- Sets the X position of the entities
    entitybase.X = entitybase.X + entitybase.Xvel * dt

    -- Sets the Y position of the entities
    entitybase.Y = entitybase.Y + entitybase.Yvel * dt

    -- Sets the Y Velocity  of the entities
    entitybase.Yvel = entitybase.Yvel + (gravity  * entitybase.Mass) * dt

    -- Sets the X Velocity of the entities
    entitybase.Xvel = entitybase.Xvel * (1 - math.min(dt * entitybase.Friction, 1))

end

-- Entity Jump feature
function entitybase:DoJump()

    -- if the entities y velocity = 0 then subtract the Yvel from Jump Value
    if entitybase.Yvel == 0 then
        entitybase.Yvel = entitybase.Yvel - entitybase.Jump
    end

end

-- Updates the Jump
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

抱歉,对于所有这些我都不知道我需要展示什么,因为我完全不知道是什么造成了这个问题,我又看了一眼它看起来与碰撞有什么关系也许,不确定。无论如何,如果有任何需要解释或理解的话,我会感谢那些想要解决我的问题的人。我会尽力清理它。

1 个答案:

答案 0 :(得分:0)

Fix your timestep并且不要使用dt进行物理计算。