Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Collision in tmxlite #22

Closed
Bramwell-Simpson opened this issue Nov 9, 2017 · 2 comments
Closed

Collision in tmxlite #22

Bramwell-Simpson opened this issue Nov 9, 2017 · 2 comments

Comments

@Bramwell-Simpson
Copy link

I have tried nearly everything to have proper collision plz help :)

@fallahn
Copy link
Owner

fallahn commented Nov 9, 2017

This is not really relevant to the library, and even if it were you do not state what 'everything' is. There are plenty of articles on the internet such as this, this and this which describe collision detection and resolution. When you have understood what the articles are explaining, think about the data they require, such as the shapes or objects - then maybe tmxlite might be useful in loading that data from a tmx file for you to work with.

@DennisSc
Copy link
Contributor

DennisSc commented Jan 8, 2018

tmxlite does only load maps - it does not detect collisions, and you'll have to do it in your own code.
For objects, it is simple since you probably draw them separately anyway and then can use SFML's "getBoundingBox()" (or equivalents in other frameworks) to check if this collides with anything.

If you want to check for collision against certain types of tiles (not objects) like when using a collision layer, then you can get the tile values like so (assuming collision layer is layer 0)

        tmx::Map map;
        map.load("some-map.tmx");
        MapLayer layerZero(map, 0);
        MapLayer layerOne(map, 1);
        MapLayer layerTwo(map, 2);  ///and so on

	const auto& allLayers = map.getLayers();
        const auto& collisionLayer = *dynamic_cast<const tmx::TileLayer*>(allLayers[0].get());
	std::vector<tmx::TileLayer::Tile> tiles = collisionLayer.getTiles();
	
	//now you can read the values of single tiles of your collision layer:
	std::cout << "Tile 35 " << " contains value " << tiles[34].ID << std::endl;
	
	//or iterate through some tiles:
	for (int i = 0; i < 10; i++)
	{
		std::cout << "Tile " << i << " has value " << tiles[i].ID << std::endl;
	}

	// or access all of them
	for (tmx::TileLayer::Tile tile : tiles)
	{
		std::cout << tile.ID << std::endl;
	}

(note that "ID" in this case probably means "integer data" - not "identifier"!)

and then check if you can walk there or not ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants