Tiled Collision Layers?

So I’ve been trying to figure out how to properly import and use Tiled based tmx map files and whatnot. I followed one of the demos (the sprite sheet animations one?) for loading in a map, however I’m not making a platformer, just trying to apply in other ways. Anyways, I was wondering if there was a way to get better/more accurate collision detection with layers, especially with tiles that are transparent in parts of their ‘tile’, for example, a skinny tree trunk that much of the tile is transparent. However, with similar code to the platforming zombie demo, the character’s rectangle does collide with the transparent corners of tiles that have that design.

I know Tiled has a way to manually cut out and define specific collision boundaries, and I was wondering if there was a way to integrate that into a Monogame project through Extended?

1 Like

Currently there’s very limited support for collision detection in MonoGame.Extended. Although, there is work being done to make it better. Of course, you could have a go at writing something yourself.

Actually, I didn’t know about the collision editor until now. That’s very cool. I’ll have to look into it for MonoGame.Extended in the future.

Try Nez Framework for Tiled stuff, I think it supports them.

PS. @craftworkgames you should probably talk to @prime31 about copying some tiled code to MG Extended, Nez has more stuff supported right now.

Thanks @harry-cpp. I’ve already talked to @prime31 a few times.

Is there a way to have another CollisionWorld constructor that takes a TileObjectGroup instead of just the TiledTileLayer?

I don’t really know where to begin in regards to building my own, but say I have defined some objects in an object layer in Tiled, for example like in this screen cap (simplified to increase visibility) http://puu.sh/odyCI/2d9ae591fb.png, and then in code have something akin to:

CollisionWorld world;
TiledMap map;

// initializations and whatnot

TiledObjectGroup objCollision = new TiledObjectGroup(“collisions”, map.GetObjectGroup(“obj collision”).Objects);

world.CreateGrid(objCollision);

world.CreateActor(player);

It wouldn’t be a grid, perse, but it would be a series of shapes that maybe you can’t go through/enter i.e. collide with?

Any ideas?

Hi @IllustriousEpoch

The current API for collision detection is going hopefully change for the better very soon.

To make it work with the current API you would need to loop over all your collision objects in the map when the map is loaded and add them to the collision world.

How do I do that with TileObjects? Do I use CreateActor? If so, the objects don’t implement the IActorTarget interface.

The current API is to use CreateActor() yes. Your collision game object would need to implement the IActorTarget interface. See abstract data type here
https://github.com/craftworkgames/MonoGame.Extended/blob/develop/Source/MonoGame.Extended/Collisions/IActorTarget.cs

Right, so now, how do I tell the player actor to collide with the object actors? Right now, the player is just walking through them. Something to do with the OnCollision definitely, but I don’t really see where that is called other than maybe for a grid cell based CollisionWorld using cell flags?

You just need to call Update() on the collision world in your game update logic every frame. It will check for collisions among the added collision actors and invoke the ICollidable.OnCollision() for any colliding collision actors.

Like I said, the API is most likely going to change for the better. If you have any feedback on how you would expect or like the API to be, from wiring up collision objects to collision callbacks, let us know.

I am calling Update for it, still nothing. Collision worked for when I was doing TileLayer based collision, not object layer, which is the current problem.

@IllustriousEpoch Just to be clear. @LithiumToast is currently working on making the collision API better. The current API doesn’t currently support object layer collision out of the box. So you have 2 choices:

  1. Either wait until we’ve improved the API an a future version.
  2. Try implement something yourself. The library is open source, so you can always change it do your liking.

Either way your feedback is greatly appreciated. We’ll prioritize the to-do list accordingly. These things do take time though, so please be patient and understand that we are doing everything we can to help you.

No yeah, I understand collision doesn’t currently support what I am trying do, and you guys are working on it, just trying to figure out something in the meantime. I just don’t fully understand how actors and collision go hand in hand. Do all actors collide with other actors? Is there something I can try doing to make the actors created from the Object Layer (essentially getting their position and size, to set their BoundingBox) react to the player actor colliding with them?

Or should I just wait for now. If so, is there a rough estimate when something like this might be added?

Check the code Update() method in the class

https://github.com/craftworkgames/MonoGame.Extended/blob/develop/Source/MonoGame.Extended/Collisions/CollisionWorld.cs

It iterates the collision actors (updating their motion physics for gravity in the process) and checks if there are any collision objects in the collision grid that occupy the same space. If so, it invokes the OnCollision() callback for the collision actor.

I didn’t write that code myself (hi, Dylan!), and there are definitely ways to make the collision detection process more performant and easier to use; we are working towards that goal.

In your case, my guess is either the collision grid is null, the actors we not added to the collision world, or the bounding volume (rectangle for this API) is not the correct for the collision actors in your setup.