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

2D Collision Detection between tiles and free moving entities

9 posts in this topic

I'v been working on this game for "OneGameAMonth". So far it's been going pretty good, with only minor hiccups until just recently. I'm trying to do the collision and I just can't figure out what to do!

My world is built on a grid ( 2D array ) and uses 32x32 tiles, my Player and Monsters however are free moving and not bound by the grid, this has caused some problems when trying to do collision.

Here is an image:
535028_530036847035485_539994630_n.jpg

Frankly I don't know how to do it, I tried converting my character coordinates to tile coordinates which works kinda... until you try to move through the small passages or "doorways", then it goes wild, suddenly you can walk through walls and what not.

The code looks something like this:
 

void Player::update(float dt, const Tiles& tiles)
{
	float x = m_x;
	float y = m_y;
	
	if( sf::Keyboard::isKeyPressed( sf::Keyboard::Up ) )
	{
		y -= m_speed;
	}
	if( sf::Keyboard::isKeyPressed( sf::Keyboard::Down ) )
	{
		y += m_speed;
	} 
	if( sf::Keyboard::isKeyPressed( sf::Keyboard::Left ) )
	{
		x -= m_speed;
	} 
	if( sf::Keyboard::isKeyPressed( sf::Keyboard::Right ) )
	{
		x += m_speed;
	}
	
	int topY = y / 32;
	int leftX = x / 32;
	int bottomY = ( 32 + y )/32;
	int rightX = ( 32 + x )/32;
	
	if( tiles[leftX][topY] == 1 && tiles[rightX][topY] == 1)
	{
		m_x = x;
	}
	
	if( tiles[leftX][bottomY] == 1 && tiles[rightX][bottomY] == 1)
	{
		m_y = y;
	}
	
	m_sprite.setPosition(m_x, m_y);
}

 

I'm out of ideas and don't know what to do!

EDIT:
I'm using C++ and SFML

Edited by Moonkis
0

Share this post


Link to post
Share on other sites

The way I done my collision in Java was to bound each object that requires collision detection to be bounded by a Rectangle object(which is built into the Java language) and you can use the built in intersects method to see if these rectangles overlapped. This type of bounding box or bounding rectangle collision works well for circle, oval or rectangle shaped objects which would pretty much work with the objects you have in the picture above. Your code looks like OOP C++ so there is probably something similar for that language.

 

I am curious about your code. Why are you testing for the elements in a 2D array to be equal to 1? Is that the code to prevent the character from reaching a certain boundary?

Edited by warnexus
0

Share this post


Link to post
Share on other sites

The way I done my collision in Java was to bound each object that requires collision detection to be bounded by a Rectangle object(which is built into the Java language) and you can use the built in intersects method to see if these rectangles overlapped. This type of bounding box or bounding rectangle collision works well for circle, oval or rectangle shaped objects which would pretty much work with the objects you have in the picture above. Your code looks like OOP C++ so there is probably something similar for that language.

 

I am curious about your code. Why are you testing for the elements in a 2D array to be equal to 1? Is that the code to prevent the character from reaching a certain boundary?

 

Yes, SFML does have a rectangle class, but I'm quite new to collision detection and I didn't quite know what to do with the tiles, seeing as a tile is just a number I can't attach any additional information, such as a bounding box, and really seeing as the map is 1000x1000 I think maybe that many bounding-boxes would be a bit to much?

I'm testing against 1 seeing as that is the walkable tile.

Edited by Moonkis
0

Share this post


Link to post
Share on other sites

You should be able to treat the Rectangle object like a data member(C++)/instance variable(Java) of the Wall class.

 

In Java, it would look like this:

 

public class Wall{

 

private Rectangle rectangle;

 

}

 

In your case, it would look similar to this:

 

public class Tiles{

 

private Rectangle rectangle;

}

 

 

The idea I have is that you can test for collision when the character is close to object it is not suppose to be able to over-lap. That would make the code less expensive.

Edited by warnexus
0

Share this post


Link to post
Share on other sites

You should be able to treat the Rectangle object like a data member(C++)/instance variable(Java) of the Wall class.

 

In Java, it would look like this:

 

public class Wall{

 

private Rectangle rectangle;

 

}

 

In your case, it would look similar to this:

 

public class Tiles{

 

private Rectangle rectangle;

}

 

 

The idea I have is that you can test for collision when the character is close to object it is not suppose to be able to over-lap. That would make the code less expensive.

But I don't want to have a tile class, i.e I don't want to have 1'000'000 bounding boxes....



EDIT:

I came up with this:


void Player::collision(Tiles& tiles)
{
	int startY = ( m_y - 32 ) / 32;
	int startX = ( m_x - 32 ) / 32;
	int endY = ( m_y + 64 ) / 32;
	int endX = ( m_x + 64) / 32;
	
	for( int sy = startY; sy < endY; sy++ )
	{
		for( int sx = startX; sx < endX; sx++ )
		{
			if( tiles[sx][sy] != 1 && tiles[sx][sy] != 5 )
			{
				sf::IntRect wall;
				wall.top = sy * 32;
				wall.left = sx * 32;
				wall.width = 32;
				wall.height = 32;
				m_boundingbox.top = m_nextY+20;
				m_boundingbox.left = m_nextX+8;
				if(m_boundingbox.intersects(wall))
				{
					return;
				}
				
			}
		}
	}
	m_x = m_nextX;
	m_y = m_nextY;
}

 

It works BUT... I'm not sure how to make the character "slide" against a wall if the collision is only because of one axis. Example:
If you are moving both up and right and there is an collision because you are moving upwards you don't want to move up but towards the left instead, making the character slide on the x-axis ( to the right ). 

Edited by Moonkis
0

Share this post


Link to post
Share on other sites

That's true about lots of bounding boxes. Now that I think about it some more, you might want to try creating a bounding box around the object only when the collision function is called.

 

How can the character be towards the left but slide on the x axis to the right? blink.png

 

On a side note: you should try to avoid "magic numbers" instead replace the value of a variable with a variable name.

Edited by warnexus
0

Share this post


Link to post
Share on other sites

until you try to move through the small passages or "doorways", then it goes wild, suddenly you can walk through walls and what not.

I've encountered this problem too. Try giving your free moving entities a slightly smaller rectangle, so that there won't be any exact fits.

0

Share this post


Link to post
Share on other sites

It works BUT... I'm not sure how to make the character "slide" against a wall if the collision is only because of one axis. Example:
If you are moving both up and right and there is an collision because you are moving upwards you don't want to move up but towards the left instead, making the character slide on the x-axis ( to the right ).

 

What you want to do is make two collision checks. First, check for collision along the X axis (the destination X value + the old Y value) then check for collision along the Y axis (the destination Y value + the "old" X value. Note that this X value would have been updated to the new position if the previous check had no collision).

0

Share this post


Link to post
Share on other sites

It works BUT... I'm not sure how to make the character "slide" against a wall if the collision is only because of one axis. Example:
If you are moving both up and right and there is an collision because you are moving upwards you don't want to move up but towards the left instead, making the character slide on the x-axis ( to the right ).

 

What you want to do is make two collision checks. First, check for collision along the X axis (the destination X value + the old Y value) then check for collision along the Y axis (the destination Y value + the "old" X value. Note that this X value would have been updated to the new position if the previous check had no collision).


Yes I was trying for that last night but it didn't want to work, I revamped the code ( you gave me an idea ) and here is the working code if anyone runs into the same problem:
 

void Player::update(float dt, const Tiles& tiles)
{
	float x = m_x;
	float y = m_y;
	
	if( sf::Keyboard::isKeyPressed( sf::Keyboard::Up ) )
	{
		y -= m_speed;
	}
	if( sf::Keyboard::isKeyPressed( sf::Keyboard::Down ) )
	{
		y += m_speed;
	} 
	if( sf::Keyboard::isKeyPressed( sf::Keyboard::Left ) )
	{
		x -= m_speed;
	} 
	if( sf::Keyboard::isKeyPressed( sf::Keyboard::Right ) )
	{
		x += m_speed;
	}
	
	// Find the nerest tiles around the player.
	int startY = ( m_y - 32 ) / 32;
	int startX = ( m_x - 32 ) / 32;
	// We add 64 because we need to compensate the sprites width as well.
	int endY = ( m_y + 64 ) / 32;
	int endX = ( m_x + 64) / 32;
	
	// Set up the movement variables.
	bool moveX = true;
	bool moveY = true;
	
	// Iterate through the nerest tiles.
	for( int sy = startY; sy < endY; sy++ )
	{
		for( int sx = startX; sx < endX; sx++ )
		{
			if( tiles[sx][sy] != 1 && tiles[sx][sy] != 5 )
			{
				// For each tile make a boundingbox.
				sf::IntRect wall;
				wall.top = sy * 32;
				wall.left = sx * 32;
				wall.width = 32;
				wall.height = 32;
				
				// Create two boundingboxes for the player, one for X-axis and one for Y-axis.
				sf::IntRect xaxis(x+8,m_y+20,16,8);
				sf::IntRect yaxis(m_x+8,y+20,16,8);
				
				// If we intersect on the x-axis, prevent moving through the x-axis.
				if( xaxis.intersects(wall) )
				{
					moveX = false;
				}
				// If we intersect on the y-axis, prevent moving through the y-axis.
				if( yaxis.intersects(wall) )
				{
					moveY = false;
				}
			}
		}
	}
	// If movement is true, then move accordingly.
	if( moveX ) m_x = x;
	if( moveY ) m_y = y;
}
0

Share this post


Link to post
Share on other sites

you should get rid of the moveX and moveY variables.

If you detect a collision you should move the object along the axis such that it just touches the tile.

You should not reset to previous position.

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