Public Group

# Having problems with collision detection in 2d platformer

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

## Recommended Posts

Oh i just looked at your render function and it looks like your doing everything the way I'd suggest there. In that case a better solution(the results should be the same but this method just removes unnecessary code) would be this:


if(rings[n]->alive)
{
//find the center of the player
x = player->x + player->width/2;
y = player->y + player->height/2;

//get the rings bounding rectangle
x1 = rings[n]->x; //I removed the - mapxoff here
y1 = rings[n]->y; //and the - mapyoff here.
x2 = x1 + rings[n]->width;
y2 = y1 + rings[n]->height;

//now check for collision
if(inside(x, y, x1, y1, x2, y2)) //THIS IS THE ONLY LINE I CHANGED
{
rings[n]->alive = 0;
stop_sample(ringSound);
play_sample(ringSound, VOL+100, PAN, FREQ, FALSE);
ringCounter++;
score += 10;
}
}


x1 = rings[n]->x;    //I removed the - mapxoff here
y1 = rings[n]->y;    //and the - mapyoff here.

those are the only 2 lines i changed here, subtracting mapxoff and mapyoff only throws off your collision detection and is unnecessary unless you need it for something else besides collision detection

##### Share on other sites

Okay I tried the suggested solution. It work fine for a few rings, after that it doesn't detect anything. Hmmmm maybe it has to do with how I draw my player?

if(LIVES != 0)
{
if(facing)
{
draw_sprite(buffer, player_image[player->curframe], player->x - mapxoff,
player->y - mapyoff);
}
else
{
draw_sprite_h_flip(buffer, player_image[player->curframe], player->x - mapxoff,
player->y - mapyoff);
}
}
else
GameOver();

Edited by ISDCaptain01

##### Share on other sites

Update: I tested the same code with enemies, and it works flawlessly, maybe my ring sprites are too small. Ill update you all once I resize them

##### Share on other sites

hmmm, can you post the code for how you draw your enemies as well as how you draw the rings? and with the rings does it just stop working completely after a few rings or does it work randomly or seem like it's just off?

##### Share on other sites

It just stops working after a few rings, here is the code for both:

rings:

//Spawn the rings across the stage
void initializeRings()
{
int n;
for(n = 0; n < RINGS; n++)
{
rings[n] = new SPRITE;
rings[n]->alive = 1;
rings[n]->x = n * 200;
rings[n]->y = 100;
rings[n]->curframe = 0;
rings[n]->maxframe = 4;
rings[n]->framecount = 0;
rings[n]->framedelay = 4;
rings[n]->width = ring_images[n]->w;
rings[n]->height = ring_images[n]->h;
}
}

void drawRings()
{
int n;

//loop through all ring object
for(n=0; n<RINGS; n++)
{
//If the rings have not been collected yet
if(rings[n]->alive)
{
//Animate each ring
if(++rings[n]->framecount > rings[n]->framedelay)
{
if(++rings[n]->curframe > rings[n]->maxframe)
rings[n]->curframe = 0;
rings[n]->framecount = 0;
}

//now draw each ring to the buffer
draw_sprite(buffer, ring_images[rings[n]->curframe], rings[n]->x - mapxoff, rings[n]->y-mapyoff);
}
}
}


enemy:

//initialize the enemies values
void initializeEnemy()
{
int n;
for(n = 0; n < totalEnemies; n++)
{
spinner[n] = new SPRITE;
spinner[n]->x = n * 200;
spinner[n]->y = 200;
spinner[n]->alive = 1;
spinner[n]->curframe = 0;
spinner[n]->maxframe = 9;
spinner[n]->framecount = 0;
spinner[n]->framedelay = 2;
spinner[n]->width = spinner_image[0]->w;
spinner[n]->height = spinner_image[0]->h;
}
}

//Draw the enemies on screen
void drawEnemy()
{
int n;

//loop through all enemy object
for(n=0; n<totalEnemies; n++)
{
//If the enemies have not been defeated yet
if(spinner[n]->alive)
{
//Animate each enemy
if(++spinner[n]->framecount > spinner[n]->framedelay)
{
if(++spinner[n]->curframe > spinner[n]->maxframe)
spinner[n]->curframe = 0;
spinner[n]->framecount = 0;
}

//now draw each enemy to the buffer
draw_sprite(buffer, spinner_image[spinner[n]->curframe], spinner[n]->x - mapxoff, spinner[n]->y-mapyoff);
}
}
}

Edited by ISDCaptain01

##### Share on other sites

hmmmm, I didn't have much time to look at it by I'm honestly stumped, the problem wasn't what I was expecting it to be.

If you felt like uploading your whole project I'd take a look at it and see if I can figure it out, but that might be a hassle for you and if you just wait a little bit someone who knows c++ better than me might be able to help you out just using the information you already provided.

up to you, I'll check back here later(probably tomorrow morning) to see if I can figure it out or if you posted any more info. In the mean time try to think about what differences there are between your enemy and ring classes and how you draw them, there's gotta be something causing them to behave differently and based on my experience with these kind of annoying bugs it's probably something silly and easily-overlooked

##### Share on other sites
Yeah I think I'm gonna rewrite the all ring functions, I can post my engine, but it's pretty ugly lol. Let me know if u still want it

##### Share on other sites

If you are still having problems after rewriting it you can post it here and I'll take a look, I don't mind the ugliness I let my hobby projects get pretty messy a lot of the time myself

##### Share on other sites

I experimented a bit. I set my code so that if I collide with an enemy the corresponding ring disappears instead of the enemy and the rings did disappear.

hmmmmmm, I have no conclusions lol. Im just gonna finish this game now, screw the rings.

Edited by ISDCaptain01

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 13
• 30
• 9
• 16
• 12