-2
我最近爲我的代碼添加了一個實體框架(完成了一半),並且在我添加它之後,我注意到玩家的y速度似乎在1和0之間波動,我完全被難住了。發生了我認爲這與我所投入的當前物理有關,但不知道發生了什麼。下面的代碼:Y Velocity在8和其他東西之間移動不一致?
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
對不起對於這一切,我不知道我裝卡需要什麼來證明,因爲我不知道完全造成這個問題的原因是什麼,我又看了一遍,它似乎是碰撞的事情,也許不確定。無論如何,謝謝那些想要解決我的問題的人,如果有什麼需要解釋或理解的話,我會盡我所能來清除它。
這是太多的代碼。將問題縮小爲[最小,完整和可驗證示例](http://stackoverflow.com/help/mcve)。 –
好吧會做編輯它的感謝,雖然不知道我在做什麼 – Codedin97
希望工程。 – Codedin97