Game Development Community

Tile Map Collision Detection

by Tony Pitman · in Torque X 2D · 07/03/2009 (11:28 pm) · 10 replies

I have a tile map for my game. In the tile map editor I have assigned object types and collision bounds to each tile.

When my player object collides with a tile location how do I determine which type it collided with and better yet just get the object type of that single tile?

I created the OnCollision method and it is getting called when my player object collides with the tile map tiles, but properties like ObjectType are all unknown. Can someone share some code with how to detect the type of tile inside the tile layer I collided with?

#1
10/22/2009 (2:04 pm)
Yes I am having this same problem. For me, I find if i have an entire tilemap with just one block of object type 'x', then the entire tilemap becomes an object of type 'x'. Im trying to set up an On Enter trigger in the T2DTriggerComponent and instead of doing the OnEnter when My object collides with the tile of type 'x', it collides with the entire tilemap. Did you end up solving it Tony?
#2
10/22/2009 (3:06 pm)
No. I quit using the tile map and just have a background image and created transparent objects and then set their collision points. It would have worked much better to have use the tile map, but this solution got the job done.
#3
10/23/2009 (10:48 am)
Yeah I'm about to give up and do somthing like that, but I'd kind of like to work with the engine rather than against it. If anyone has a solution it would be very helpful.
#4
10/23/2009 (11:13 am)
I know how you feel!
#5
10/27/2009 (4:16 am)
In the builder when you create the tilemap, and are editing the tiles, right in there with the brushes etc is an object type. Each tile can have an object type associated. If you select a type it will stay there for each tile until you change it. You also MUST pick an object type and then each tile created will have it. The tiles created before will not have the object type.

I had the same problem, I thought the object type for the tile layer would just drop in.



#6
10/27/2009 (9:37 am)
I am doing this. I select a unique object type for the tile, and it still assigns that object type to the entire tilemap. Even if i select a different object type z for the tilemap itself, then assign another object type x for each tile, it still seems to do the trigger events when, for example, the player enters and leaves the tilemap (not the individual tiles like it should).
#7
11/06/2009 (4:14 pm)
So I've got it so that I can detect the object type each tile object. My problem has been caused by the fact that I was using the t2dTriggerComponent OnEnter delegate, because this apparently performs differently than the OnCollision delegate. The OnEnter delegate always fires when we simply enter the tilemap, but OnCollision seems to be able to detect collision with individual tiles within that tilemap.

But now my problem is that when we get a collision between say the player and the Tile Object x, the "theirObject" field of the OnCollision delegate returns the whole T2DTileLayer (the tilemap), and not the individual T2DTileObject (which is what i want). So Now I'm trying to get a reference to all the tiles that the player may be colliding with within the tilemap.

I was trying to use the GetCollisionMatches method of the T2DTileLayer object, Using it like this:

ReadOnlyArray<T2DTileObject> arrayOfCollTiles;
arrayOfCollTiles = tileLayer.GetCollisionMatches(new Vector2(0,0), new Vector2(0.1f,0.1f));

I'm assuming that with this function, we pass a rectangle in (specified by min and max), and it will return a list of all tiles that collide with that rectangle (If i'm wrong someone please correct me).

But if this is how it operates, then it is operating wrong. With a min and max of (0,0), this is a point, not a rectangle, so it should be impossible to return more than one TileObject. What it seems to be returning is the tiles that it collides with and all the surrounding tiles. But also not really its weird.

Does anyone have a solution to my problem, or advice on how to do this properly. Thanks

#8
11/06/2009 (4:19 pm)
Oh the way I made reference to the individual T2DTileObject's object type is like this:

T2DTileObject tileOBJ;

...we can refer to its object type like this:

tileOBJ.TileType.ObjectType;

...rather than just:

tileOBJ.ObjectType;

Furthermore, since the ObjectType that we care about exists within the TileType member, we cannot make use of the TorqueObject's method TestObjectType, so i had to throw the object type into a temporary TorqueObject so that i could call that function TestObjectType. Any better ways of doing this would be greatly appreciated.
#9
03/10/2010 (12:47 pm)
This might relate to the problem I'm having. I have been "painting" my map with tiles, and I've been creating polygons to represent the collision area within a tile as a convex polygon.

I have a SceneObject that has a collision polygon defined, but it does not seem to collide with my tiles.

I guess this becomes a general TX2D question...do you HAVE TO assign a type to an object in order for collisions to be processed?

Thanks
--RB
#10
03/11/2010 (4:58 am)
Figured out my problem here. This was less than intuitive to me...so I'll post it here in case it's helpful to anyone else.

In addition to defining the object type of the tile that you are editing, you must also go to the TileLayer's SceneObject configuration and enable collisions (which are not enabled by default).

That was what did it for me...thanks!
--RB