
so i found how to deal with tile and sprite collision
Based on sprite sample
Code: Select all
typedef struct {
u16 col_x;
u16 col_y;
u16 tile;
} collision_info_t;
static u16 checkTileXCollision(u16 x, u16 y0, u16 y1, collision_info_t * coll) {
u16 tile = 0;
u16 y;
for(y = y0; y <= y1; y++) {
tile = MAP_getTile(&MyLevel, x, y, 0);
if (tile) {
coll->col_x = x;
coll->col_y = y;
coll->tile = tile;
return tile;
}
}
return tile;
}
static u16 checkTileYCollision(u16 y, u16 x0, u16 x1, collision_info_t * coll) {
u16 tile = 0;
u16 x;
for(x = x0; x <= x1; x++) {
tile = MAP_getTile(&MyLevel, x, y, 0);
if (tile) {
coll->col_x = x;
coll->col_y = y;
coll->tile = tile;
return tile;
}
}
return tile;
}
static void updateTileCollision() {
// @todo use boundingbox
#define BOX_X 16
#define BOX_W 16
#define BOX_H 40
s16 __xr = ((fix32ToInt(posx) + BOX_W + BOX_X) >>3);
s16 __xl = ((fix32ToInt(posx) + BOX_X) >>3);
s16 __y = (fix32ToInt(posy)>>3);
s16 __yb = ((fix32ToInt(posy)+ BOX_H) >>3);
collision_info_t col= {};
u16 left = 0;
u16 right = 0;
u16 bottom = 0;
u16 top = 0;
// XAXIS
// Left
if (checkTileXCollision(__xl, __y, __yb-1, &col)) {
posx = FIX32((col.col_x<<3) - 8);
movx = 0;
}
// Right
else if (checkTileXCollision(__xr, __y, __yb-1, &col)) {
u16 m = (col.col_x<<3) - BOX_X - BOX_W;
posx = FIX32(m);
movx = 0;
}
// YAXIS
if (movy>0 && checkTileYCollision(__yb, __xl, __xr, &col)) {
posy = FIX32((col.col_y<<3) - BOX_H);
movy = 0;
}
if (left || right/* || bottom*/) {
VDP_drawText("COLL", 5, 0);
} else {
VDP_drawText("NO_C", 5, 0);
}
}
Now i have some other questions

How to deal with map and plan scrolling ?
Do i need to refresh the plan in a vblank ?