• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
ISDCaptain01

Having problems with collision detection in 2d platformer

19 posts in this topic

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.

2

Share this post


Link to post
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.

1

Share this post


Link to post
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;
0

Share this post


Link to post
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

2

Share this post


Link to post
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

1

Share this post


Link to post
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

0

Share this post


Link to post
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
1

Share this post


Link to post
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

0

Share this post


Link to post
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)

1

Share this post


Link to post
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
 

1

Share this post


Link to post
Share on other sites

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

1

Share this post


Link to post
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
0

Share this post


Link to post
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

0

Share this post


Link to post
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?

1

Share this post


Link to post
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
0

Share this post


Link to post
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

0

Share this post


Link to post
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
0

Share this post


Link to post
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

0

Share this post


Link to post
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
0

Share this post


Link to post
Share on other sites

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  
Followers 0