Added README.md and some comments in the code

This commit is contained in:
Sylvain PILLOT 2023-08-25 10:22:50 +02:00
parent 50b54afc2f
commit 2c6e923ea1
3 changed files with 29 additions and 17 deletions

Binary file not shown.

9
README.md Normal file
View File

@ -0,0 +1,9 @@
# Simple AABB and Pixel Perfect collision detection routines for fxSDK/gint
This is an example of routines that can be used on some image_t formats used within gint/fxSDK:
- AABB works on all format and is based on overlapping surrounding boxes detection, which is really fast but not accurate in the case of partially transparent images,
- Pixel Perfect work on IMAGE_RGB565, IMAGE_RBG565A, IMAGE_P8_RGB565 and IMAGE_P8_RGB565A formats (P4 formats not supported yet) and is based on image data scanning and search for collision of two non transparent pixels (one in each image)
The DEBUG version of pixel perfect draws a lot of stuff on the screen to help debugging and understanding how it works.
Feel free to use in you own programs if needed.

View File

@ -15,19 +15,21 @@ extern bopti_image_t img_rock;
bool exit = false;
KeyboardExtra MyKeyboard;
int selector = 0;
int16_t xmin[2], ymin[2];
int16_t xmax[2], ymax[2];
SpriteLocator sprite1, sprite2;
static void get_inputs( )
{
MyKeyboard.Update( 0.0f );
if(MyKeyboard.IsKeyPressedEvent(MYKEY_F1)) (selector = 0);
if(MyKeyboard.IsKeyPressedEvent(MYKEY_F2)) (selector = 1);
if(MyKeyboard.IsKeyPressed(MYKEY_SHIFT) && MyKeyboard.IsKeyHoldPressed(MYKEY_EXIT)) {exit = true; };
if(MyKeyboard.IsKeyPressed(MYKEY_EXIT)) {exit = true; };
if(MyKeyboard.IsKeyPressed(MYKEY_LEFT)) { if(xmin[selector]>0) xmin[selector]--;}
if(MyKeyboard.IsKeyPressed(MYKEY_RIGHT)) { if(xmax[selector]<DWIDTH) xmin[selector]++;}
@ -44,19 +46,14 @@ void update()
xmax[1] = xmin[1] + img_rock.width;
ymax[1] = ymin[1] + img_rock.height;
sprite1 = { .x = xmin[0],
.y = ymin[0],
.image = &img_player };
sprite2 = { .x = xmin[1],
.y = ymin[1],
.image = &img_rock };
sprite1 = { .x = xmin[0], .y = ymin[0], .image = &img_player };
sprite2 = { .x = xmin[1], .y = ymin[1], .image = &img_rock };
}
int main(void)
{
/* */
/* These are declaration of stuff to be used with LibProfg to measure performances */
uint32_t time_AABB=0, time_FastPP=0, time_DebugPP=0;
prof_t perf_AABB, perf_FastPP, perf_DebugPP;
@ -65,6 +62,7 @@ int main(void)
__printf_enable_fp();
/* initial declaration of images position and data */
xmin[0] = 10;
ymin[0] = 10;
xmin[1] = DWIDTH/2 - img_rock.width/2;
@ -73,6 +71,8 @@ int main(void)
sprite2 = { .x = xmin[1], .y = ymin[1], .image = &img_rock };
update();
/* boolean used to track if we have collsion or not (one for each method) */
bool AABB = false;
bool FastPP = false;
bool DebugPP = false;
@ -87,12 +87,14 @@ int main(void)
dclear( C_BLACK );
/* drawx the sprites wih surrounding boxes */
dimage( xmin[0], ymin[0], &img_player );
drect_border( xmin[0], ymin[0], xmax[0], ymax[0], C_NONE, 1, C_WHITE );
dimage( xmin[1], ymin[1], &img_rock );
drect_border( xmin[1], ymin[1], xmax[1], ymax[1], C_NONE, 1, C_WHITE );
/* AABB collision test */
perf_AABB = prof_make();
prof_enter(perf_AABB);
@ -101,7 +103,7 @@ int main(void)
prof_leave(perf_AABB);
time_AABB = prof_time(perf_AABB);
/* Pixel Perfect collision test */
perf_FastPP = prof_make();
prof_enter(perf_FastPP);
@ -110,7 +112,7 @@ int main(void)
prof_leave(perf_FastPP);
time_FastPP = prof_time(perf_FastPP);
/* Pixel Perfect collision test but with internal rendering routines (Very slow, but needed to check what's happening internally)*/
perf_DebugPP = prof_make();
prof_enter(perf_DebugPP);
@ -120,27 +122,28 @@ int main(void)
time_DebugPP = prof_time(perf_DebugPP);
/* Visual restitution of tests (GREEN = Not Colliding / RED = Colliding) */
/* Times are all in µs (microseconds = 1/1000000 sec) */
dtext( 50, DHEIGHT-35, C_WHITE, "AABB");
if (AABB) drect( 10, DHEIGHT-40, 40, DHEIGHT-10, C_RED);
else drect( 10, DHEIGHT-40, 40, DHEIGHT-10, C_GREEN );
//dprint( 10, DHEIGHT-35, C_WHITE, "%.3f", (float) time_AABB / 1000.0f);
dprint( 10, DHEIGHT-35, C_WHITE, "%d", time_AABB);
dtext( 215, DHEIGHT-35, C_WHITE, " Pixel Perfect");
dtext( 215, DHEIGHT-20, C_WHITE, "<DEBUG vs Fast>");
if (FastPP) drect( DWIDTH-40, DHEIGHT-40, DWIDTH-10, DHEIGHT-10, C_RED);
else drect( DWIDTH-40, DHEIGHT-40, DWIDTH-10, DHEIGHT-10, C_GREEN );
//dprint( DWIDTH-40, DHEIGHT-35, C_WHITE, "%.3f", (float) time_FastPP / 1000.0f);
dprint( DWIDTH-40, DHEIGHT-35, C_WHITE, "%d", time_FastPP);
if (DebugPP) drect( DWIDTH/2-15, DHEIGHT-40, DWIDTH/2+15, DHEIGHT-10, C_RED);
else drect( DWIDTH/2-15, DHEIGHT-40, DWIDTH/2+15, DHEIGHT-10, C_GREEN );
//dprint( DWIDTH/2-15, DHEIGHT-35, C_WHITE, "%.3f", (float) time_DebugPP / 1000.0f);
dprint( DWIDTH/2-15, DHEIGHT-35, C_WHITE, "%d", time_DebugPP);
dupdate();
dupdate();
}
while (exit==false);