### Problem with basic collision detection

So i've decided to continue with trying to make a basic space invaders type game in the console.
But i have run in to a problem with the collision detection.For some reason it only seems to detect when the bullet hits the first space invader but not the second.

I think the problem is in `bool Physics::CheckColision()`

Also i have commented out the movement so that the collision can be tested more easily.
This is still a work in progress and so any criticism is welcomed and i understand it is still very buggy.
Anyway heres my code.

 Physics.h
 ``12345678910111213141516171819202122232425262728293031`` ``````#ifndef PHYSICS_H_INCLUDED #define PHYSICS_H_INCLUDED #include "Graphics.h" class Physics { private: Graphics SpaceGraph; int PlayerPos, ShipPos[2][2], BulletPos[2]; bool reverse[2]; bool bulletfired; public: Physics():PlayerPos(19) { reverse[0] = 0;reverse[1] = 0; ShipPos[0][0] = 1;ShipPos[0][1] = 1; ShipPos[1][0] = 2;ShipPos[1][1] = 5; BulletPos[0] = 0;BulletPos[1] = 0; } void MovePlayer(char); char MoveAll(); void MoveSpaceship(); void Play(); void firebullet(); bool CheckColision(); }; #endif // PHYSICS_H_INCLUDED ``````

 Graphics.h
 ``1234567891011121314151617`` ``````#ifndef GRAPHICS_H_INCLUDED #define GRAPHICS_H_INCLUDED class Graphics { private: static const int Width = 40, Height = 20; char drawGame [Height][Width]; public: void makegame(); void drawgame(); void drawplayer(int x , int y); void drawspaceship(int x , int y); void drawbullet(int x , int y); }; #endif // GRAPHICS_H_INCLUDED ``````

 main.cpp
 ``1234567891011`` ``````#include #include "Physics.h" using namespace std; int main() { Physics SpaceInvader; SpaceInvader.Play(); } ``````

 Physics.cpp
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125`` ``````#include #include "Physics.h" #include #include char Physics::MoveAll() { char Move(0); if (kbhit()!=0) { Move = getch(); MovePlayer(Move); return(Move); } MoveSpaceship(); SpaceGraph.drawplayer(18, PlayerPos); } void Physics::MovePlayer(char Move) { using namespace std; switch (Move) { case 's' : if(PlayerPos > 1) { PlayerPos--; } break; case 'd': if(PlayerPos < 38) { PlayerPos++; } break; case 'a': if (bulletfired == 0) firebullet(); break; } } void Physics::MoveSpaceship() { using namespace std; for(int i = 0; i < 2; i++) { if(reverse[i] == 0) { //ShipPos[i][1]++; if(ShipPos[i][1] == 38) { //ShipPos[i][0]++; reverse[i] = 1; } } else { //ShipPos[i][1]--; if(ShipPos[i][1] == 1) { //ShipPos[i][0]++; reverse[i] = 0; } } SpaceGraph.drawspaceship(ShipPos[i][0],ShipPos[i][1]); } } void Physics::Play() { char quit(0); while(quit != 'q') { SpaceGraph.makegame(); quit = MoveAll(); } } void Physics::firebullet() { using std::cout; if(bulletfired == 0 ) { bulletfired++; BulletPos[1] = PlayerPos; BulletPos[0] = 0; for(int i = 17; i > 0; i--) { BulletPos[0] = i; if (CheckColision() == 0) { SpaceGraph.drawbullet(BulletPos[0],BulletPos[1]); MoveAll(); SpaceGraph.makegame(); } else { cout << "You Win !"; exit(1); } } } bulletfired = 0; } bool Physics::CheckColision() { for(int i = 0; i < 2; i++) { if((BulletPos[0] == ShipPos[i][0]) && (BulletPos[1] == ShipPos[i][1])) return 1; else return 0; } }``````

 Graphics.cpp
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647`` ``````#include #include "Graphics.h" #include void Graphics::makegame() { for(int i = 0; i < 20; i++) { for(int j = 0; j < 40; j++) { if(i == 0 || i == 19 || j == 0 || j == 39) drawGame[i][j] = '!'; else drawGame[i][j] = ' '; } } } void Graphics::drawgame() { using namespace std; system("cls"); for(int i = 0; i < 20; i++) { for(int j = 0; j < 40; j++) { cout<
anyone ?
Do you iterate the bullet over every single invader to check for collision?
yeah i think so
 ``12345678`` ``````{ for(int i = 0; i < 2; i++) { if((BulletPos[0] == ShipPos[i][0]) && (BulletPos[1] == ShipPos[i][1])) return 1; else return 0; }``````

so it finds ShipPos[0][0] and ShipPos [0][1] but not ShipPos [1][0] and ShipPos [1][1]
even though its in a loop that it can't exit before its checked.
Unless its because if it returns 0 on the first one it jumps out of the loop.
If thats the case how would i solve it ?
ok solved it
stupid mistake.
Just had to put the return 0 outside the loop.
Topic archived. No new replies allowed.