Slow xVelocity when using moveWithCollisions

Gif Of Issue

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

@dan Do you know what might be causing this?

I am not sure I understand correctly what the gif is demonstrating. The movement looks smooth in both images to me, but the player sprite is hovering off the ground in one?

It's hard to say what's going on without a complete running example, but if I had to guess why moveWithCollisions() is returning 0, it might be because the sprite is not being moved downwards after your applyGravity() is called, setting self.yVelocity = 0, and therefore no collision is happening.

Oh, but is touchingGround getting reset in handleMovementAndCollisions()? Which seems like it might cause some oscillations because collisions would only happen only on every second frame?

It's very possible I'm completely wrong about this or am missing what the issue actually is :slight_smile: If you have a code sample that I can run that demonstrates the problem I'd be happy to take another look! But to start I'd check that the values being passed into moveWithCollisions() are what you expect them to be.