Calling sprite:setCenter() does not update the position of collideRect

When :setCenter() is called on a sprite, its position changes and even the sprite collision position seems to have moved (when viewing the collisions debug overlay in the Sim).

However, an attempt to check collisions reveals that the collideRect is still at the original position. The position is only corrected when that sprite is moved.

The issue is that the position isn’t being updated but also that the presented state in Simulator (sprite debug drawing) differs from the actual state. (I can see how using getCenter + getCollideBounds easily creates this.)

Tested on macOS Sim, version 3.1.2.

Minimal example

Creating two 20x20px sprites with matching 20x20 collision rects like this:

function createSprite()
	local s = gfx.sprite.new()
	s:setImage(pic) -- a 20x20 rectangle
	s:setCollideRect(0, 0, 20, 20)
	s:add()
	return s
end

local a = createSprite()
a:setCenter(0, 0) -- calling set center before moveTo
a:moveTo(20, 20)


local b = createSprite()
b:moveTo(60, 20)
b:setCenter(0, 0) -- calling set center after moveTo

local _, _, cols = a:checkCollisions(60, 20)
printTable(cols[1].otherRect)

function playdate.update()
	gfx.sprite.update() -- to draw on screen
end

Shows up nicely: the images and the rects are both in the correct position, as if setCenter was applied to the collideRect:

However, the output in console shows that sprite b’s collision rect did not move:

(50.0, 10.0, 20.0, 20.0)

This changes only after calling moveTo()on sprite b.

Minimal example

Source + pdx file:

set-center.zip (16.8 KB)

1 Like