【发布时间】:2015-07-25 19:08:30
【问题描述】:
我最近在我的代码中添加了一个实体框架(差不多完成了一半),添加后我注意到播放器的 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
抱歉,我不知道我需要展示什么,因为我不完全知道是什么造成了问题,我又看了一遍,它看起来可能与碰撞有关,不确定.无论如何,感谢那些想要解决我的问题的人,如果有什么需要解释或理解,我会尽力解决它。
【问题讨论】:
-
代码太多了。将您的问题缩小到a Minimal, Complete, and Verifiable example。
-
好的,谢谢你的编辑,虽然不知道我在做什么
标签: lua game-physics love2d