Weird 2D Collision Problem

Started by
3 comments, last by os3330 16 years, 2 months ago
I have this odd problem with my collision. It works perfectly fine except in one case. When I start firing like crazy towards an enemy ship or something similar, the bullet will disappear (it's supposed to), but the enemy ship won't. Seems like a simple error, right? When I get closer to the enemy however, the enemy disappears when I shoot it then. Point blank range isn't what I'm aiming for here. I don't know where to begin with this problem. Is the problem in the collision detection or is how I'm implementing the collision detection? Can it be something else? I'm running out of options on how to tackle this thing. Here's the code (Visual Studio 2005): http://rapidshare.com/files/92546401/psunrise_4.zip.html
Advertisement
spr.move();spr.show();e.show();  <------env.show();// Updates the screen.shell->update();


You're drawing the enemy regardless of whether it is in the environment.

Even so, It would still be drawn with env.show() as you're not removing enemy from env. You need to call enemy's #remove when it collides.

Also I think the enemy disappearing when you shoot at point blank range might be more to do with overlapping sprites than anything else.
--------------------------------------"Those alien bastards are gonna pay for ruining my ride !"
// Removes elements of like type and checks the remaining sprites for collision.bool Character::sprite_collision(){	// Gets the objects on the screen.	std::vector<Sprite*> sprites = env->get_objects();	// Checks with every single sprite if they have collided.	for ( unsigned int index = 0; index < sprites.size(); ++index ) {		// Doesn't check the sprite that's currently being checked.		if ( sprites.at( index ) == this ) {			continue;		}		if ( check_collision( this, sprites.at( index ) ) ) {			return true;		}	}	// No collision.	return false;}
Here's where I check if it actually collides (Character is the Enemy's parent class). In the move() function, I decide whether the enemy should be removed or not. Here's the Enemy move() function:
// Moves the enemy.  Enemies have a different moving style from the player.void Enemy::move(){	// Moves it.	offset.x += xVel;	offset.y += yVel;	// If the enemy went out of bounds.	if ( offset.x < 0 || ( offset.x + offset.w > env->get_surface()->w ) ||		offset.y < 0 || ( offset.y + offset.h > env->get_surface()->h )) 	{		// The enemy's no longer needed.		remove();	}		// If the collided with another sprite.	if ( sprite_collision() == true ) {		// The enemy loses health.		health -= 1;	}}
Checking for health is done at the handle_events() function, in the Character class.
// Handles any character event.void Character::handle_events(SDL_Event *event){	// Checks if the character has any health left.	if ( health <= 0 ) {		remove();	}}
Is there any other instance that I forgot to remove the enemy?
Have you confirmed that those code-points are hit during execution? I took a quick look again and It looks like the enemy's #remove is never called as the bullet's collision detection removes the bullet, and then the collision check for enemy will never return true.
--------------------------------------"Those alien bastards are gonna pay for ruining my ride !"
I did realize that while the Bullet's remove() was being called, the Enemy's remove() wasn't.

The computer deleted the instance of the bullet before the Enemy had a chance to check itself for collision. So what I did was I set up a boolean, needsDeletion. Whenever a collision happens (or anything that has to be deleted), needsDeletion is set to true. Then, in the Environment class rendering method, I then chuck out the objects that need to be deleted, then draw the rest (an if/else statement).

Hey, thanks for replying. If I didn't have it set up to send me an e-mail, I probably would have never come back in here.

This topic is closed to new replies.

Advertisement