Problem: Check a collision between 2 objects in graphics mode!

I have these files:
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
//ball.h
#ifndef BALL_H
#define BALL_H

class ball
{
	int x, y, vx, vy, r;
	public:
	ball(int a, int b, int s, int dx, int dy)
	{
		x= a; y= b; r= s;
		vx= dx; vy= dy;
	}
	friend int chek_in(class bar &Ob_bar, class ball &Obj_b);
	void move();
	void setCord(char c);
} ;

void ball::move()
{
	if (y>= 490) y= 0;
	setcolor(getbkcolor()); setfillstyle(1, getbkcolor());
	fillellipse(x, y, r+4, r+4);//erase

	setcolor(RED); setfillstyle(1, GREEN);
	x+= vx; y+= vy; vx= 0;//reset vector x, not vy
	fillellipse(x, y, r, r);//draw
}

void ball::setCord(char c)
{
	switch (c)
	{
		case 75: vx-=10; break;
		case 77: vx+=10;
		default: break;
	}
}
#endif 

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
//bar.h
#ifndef BAR_H
#define BAR_H
class bar
{
	int x, y, vy, wx, hy;
	public:
	bar(int a, int b, int c, int d, int dy)
	{
		x= a; y= b; wx= c; hy= d;
		vy= dy;
	}
	void move();
	friend int chek_in(class bar &Ob_bar, class ball &Obj_b);
} ;
void bar::move()
{
	if (y<= -10)
	{
		y= 460;
		x= 240;
	}
	setcolor(getbkcolor()); setfillstyle(1, getbkcolor());
	bar(x- wx/2, y-hy/2, x+wx/2, y+hy/2);//erase

	setcolor(WHITE); setfillstyle(1, YELLOW);
	y+= vy;
	bar(x- wx/2, y-hy/2, x+wx/2, y+hy/2);//erase
}
#endif 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//Initg.h
#ifndef INITG_H
#define INITG_H

#include <dos.h>
#include <graphics.h>
#include <conio.h>
#include <stdlib.h>

int init_graph()
{
	int gd=DETECT, gm, err;
	initgraph(&gd, &gm, "e:\\tc\\bgi");
	err= graphresult();
	if (err!= grOk) return 0;
	return 1;
}
#endif 

and
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
//main file
#include <iostream.h>
#include "initg.h"
#include "ball.h"
#include "bar.h"

#define G 2
//gravity

int chek_in(class bar &Ob_bar, class ball &Obj_b)
{
	int 	left= Ob_bar.x- Ob_bar.wx/2,
		rght= Ob_bar.x+ Ob_bar.wx/2,
		y_dist= Ob_bar.y-Ob_bar.hy/2- (Obj_b.y+ Obj_b.r);//reverse Oy
	if (left< Obj_b.x+Obj_b.r && Obj_b.x-Obj_b.r< rght)
	{
		if (-10< y_dist && y_dist<= 0)
		{
			Obj_b.y= Ob_bar.y- Ob_bar.hy/2- Obj_b.r;
			Obj_b.vy= Ob_bar.vy;
		}
		else Obj_b.vy= G;
		return 1;
	}
	Obj_b.vy= G;
	return 0;
}

int main()
{
	init_graph(); srand(time(NULL));


	int posx= 240;
	class bar bar0(posx     , 479, 60, 10, -2);
	class bar bar1(posx	, 300, 60, 10, -2);
	class bar bar2(posx	, 100, 60, 10, -2);
	class ball bll(posx	, 200, 10, 0, 0);
	class bar *ptobar[3];
	ptobar[0]= &bar0; ptobar[1]= &bar1; ptobar[2]= &bar2;
	int quit= 0; char c; int i;
	while (!quit)
	{
		if (kbhit())
		{
			c= getch();
			if (c== 27) quit= 1;
			bll.setCord(c);
		}
		for (i= 0; i< 3; ++i)
			if (chek_in( *ptobar[i], bll) )	break;
		bll.move();
		bar0.move();
		bar1.move();
		bar2.move();
		delay(10);
	}
	return 0;
}


The problem is my program can't perform well in checking collision. Well...U can run the program in few seconds to see the problem, and then help me fix it, please!
Last edited on
Sorry, I can't test your program because I don't have conio, graphics or dos (they are not standard). Can you explain the problem?
1
2
3
4
5
void bar::move()
{
	//...
	bar(x- wx/2, y-hy/2, x+wx/2, y+hy/2);// ? you create an object, but you aren't using it
}


I suppose that check_in is the one that detect the collision. if (-10< y_dist && y_dist<= 0) why -10?
if (left< Obj_b.x+Obj_b.r && Obj_b.x-Obj_b.r< rght) eh? the rightmost point of the circle against the left of the bar? I suppose that right > left, so what is the purpose of the other comparison?

To check the collision between a circle and a line, you calculate the height of the triangle AOB. (you can use cross product to find the area)
However you can use some restrictions in the problem (by instance the bar is always horizontal)
Last edited on
The 'bar(x- wx/2, y-hy/2, x+wx/2, y+hy/2)' this is not the class bar, it is a function to draw and fill a rectangle defined in graphics.h
By the way, I figured out the solution. Thanks for your reply.
Topic archived. No new replies allowed.