Sign in to follow this  

Weird 2D Collision Problem

This topic is 3580 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
// 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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 3580 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this