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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#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
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include "Physics.h"

using namespace std;

int main()
{
Physics SpaceInvader;
SpaceInvader.Play();
}

Physics.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <iostream>
#include "Physics.h"
#include <conio.h>
#include <stdlib.h>
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include "Graphics.h"
#include <cstdlib>
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<<drawGame[i][j];
        }
        cout<<endl;
    }
}
void Graphics::drawplayer(int x , int y)
{
    char X = 'X';
    drawGame[x][y] = X;
    drawgame();
}
void Graphics::drawspaceship(int x , int y)
{
    char X = '@';
    drawGame[x][y] = X;
}
void Graphics::drawbullet(int x , int y)
{
    char X = '|';
    drawGame[x][y] = X;
}
anyone ?
Do you iterate the bullet over every single invader to check for collision?
yeah i think so
1
2
3
4
5
6
7
8
{
    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.