Faster way to get overlaping sprites?

Is there any way to get a sprite that is overlapping with a sprite of a kind that’s faster then this:

image

Does Arcade keep track of “overlapping” sprites on sprites somewhere so that we don’t have to iterate over every other sprite to see if it’s overlapping with it? Like maybe a private list of sprites?

Unfortunately there is no faster way to do this that isn’t very hacky…

We do keep this information around in the physics engine, it just isn’t accessible from outside. You can see where it’s declared here (note the protected). The actual implementation is here:

Of course protected is more of a “suggestion” in makecode than it is a rule, so you could do this:

function getOverlappingByKind(sprite: Sprite, kind: number) {
    // EVIL CODE
    const map = (game.currentScene().physicsEngine as any).map as sprites.SpriteMap;

    return map.neighbors(sprite).filter(s => s.kind() === kind).filter(s => s.overlapsWith(sprite));
}

but you should not do that because it could break at any time.

1 Like