Public Group

Having problems with collision detection in 2d platformer

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

Recommended Posts

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.

Share on other sites

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.

Share on other sites

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;


Share on other sites

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

Share on other sites

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

Share on other sites

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

Share on other sites

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

Share on other sites

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

Share on other sites

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)

Share on other sites

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

• 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!

• 14
• 30
• 9
• 16
• 22