Skip to main content
GameDev.net gamedev.net
🔒 Locked

collision troubles

Started by mounty0 Nov 21, 2004 at 12:07 PM 7 replies 1.1k views
Original Post
mounty0
mounty0
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?

/*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();
}





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]
Zakwayda
Zakwayda
There's a typo in this line:

dist2 = sqrt(((bullet.xpos-enemies.xpos)*(bullet.xpos-enemies.ypos))+((bullet.ypos-enemies.ypos)*(bullet.ypos-enemies.ypos)));

You have a 'ypos' where you want an 'xpos'. Try fixing that and see if it solves your problem.
mounty0
mounty0
ah well spotted, should have noticed that myself. got a bit square eyed after a few hours ~_^

That fixed the 'random disapearing' of my enemy objects.
Still have a couple of niggling problems though.
one, collision only seems to detect between certain projectiles and certain enemies, cant figure out why that is.

2nd, When I 'destroy' an enemy, the object disapears but the collision sphere seems to stay there, if a bullet hits that spot again it still disapears.


[Edited by - mounty0 on November 24, 2004 7:25:55 AM]
USArmyMortars
USArmyMortars
Well as for your second problem, i am assuming that this might fix your problem. When an enemy is destroyed, the only part of the structure you are changing is the active member. All the other data is still there. Try this and let me know if it works

DestroyEnemy(BoundingSphere& enemies){	enemies.active = false;	enemies.xpos = 0; 	enemies.ypos = 0;	enemies.radius = 0;	enemies.xsize = 0;	enemies.ysize = 0;	enemies.angle2 = 0;}
HHC 1st Battalion, 5th Infantry Regiment1st Brigade (SBCT), 25th Infantry DivisionRest In Peace SSG Julian Melo
vHaB
vHaB
Actually one thing that should speed everything up, as well as solving the problem of detecting collisons for enemies that are no longer there:

As the first line inside your for loop, you need to check if the enemy or bullet is active. If either one is inactive, you can skip all the distance detection. It also means you don't have to zero out your struct params.

In other words, you're using the active flag to draw, but you can also use it to detect collisions as well. If something's not active (and therefore not drawn) then it shouldn't collide either.

Good luck.

mounty0
mounty0
thanks for the replies. and Vhab you were spot on, that works for it. thanks!

[Edited by - mounty0 on November 24, 2004 12:03:37 PM]
mounty0
mounty0
Ive a new question, I want to animate an object after collision happens, ive tried including a test in my collision function above which creates a simple square at the moment, but it only animates for half a second at collision then dispears again

[Edited by - mounty0 on November 25, 2004 3:02:44 PM]
_DarkWIng_
_DarkWIng_
Quote:
Original post by mounty0
Ive a new question, if I want to animate an object after collision happens, how could this be done? ive tried including a test in my collision function above like:
*** Source Snippet Removed ***
animateBomb function is only an simple square at the moment, but it only animates for half a second at collision then dispears again, however the enemies[j].active remains false, so I thought the square object would stay on the screen?

You have something like:
if (enemies[j].active) {   //...do some stuff..  if (enemies[j].active==false) {     AnimateThingy();  }}

AnimateThing is called only once when you flip active status then never agian becouse it cn't be reached. Move the if statement out of block or something like that.. Bettor solution would be to use some kind of enumerator for ship state. Instead active=true/false you would have state=alive/exploding/dead/...

PS: This really doesn't have anything to do with OpenGL.
You should never let your fears become the boundaries of your dreams.
mounty0
mounty0
youre right of course, thanks all the same though. my code is based pretty much on that. Guess i got a bit carried away, I'll be back when i want to discuss more opengl specific stuff




-mounty

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.