重力跳跃与碰撞

Jumping with gravity & collision

我正在尝试在 love2D 中制作一个简单的平台游戏。目前我的播放器 class 在其他方面(碰撞处理 class、关卡 classes 等)

我遇到的主要问题是跳跃。我只是无法让它正常工作。

当我跳跃时,玩家被拉回的速度太快以至于无法真正使跳跃有用。为什么会这样?这是从ROBLOX移植过来的代码,在ROBLOX Studio中,跳转正常。

这是在播放器的更新函数中,从 love.update 开始的每一帧调用:

if not love.keyboard.isDown("a") and not love.keyboard.isDown("d") then 
    self.Velocity=self.Velocity * Vector2.new(0.95,1)
end
if self.Velocity.Y < -self.maxFallVel then
    self.Velocity=Vector2.new(self.Velocity.X,self.maxFallVel)
end
if love.keyboard.isDown("d") and self.Velocity.X<self.maxVel and not love.keyboard.isDown("a") then
    self.Velocity = self.Velocity+Vector2.new(.1,0) -- right movement
end
if love.keyboard.isDown("a") and self.Velocity.X<self.maxVel and not love.keyboard.isDown("d") then
    self.Velocity = self.Velocity-Vector2.new(.1,0) -- left movement
end
if love.keyboard.isDown("space") and self.hasCollision and not self.Jumped then
    if self.Velocity.Y == 0 then
        self.Velocity.Y = -30 
    end
end
self.Position=self.Position+Vector2.new(self.Velocity.X,self.Velocity.Y)
if not self.hasCollision then self.Velocity.Y = self.Velocity.Y - self.Gravity end

当我在 main.lua 文件中检查冲突时,我在此处将变量 self.hasCollision 设置为 true 或 false。

if self.Velocity.Y < -self.maxFallVel then
    self.Velocity=Vector2.new(self.Velocity.X, -self.maxFallVel)  -- minus!
end
...
-- multiply by dt
self.Position=self.Position+Vector2.new(self.Velocity.X,self.Velocity.Y)*dt
if not self.hasCollision then 
  self.Velocity.Y = self.Velocity.Y - self.Gravity*dt   
end