Page 1 of 1
Tile collision example
Posted: Wed Jan 02, 2013 10:41 pm
by dreadkillz
Just working on tile collision and thought I'd share my method for people curious on how to do tile physics. There is a map class that takes care of the collision. The heart of it is the customizable callbacks called the preSolve and postSolve. With the callbacks, you can do neat things like one way tiles in the example. Collision detection is done by using the edges of a bounding box as vertical or horizontal sensors.
I know there's plenty of tile collision examples, but I wanted to post one with minimal amounts of code
Things to do:
* Slopes
In the demo:
Try changing the tile size or player box size! It'll work for any size! Try defining your own custom callback for neat stuff like destructible tiles or bouncy tiles.
Hope it helps!
Updates will be posted here:
Git Repo
Re: Tile collision example
Posted: Fri Jan 18, 2013 6:19 pm
by markgo
Update! It now supports slopes and continuous collision detection. Syntax is similar to my lib:
ATC, except you don't need Tiled. Just feed it a grid structure and a table with tile properties. Modify it however you want!
Tile collision 1.0
Posted: Sun Jan 20, 2013 3:40 pm
by markgo
Just a bump for the update. Slope example is in my ATC demo, sorry! It's quite similar in code so hopefully you can figure it out.
For those who want to learn:
http://info.sonicretro.org/SPG:Solid_Tiles
http://forums.nesdev.com/viewtopic.php?f=2&t=6319
The difference in methodology from the posted links is that my sensors are the actual box edges so it's possible to miss tiles completely contained with a rectangle. This shouldn't be an issue when character movement is small enough for a sensor to catch collision tiles.
Slopes and curves are handled like retro games: Store an array of height values per slope tile. How the player reacts to the tile and the direction he needs to be pushed to resolve the collision depends on which sensor touches it.
I took the route of using the box corners to resolve slope collisions instead of the midpoints of each side. This has the problem of a character sticking out from a slope more instead of looking like he is standing on it with his "feet". Many old games covered this up visually by extending the slope.
Re: Tile collision example
Posted: Tue Jan 22, 2013 6:32 am
by norubal
nice work, better than my code
Re: Tile collision example
Posted: Sun Feb 24, 2013 4:14 pm
by lieudusty
Are slopes implemented yet? I can't figure out how to make one.
Re: Tile collision example
Posted: Sun Feb 24, 2013 9:46 pm
by markgo
lieudusty wrote:Are slopes implemented yet? I can't figure out how to make one.
Here's an updated demo with the slopes. Each slope tile has an associated height map that determines the shape of the slope. It's similar to the Sonic physics link.
Re: Tile collision example
Posted: Mon Apr 22, 2013 12:46 pm
by markgo
I updated the module to version 3. You no longer have to create a box object. Instead, you create a handler object and pass the dimensions of your box to it for collision resolution. A lot of extraneous functions were also removed. It's much more flexible now so it should work with any map format, even Advanced Tiled Loader.
The repo is the same as above.
Re: Tile collision example
Posted: Fri Mar 20, 2015 3:14 pm
by Mekapix
Hey, your collision detection is amazing but I'm not used to using entities, so could I ask how you'd bound images to an entity like you've made and then apply physics to it? I've essentially got code that doesn't have collision detection but can use images as the player character and has gravity applied to it, then there's this code that has great collision detection but I don't know how to change the player from a square to an image or how to apply gravity to it, so that it works the same way as a traditional platformer.
Re: Tile collision example
Posted: Fri Mar 20, 2015 3:16 pm
by Mekapix
Or if it's possible to use the tile collision and bind it to the world from the physics tutorial, that'd really help too!