Okay language agnostic, I much prefer my approach to the shallow penetration test.
Things You Need
- You need some way to store previous positions of your objects
- You need a way to detect and store all current objects you're colliding with
How It Works
First, you need to find the object with the nearest wall- in a tile system this is a simple check for the tile with the nearest center. When I did this I sorted my objects from closest to farthest.
Next, you run this routine on it:
if (we are colliding){
if (mypreviousdown <= theirpreviousup){ //if I was previously above them
move upwards() //probably set our position to their position - our height
set our y velocity = their y velocity() // for moving platforms
//optional
add their velocity to our position() // for sticky platforms
// end optional
}
if (mypreviousup >= theirpreviousdown){ //if I was previously below them
move downwards()
set our y velocity = their y velocity() // for moving platforms
//optional
add their velocity to our position() // for sticky platforms
// end optional
}
}
if (we are still colliding){
if (mypreviousright <= theirpreviousleft){
move to the left()
set our x velocity = their x velocity
}
if (mypreviousleft >= theirpreviousright){
move to the right()
set our y velocity = their y velocity
}
}
Considerations
This system works fantastically well, and I have yet to see any bugs- unless you're moving fast enough to move to the other side of the object, but if you're not, this works great.
One thing you may want to consider is adding some functionality to slip around edges or go up steps, it will add some good gameplay mechanics to your game.
Another thing you may want to add is a way to detect which side is currently colliding at any given time(for things like jumping and such)
You may also consider a custom event system, it's been a great help to my game programming lately.