EDIT: Found the print statement in the docs and it appears that "length" is returning 0 than 1 in a cycle. despite colliding with the ground the entire time (as seen in the gif).
I am making my first game for the playdate and have encountered a movement speed issue related to the collision system. when the player is colliding with the ground the movement is very choppy. if I remove gravity, and therefore do not, the game is very smooth. am I not using the collision system correctly?
here is the player code:
local pd <const> = playdate
local gfx <const> = playdate.graphics
class('Player').extends(AnimatedSprite)
function Player:init(x, y)
local playerImageTable = gfx.imagetable.new("images/player-table-16-16")
Player.super.init(self, playerImageTable)
-- States
self:addState("idle", 1, 2, { tickStep = 4 })
self:addState("moving", 4, 7, { tickStep = 4 })
self:addState("jump", 3, 3, { tickStep = 4 })
self:addState("dead", 8, 10, { tickStep = 4, loop = false })
self:playAnimation()
-- Sprite Properties
self:moveTo(x, y)
self:setZIndex(Z_INDEXES.Player)
self:setTag(TAGS.Player)
self:setCollideRect(5, 2, 6, 14)
self.speed = 3
self.jumpPower = 6
-- Physics
self.xVelocity = self.speed
self.yVelocity = 0
self.gravity = 0.5
self.maxSpeed = 5
-- Player State
self.touchingGround = false
end
function Player:collisoinResponce()
return gfx.sprite.kCollisionTypeSlide
end
function Player:update()
self:updateAnimation()
self:applyGravity()
self:handleState()
self:handleMovementAndCollisions()
end
function Player:handleState()
if self.currentState == "idle" then
elseif self.currentState == "moving" then
if pd.buttonJustPressed(pd.kButtonA) then
self:changeToJumpState()
end
elseif self.currentState == "jump" then
if self.touchingGround then
self:changeToMoveState()
end
elseif self.currentState == "dead" then
end
end
function Player:handleMovementAndCollisions()
local _, _, collisions, length = self:moveWithCollisions(self.x + self.xVelocity, self.y + self.yVelocity)
self.touchingGround = false
local canReverse = true
for i = 1, length do
local collision = collisions[i]
if collision.normal.y == -1 then
self.touchingGround = true
end
if math.abs(collision.normal.x) == 1 and canReverse then
self:reversePlayerDirection()
canReverse = false
end
end
if self.xVelocity < 0 then
self.globalFlip = 1
elseif self.xVelocity > 0 then
self.globalFlip = 0
end
end
function Player:reversePlayerDirection()
self.xVelocity = self.xVelocity * -1
end
function Player:applyGravity()
self.yVelocity += self.gravity
if self.touchingGround then
self.yVelocity = 0
end
end
function Player:changeToMoveState()
self:changeState("moving")
end
function Player:changeToJumpState()
self.yVelocity = -self.jumpPower
self:changeState("jump")
end
function Player:changeToDeadState()
self:changeState("dead")
end