Collision working but very very very slow

This commit is contained in:
Arne van Iterson 2019-12-19 21:00:20 +01:00
parent 000a5cfe90
commit 663bd8d990
7 changed files with 720 additions and 204 deletions

1
.gitignore vendored
View File

@ -1,4 +1,5 @@
.vscode/* .vscode/*
.vs/*
bin/* bin/*
src/gfx/convpng.* src/gfx/convpng.*
screenshot.png screenshot.png

File diff suppressed because it is too large Load Diff

View File

@ -7,6 +7,11 @@ struct Baddie {
float x; float x;
float y; float y;
} pos; } pos;
struct oldPos
{
float x;
float y;
} oldPos;
} baddies[baddieAmount]; } baddies[baddieAmount];
int spawnBaddie(float posX, float posY) int spawnBaddie(float posX, float posY)
@ -31,7 +36,12 @@ int updateBaddie(int i)
if (!(baddies[i].pos.x <= (0 - 32))) if (!(baddies[i].pos.x <= (0 - 32)))
{ {
baddies[i].pos.x -= speed; baddies[i].pos.x -= speed;
if ((abs(baddies[i].oldPos.x - baddies[i].pos.x) >= 1) || (abs(baddies[i].oldPos.y - baddies[i].pos.y) >= 1))
{
gfx_Sprite(baddie, baddies[i].pos.x, baddies[i].pos.y); gfx_Sprite(baddie, baddies[i].pos.x, baddies[i].pos.y);
baddies[i].oldPos.x = baddies[i].pos.x;
baddies[i].oldPos.y = baddies[i].pos.y;
}
return 1; return 1;
} }
else else

View File

@ -9,6 +9,11 @@ struct Bullet
float x; float x;
float y; float y;
} pos; } pos;
struct oldPos
{
float x;
float y;
} oldPos;
} bullets[bulletAmount]; } bullets[bulletAmount];
int spawnBullet(float posX, float posY) int spawnBullet(float posX, float posY)
@ -33,7 +38,12 @@ int updateBullet(int i)
if (!(bullets[i].pos.x >= (LCD_WIDTH + 16))) if (!(bullets[i].pos.x >= (LCD_WIDTH + 16)))
{ {
bullets[i].pos.x += speed; bullets[i].pos.x += speed;
if ((abs(bullets[i].oldPos.x - bullets[i].pos.x) >= 1) || (abs(bullets[i].oldPos.y - bullets[i].pos.y) >= 1))
{
gfx_Sprite(bullet, bullets[i].pos.x, bullets[i].pos.y); gfx_Sprite(bullet, bullets[i].pos.x, bullets[i].pos.y);
bullets[i].oldPos.x = bullets[i].pos.x;
bullets[i].oldPos.y = bullets[i].pos.y;
}
return 1; return 1;
} }
else else

40
src/collision.h Normal file
View File

@ -0,0 +1,40 @@
// Variables for collision detection
float distance = 0;
float difference[2];
int a;
// Check collisions
void checkCollision() {
for (i = 0; i < (baddieAmount - 1); i++)
{
// Collision baddie x bullet
for (a = 0; a < (bulletAmount - 1); a++)
{
//// Difference x
difference[0] = (baddies[i].pos.x + (32 / 2)) - (bullets[a].pos.x + (16 / 2));
//// Difference y
difference[1] = (baddies[i].pos.y + (32 / 2)) - (bullets[a].pos.y + (16 / 2));
// Calculate diffence
distance = sqrt(pow(difference[0], 2) + pow(difference[1], 2));
if (!baddies[i].dead && !bullets[a].dead && distance <= 11)
{
bullets[a].dead = true;
baddies[i].dead = true;
}
}
// Collision baddie x player
//// Difference x
difference[0] = (baddies[i].pos.x + (32 / 2)) - (player.pos.x + (32 / 2));
//// Difference y
difference[1] = (baddies[i].pos.y + (32 / 2)) - (player.pos.y + (32 / 2));
// Calculate diffence
distance = sqrt(pow(difference[0], 2) + pow(difference[1], 2));
if (!baddies[i].dead && distance <= 15)
{
// Gameover
}
}
}

View File

@ -15,15 +15,19 @@
// set global vars // set global vars
const int bulletAmount = 5; const int bulletAmount = 5;
const int baddieAmount = 32; const int baddieAmount = 12;
int spriteActive = 0; int spriteActive = 0;
int gamestate = 0;
float speed = 0.5; float speed = 0.5;
int i; int i;
float t;
float dt;
// Include entity headers // Include entity headers
#include "player.h" #include "player.h"
#include "bullet.h" #include "bullet.h"
#include "baddie.h" #include "baddie.h"
#include "collision.h"
#include "controls.h" #include "controls.h"
void setup() void setup()
@ -32,8 +36,6 @@ void setup()
/* Initialize the 8bpp graphics */ /* Initialize the 8bpp graphics */
gfx_Begin(); gfx_Begin();
dbg_sprintf(dbgout, "This is the start of a CEmu debugging test\n");
dbg_Debugger();
/* Set up the palette for our sprites */ /* Set up the palette for our sprites */
gfx_SetPalette(sprites_pal, sizeof_sprites_pal, 0); gfx_SetPalette(sprites_pal, sizeof_sprites_pal, 0);
@ -92,6 +94,8 @@ void main(void)
} }
checkCollision();
// Redraw player if position has changed or if a bullet has been fired // Redraw player if position has changed or if a bullet has been fired
updatePlayer(spriteActive); updatePlayer(spriteActive);

View File

@ -13,9 +13,9 @@ struct Player
} oldPos; } oldPos;
} player; } player;
void updatePlayer(int bulletsAlive) void updatePlayer(int spriteActive)
{ {
if ((abs(player.oldPos.x - player.pos.x) >= 1) || (abs(player.oldPos.y - player.pos.y) >= 1) || bulletsAlive == 1) if ((abs(player.oldPos.x - player.pos.x) >= 1) || (abs(player.oldPos.y - player.pos.y) >= 1) || spriteActive == 1)
{ {
gfx_Sprite(spaceship, player.pos.x, player.pos.y); gfx_Sprite(spaceship, player.pos.x, player.pos.y);
player.oldPos.x = player.pos.x; player.oldPos.x = player.pos.x;