Collision detection with a class not working? Any advice?

So I have gotten a class for my enemy for my game... but now collision has gotten all screwed up :(.

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
class Reaper
{
public:
	void drawReaper();
	void check_reapers();//checks to see if char has hit anything	
	void checkReapersSectors();
	void refreshReapersSector(int i, int j);
	void update();
	void initReaperSectors();
	void LevelUp(int levelNum);
	int reaperHealth;
	Reaper();
	
private:
	int xEnemy;
	int yEnemy;
	
	int direction;
	int alive;
}Reaperd;

Reaper::Reaper(){
	reaperHealth = 20;
	xEnemy = rand() % 600 + 200;
	yEnemy = rand() % 600 + 200;
	direction = rand() % 4;
}


This is my class for enemy ^

And this is the boundingBox I am using:

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
class collisionBox{
public:
	int boundx, boundy, boundheight, boundwidth;
};

short int Sprite_Collide(class collisionBox object1, class collisionBox object2) {
  
	int left1, left2;
	int right1, right2;
	int top1, top2;
	int bottom1, bottom2;

	left1 = object1.boundx;
	left2 = object2.boundy;
	right1 = object1.boundx + object1.boundwidth;
	right2 = object2.boundx + object2.boundwidth;
	top1 = object1.boundy;
	top2 = object2.boundy;
	bottom1 = object1.boundy + object1.boundheight;
	bottom2 = object2.boundy + object2.boundheight;

	if (bottom1 <= top2) return false;
	if (top1 >= bottom2) return false;

	if (right1 <= left2) return false;
	if (left1 >= right2) return false;

	return true;

};
struct collisionBox charHit;
struct collisionBox bossHit;
struct collisionBox reaperHit;
struct collisionBox thunderHit;
struct collisionBox shadowHit;


Then I call this in my runGame() function to get enemy:

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
void Reaper::update()
	{
		Reaperd.initReaperSectors();
		
		do{
			direction = rand() % 4;	
		for(int k = 0; k < 15; k++){
			
		if(direction == 0)
			xEnemy = xEnemy + 5;
		if(direction == 1)
			xEnemy = xEnemy - 5;
		if(direction == 2)
			yEnemy = yEnemy - 5;
		if(direction == 3)
			yEnemy = yEnemy + 5;
		
		Reaperd.checkReapersSectors();

		Reaperd.drawReaper();

		check_reapers();
		
		if(Sprite_Collide(reaperHit, thunderHit) == true){
			reaperHealth = reaperHealth - 10;
			printf("Reaper hit");}
		}
		
		} while(reaperHealth > 0 && charHealth > 0);
	
	}


So what this does is successfully animates a reaper. He zooms around screen. But interaction between reaper and character is all messed up. It will sometimes say reaper and character collided when they are not even close. It won't detect reaperHit or thunderHit either.

I have reaper stored in class, but character is stored using structs and has a different set up. Sprite_Collide(charHit, reaperHit) == true is currently being tested in my character_animation() function. And I also have my thunderball animation in character_animation(), and I animate it using this while loop:

while(character[i].xCoord < 733 && Sprite_Collide(thunderHit, bossHit) == false) {

//draw and move thunderball
}

That DID work till I converted reaper to class. Now thunderball stops at random places on the screen sometimes?

I wonder if it has something to do with xEnemy and yEnemy being private? Idk... im very new to classes :P

I hope all this makes sense and can kind of be visualized :P But I am wondering what might be wrong to get collision detection when sprites are not even close to each other.
Topic archived. No new replies allowed.