View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Having problems with collision detection in 2d platformer

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

19 replies to this topic

### #1ISDCaptain01  Members

Posted 13 November 2013 - 12:49 AM

Here is my collision detection function:

/Collision detection function
int inside(int x, int y, int left, int top, int right, int bottom)
{
if(x > left && x < right && y > top && y < bottom)
return 1;
else
return 0;

}


I want to detect collision when my character touches a coin/ring/object for example I do this:

//checks if player collected a ring
void collectRings()
{
int n, x, y, x1, y1, x2, y2;

for(n = 0; n < RINGS; n++)
{
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 - mapxoff;
y1 = rings[n]->y - mapyoff;
x2 = x1 + rings[n]->width;
y2 = y1 + rings[n]->height;

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


So If the center of my character goes inside the bounding box of a ring it should detect the collision, but it doesn't. For some reason nothing happens. I don't know why. If someone knows why, please let me know.

### #2ultramailman  Prime Members

Posted 13 November 2013 - 01:33 AM

What's mapxoff and mapyoff? Can they change? Do the position of the rings depend on them? Also, there's a shorter way to write your "inside" function.

### #3ISDCaptain01  Members

Posted 13 November 2013 - 01:52 AM

What's mapxoff and mapyoff? Can they change? Do the position of the rings depend on them? Also, there's a shorter way to write your "inside" function.

mapxoff/mapyoff are my camera variables. They move along with my player and yes the position of the rings depend on them. If they are not in range of my camera, they don't render, heres the code in case:


//update the map scroll position
//camera coordinates
mapxoff = player->x + player->width/2 - WIDTH/2 + 10;
mapyoff = player->y + player->height/2 - HEIGHT/2 + 10;

//Avoid moving beyond the map edge
if(mapxoff < 0)
mapxoff = 0;
if(mapxoff > mapwidth*mapblockwidth - WIDTH)
mapxoff = mapwidth*mapblockwidth - WIDTH;
if(mapyoff < 0)
mapyoff = 0;
if(mapyoff > mapheight*mapblockheight - HEIGHT)
mapyoff = mapheight*mapblockheight - HEIGHT;


### #4ne0_kamen  Members

Posted 13 November 2013 - 06:44 AM

What's mapxoff and mapyoff? Can they change? Do the position of the rings depend on them? Also, there's a shorter way to write your "inside" function.

mapxoff/mapyoff are my camera variables. They move along with my player and yes the position of the rings depend on them. If they are not in range of my camera, they don't render, heres the code in case:


//update the map scroll position
//camera coordinates
mapxoff = player->x + player->width/2 - WIDTH/2 + 10;
mapyoff = player->y + player->height/2 - HEIGHT/2 + 10;

//Avoid moving beyond the map edge
if(mapxoff < 0)
mapxoff = 0;
if(mapxoff > mapwidth*mapblockwidth - WIDTH)
mapxoff = mapwidth*mapblockwidth - WIDTH;
if(mapyoff < 0)
mapyoff = 0;
if(mapyoff > mapheight*mapblockheight - HEIGHT)
mapyoff = mapheight*mapblockheight - HEIGHT;


You should have everything in the same coordinate system. For collision detection, you don't need to "scroll" the world using camera offset, just use the object's world coordinates like this :

//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 ;
y1 = rings[n]->y;
x2 = x1 + rings[n]->width;
y2 = y1 + rings[n]->height;

Hope this helps,

Kamen

### #5Eck  Members

Posted 13 November 2013 - 08:28 AM

Set a breakpoint in your function (F9 by default in Visual Studio) and you'll see exactly what's going on. You might want to move into the region first and then set the breakpoint in your code. You'll be able to see which line is failing and why via the Watch window or Locals window.

The problem could be alot of things. Anything from mismatched coordinate systems to oops, I forgot to actaully call my collision detection function. But a breakpoint will let you step through the code and see exactly what's happening.

- Eck

EckTech Games - Games and Unity Assets I'm working on
Still Flying - My GameDev journal
The Shilwulf Dynasty - Campaign notes for my Rogue Trader RPG

### #6McGrane  GDNet+

Posted 13 November 2013 - 09:57 AM

Only have time to have a quick look over at this ... also didnt have time to read the replies - so sorry if it has been said

if(x > left && x < right && y > top && y < bottom)


Unless your screen coards are the opposite of what im thinking - should y not be greater then bottom and less then top ? - and not the other way around

### #7ultramailman  Prime Members

Posted 13 November 2013 - 10:14 AM

mapxoff/mapyoff are my camera variables. They move along with my player and yes the position of the rings depend on them. If they are not in range of my camera, they don't render, heres the code in case:

kamen is right, positions of objects should not depend on the camera. A good way to do what you want (render only what's in the camera) is simply use a rectangle for the camera. Then if objects are in the camera rectangle, render them. When rendering, use object position - camera position.

Edited by ultramailman, 14 November 2013 - 04:44 PM.

### #8ISDCaptain01  Members

Posted 13 November 2013 - 01:03 PM

this is how I draw render the rings to the screen:

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);
}
}
}


Well its not that collision is not detected at all, it just detected way off, or sometimes not detected at all

### #9ultramailman  Prime Members

Posted 13 November 2013 - 01:11 PM

draw_sprite(buffer, ring_images[rings[n]->curframe], rings[n]->x - mapxoff, rings[n]->y-mapyoff);

You are drawing using object position minus the camera position. That's good. But for collision detection, the real position should be used, so simply remove mapxoff and mapyoff from your collision detection, because collision shouldn't have anything to do with the camera. (what kamen said)

### #10Moe091  Members

Posted 14 November 2013 - 03:33 PM


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 - mapxoff;
y1 = rings[n]->y - mapyoff;
x2 = x1 + rings[n]->width;
y2 = y1 + rings[n]->height;

//now check for collision
if(inside(x, y, x1 + mapxoff, y1 + mapyoff, x2 + mapxoff, y2 + mapyoff)) //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;
}
}


Try replacing the inside of your loop with this, all i did was replace the line:
if(inside(x, y, x1, y1, x2, y2))
with:
if(inside(x, y, x1 + mapxoff, y1 + mapyoff, x2 + mapxoff, y2 + mapyoff))

I think that the problem is that your checking the x and y coordinates of your player(which you aren't subtracting the map x and y offsets from), against the x1/x2/y1/y2 variables in which you are subtracting the mapyoff and mapxoff variables from.
x1 = rings[n]->x - mapxoff;
y1 = rings[n]->y - mapyoff;

that part there is messing you up, now your collision detection is off by whatever the value of mapxoff and mapyoff are. I bet the collisions get further and further off the further your screen scrolls right? because these values are increasing.

I only glanced over the other responses but I think this is basically what the other people where saying.

I'd suggest not calculating/caching the mapxoff and mapyoff values in objects positions at all, and only using them in your actual render function. If you don't understand anything post back and I'll try to elaborate more

### #11Moe091  Members

Posted 14 November 2013 - 03:38 PM

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

### #12ISDCaptain01  Members

Posted 14 November 2013 - 06:05 PM

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, 14 November 2013 - 06:24 PM.

### #13ISDCaptain01  Members

Posted 14 November 2013 - 06:35 PM

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

### #14Moe091  Members

Posted 16 November 2013 - 03:29 PM

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?

### #15ISDCaptain01  Members

Posted 16 November 2013 - 08:36 PM

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, 16 November 2013 - 08:40 PM.

### #16Moe091  Members

Posted 16 November 2013 - 09:43 PM

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

### #17ISDCaptain01  Members

Posted 17 November 2013 - 01:27 PM

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

### #18Moe091  Members

Posted 18 November 2013 - 04:04 PM

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

### #19ISDCaptain01  Members

Posted 19 November 2013 - 01:04 AM

Here you go:

http://www.sendspace.com/file/snqk2y

Edited by ISDCaptain01, 19 November 2013 - 01:13 AM.

### #20ISDCaptain01  Members

Posted 19 November 2013 - 01:43 AM

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, 19 November 2013 - 01:44 AM.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.