kaplay icon indicating copy to clipboard operation
kaplay copied to clipboard

bug: Agent path finding requires center components to remain within tiles

Open nojaf opened this issue 8 months ago • 2 comments

Bug Description

While playing around with the agent component.

Code

kaplay({ scale: 1, background: [0, 0, 0], });

debug.inspect = true;

loadSprite("bean", "sprites/bean.png"); loadSprite("steel", "sprites/steel.png"); loadSprite("grass", "sprites/grass.png");

const TILE_SIZE = 64; const HALF_TILE_SIZE = TILE_SIZE / 2;

const level = addLevel( [ "#############", "# #", "# #", "# #", "# #", "# #", "#############", ], { tileWidth: TILE_SIZE, tileHeight: TILE_SIZE, tiles: { "#": () => [sprite("steel"), tile({ isObstacle: true })], " ": () => [sprite("grass"), tile()], }, }, );

const bean = level.spawn( [ sprite("bean"), anchor("center"), pos(HALF_TILE_SIZE, HALF_TILE_SIZE), tile(), agent({ speed: 205, allowDiagonals: false }), "bean", ], 1, 1, );

onClick(() => { const pos = mousePos(); const tilePos = level.pos2Tile(pos); bean.setTarget( vec2( tilePos.x * TILE_SIZE + HALF_TILE_SIZE, tilePos.y * TILE_SIZE + HALF_TILE_SIZE, ), ); });

I noticed I had to include anchor("center") and pos(HALF_TILE_SIZE, HALF_TILE_SIZE), for the movement to remain within the tiles boundaries. Meaning the sprite always remains within the cells of a level.

To reproduce the problem: remove anchor and pos from the bean and see how it overlaps the tiles.

I'm not sure if this is a bug and by adding anchor and pos the user is countering the current behavior. Or this is how it is meant to be passed down, which would make it a documentation gap.

Version

master

Playground Link

https://play.kaplayjs.com/?code=eJy1U1Fr2zAQfvevOLwXeRNpG7Y9uHQw2o4WAis0UGgIRZFvjokmGZ%2FSroT8951ku3bTPuxlxj5Ld9%2Bd7rs7bVRt1LPYJQCklcEcTiSvV0pvysZtbZHD4lhCeJcy2WenSVLgaltOKks1ag9n4Jststo4VdzWTeVRpCtUNpWQUtzTUdhPalum7D%2FGkUc0Y2BUvIcsG0U0RkZFj0y0s%2BRhfj27fLi9vr%2FkrL5%2BPu20V99nPx7GpmF9BNMXZ4OPaNiqimIWloLLsOAPIP0wflLZKWF4%2Fq%2FyzenLIHbR6iuDd1Xh1%2FlAS75YrrAq1%2F5dE%2BVdhHBCmoPI4OwbLOhVZzIZsWIHFf1ckVc6TEhoOOyzpez94a1%2F26%2Fev8fuw4%2FF0LIwGVz0WPwJ1erJDnWn8TRlbQRl9do1ItVoPTa9tnYkXrdZHrQ9G5iLPlTJMZgazzHymE%2BPv0hQxrini0qVzirDJfrFMnDtWtHOdd%2BBk04ENs6em0pvRFuGUNmWH2fG9H67LeENJ8nQ3hJSuYnWljwjp%2FOQHi8iLF4aQj9XTYlexAweUU9FV%2FYuwOQPfByN9KcD4vIA%2FfwP6EiXcwjX%2FS%2FSuxY3&version=master

Extra information

No response

Summary

  • [ ] Fixed in v4000
  • [ ] Fixed in v3001

nojaf avatar Apr 16 '25 18:04 nojaf

the level component's getPath method adds half tile size to each path point. I believe is because is meant to be differentiate between getTilePath and getPath method. maybe we should add an offset option or add the offset of the agents position relative to the tile is on. while we are also on the topic of pathing, shouldn't allowDiagonals, do exactly that? say you have the folllowing level:

ax
xb

where x is an obstacle and a and b are positions, shouldn't allowDialognals allow movement between point a and point b?

KeSuave avatar Apr 16 '25 20:04 KeSuave

allowDialognals means that diagonal movement is allowed. This affects the accessibility of surrounding tiles from the current tile. If allowDialognals is false, only 4 directions are taken into consideration, otherwise 8. But I think that the blocking test looks whether left and up is not blocked to decide whether you can go left top.

mflerackers avatar Apr 16 '25 23:04 mflerackers