Original Post
Ive been writing some code that detects collisions between 2 different groups of objects, one is a line of 'enemy' squares which move down and the other objects are player controlled projectiles. basically its the start of a space invaders type clone the code seems to animate the objects fine, but the collision detection function is really strange. The first square in the enemy row always disapears straight away, and the rest will disapear at random intervals as they mvoe down. I added a print statement to say when the objects collide, but it just repeats over and over regardless whether the projectile objects are actually close to the enemy objects or not. I think the problem must be the bounding spheres arent working correctly, but I cant spot any fault in the code which creates them. So could someone take a look and see if they spot anything for me? Im pretty sure the problem is with the collision detection function but I cant really figure out what it is, anyone have a few minutes? -mounty [Edited by - mounty0 on November 24, 2004 6:59:55 AM]
/*this struct is used to store the variables for both the enemy squares and the projectile objects:*/
typedef struct
{
float xpos; //x,y position of the object
float ypos;
float radius; //used for sphere collision detection
bool active; //if true, draws the object.
float xsize; //dimensions of the object
float ysize;
float angle2; //angle of movement, not used yet
}BoundingSphere;
BoundingSphere bullet[5]; //creates 5 instances of each
BoundingSphere enemies[5];
/*in my init() function, I set the values of the bullet and enemy object structs */
for (int i = 0; i < 5; i++)
{
bullet.active = false;
bullet.xpos = 0;
bullet.ypos = 0;
bullet.radius = 2.021/2;
bullet.xsize = 1.2;
bullet.ysize = 1.2;
bullet.angle2 = 0;
}
for (int i = 0; i < 5; i++)
{
enemies.active = true;
enemies.xpos = i*5;
enemies.ypos = 0;
enemies.radius = 4.021/2;
enemies.xsize = 2;
enemies.ysize = 2;
enemies.angle2 = 0;
}
/*and collision detection function between the enemy objects and the bullets*/
void collisionDetect2(BoundingSphere bullet[], BoundingSphere enemies[])
{
float dist2[5]; //distance between projectile and enemy object
for(int i = 0; i < 5; i++)
{
dist2 = sqrt(((bullet.xpos-enemies.xpos)*(bullet.xpos-enemies.xpos))+((bullet.ypos-enemies.ypos)*(bullet.ypos-enemies.ypos)));
if (dist2 < (bullet.radius + enemies.radius))
{
enemies.active = false;
printf("Collide\n");
bullet.active = false;
}
}
glutPostRedisplay();
}