使用重力和碰撞跳跃

我正在尝试在love2D中制作一个简单的平台游戏。目前,除了碰撞处理类、级别类等其他内容外,我的玩家类已经完成了

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

当我跳跃时,玩家会被拉回太快,以至于无法真正有用地跳跃。为什么会发生这种情况?这是从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) -- 向右移动
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) -- 向左移动
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变量的真或假值的位置。

点赞
用户6834680
用户6834680
如果 self.Velocity.Y 小于 -self.maxFallVel,则
    self.Velocity=Vector2.new(self.Velocity.X, -self.maxFallVel)  -- 减去!
end
...
-- 乘以 dt
self.Position=self.Position+Vector2.new(self.Velocity.X,self.Velocity.Y)*dt
如果没有碰撞,则
  self.Velocity.Y = self.Velocity.Y - self.Gravity*dt
end
2017-01-25 14:48:06