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/*
.vs/*
bin/*
src/gfx/convpng.*
screenshot.png

File diff suppressed because it is too large Load Diff

View File

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

View File

@ -9,6 +9,11 @@ struct Bullet
float x;
float y;
} pos;
struct oldPos
{
float x;
float y;
} oldPos;
} bullets[bulletAmount];
int spawnBullet(float posX, float posY)
@ -33,7 +38,12 @@ int updateBullet(int i)
if (!(bullets[i].pos.x >= (LCD_WIDTH + 16)))
{
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);
bullets[i].oldPos.x = bullets[i].pos.x;
bullets[i].oldPos.y = bullets[i].pos.y;
}
return 1;
}
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
const int bulletAmount = 5;
const int baddieAmount = 32;
const int baddieAmount = 12;
int spriteActive = 0;
int gamestate = 0;
float speed = 0.5;
int i;
float t;
float dt;
// Include entity headers
#include "player.h"
#include "bullet.h"
#include "baddie.h"
#include "collision.h"
#include "controls.h"
void setup()
@ -32,8 +36,6 @@ void setup()
/* Initialize the 8bpp graphics */
gfx_Begin();
dbg_sprintf(dbgout, "This is the start of a CEmu debugging test\n");
dbg_Debugger();
/* Set up the palette for our sprites */
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
updatePlayer(spriteActive);

View File

@ -13,9 +13,9 @@ struct Player
} oldPos;
} 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);
player.oldPos.x = player.pos.x;