Y速度在8和其它东西之间不一致?

时间:2022-11-24 15:32:31

I recently added an entity framework to my code (sorta half done) and after I added it I noticed something wrong with the y velocity of the player it seems to fluctuate between 1 and 0. I am completely stumped as to why this has occurred I assume it's something to do with the current physics I've put in but not sure what happening. code below:

最近,我在代码中添加了一个实体框架(差不多完成了一半),在添加之后,我注意到播放器的y速度出现了问题,它似乎在1和0之间波动。我完全搞不懂为什么会发生这种事,我想这和我现在所做的物理实验有关,但我不知道会发生什么。下面的代码:

Collisions.Lua

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

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

Sorry For chucking in all this I didn't know what I needed to show as I don't completely know whats creating the problem, I have had another look and it looks to be something to do with collisions maybe, not sure. anyway thank you to those who want to have a crack at my problem if anything needs explaining or understanding I'll try my best to clear it up.

对不起,我不知道我需要展示什么,因为我不完全知道是什么造成了这个问题,我有另一个外观,它看起来和碰撞有关,也许,不确定。无论如何,谢谢那些想尝试解决我的问题的人,如果有什么需要解释或理解的,我将尽我最大的努力解决它。

1 个解决方案

#1


0  

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

修正你的时间步长,不要在物理计算中使用dt。

#1


0  

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

修正你的时间步长,不要在物理计算中使用dt。