0
101 Nov 27, 2003 at 17:55

Hi everybody ..

I’m trying to let two sheres bounced when they hit each other and when they hit a rectangle .. It works correctly when the hit each other but with the rectangle it didn’t work :) :huh: .. I don’t know why ? ..

Here is the code I used :

//if my first sphere is :
glTranslatef(x1,y1,0.0f);
glutSolidSphere( 5.0,50,50);

The second one is:
if my first sphere is :
glTranslatef(x2,y2,0.0f);
glutSolidSphere( 10.0,50,50);

dx = x2 - x1 or x1 - x2 !
dy = y2 - y1 or y1 - y2!
dz = 0;

distance = sqrt(dx*dx + dy*dy + dz*dz)

if( distance <= (15.0) then contact= true;


This is for the bouncing of the two spheres ..

For the bouncing between the two shperes and the rectangle :

dx1 = dragx - Ball1x;
dx2 = Ball2x - dragx;
dy1 = dragy - Ball1y;
dy2 = Ball2y - dragy;

distance1 = sqrt(dx1*dx1 +dy1*dy1);
distance2 = sqrt(dx2*dx2 +dy2*dy2);

if (distance1<= 10.0f) { vx=-vx; angle=-angle;vy=-vy;}
if (distance2<= 10.0f) { vx2=-vx2; angle2=-angle;vy2=-vy2;}


Where the parameters of the rectangle :

// Rectangle para.s
int x1=10.00f ,yy=-40.00f,x2=-60.00,y2=-50.00, D=0.0;

glPushMatrix();

glTranslatef(dragx,dragy,0.0f);
glColor3f(1.0, 1.0, 1.0);
glRectf(x1,yy,x2,y2);
glPopMatrix();


Any help ..?

#### 3 Replies

0
101 Jul 23, 2004 at 04:09

I read just a little bit of your code, and this is not the proper way to do collision detection. If you want your game/application to support proper Sphere/Sphere and Sphere/Mesh collision detection then use the following algorithm.

http://www.three14.demon.nl/sweptellipsoid…ptEllipsoid.pdf

Its a very nice article and pretty easy to understand. But you might have a tough time implementing it :D.

-Zul

0
101 Jul 23, 2004 at 07:14

usually this is baldurks job but…

hail my necro master !

0
101 Jul 23, 2004 at 08:21

Wow, I didn’t notice.

I must be slipping..