Actually, that sounds a lot more like what I was trying to do. However, it seemed kinda laggy since I had to do the comparison against every wall |
Well like shaktar said, you only want to check against nearby walls. In my attempts I didn't make anything as complicated as a kd tree, I just broke the world into "Sectors" (of say, 128x128 pixels), and each sector had a list of all the walls that were in it. Then objects only had to compare against walls which existed in the sectors they were in or passing through (sectors often only had 3 or 4 walls, any more than 7 or so was very uncommon).
Building the lists for each sector takes some time, but you only have to do it once when the map is loaded. Of course this only works for walls that are motionless -- moving platforms would have to be checked separately.
The method you're using to check line intersection can make a big difference, as well. I posted mine on here before (and can again if you need it). If you're calling any trig functions you're doing it wrong.
There were some other things that seemed kind of annoying too |
There are definitely situations that need to be addressed, but that's just the nature of platformers.
I wasn't really sure how to deal with "standing" on a platform kind of thing, since obviously if I just try to apply gravity and move left/right then they won't move since the intersection point will be right at where they are standing. |
My objects had a "grounded" boolean which was false if they were in the air and subject to gravity. When grounded, I also kept a unit vector of the ground which they were standing on. When they moved left/right, I simply multiplied their movement by the unit vector and they would walk up/down whatever slope they were standing on.
After movement, you do a mock move a few pixels downward to ensure they're still grounded. If you hit a wall, you know you're still grounded, and you update the unit vector to make sure it reflects the new slope. If there's no ground, you know they just walked off a cliff, are no longer grounded, and you can start applying downward force.
There are other things you have to watch for, but it all can be worked out. The end result is very nice.
I made a small demo a while back:
http://www.filefactory.com/file/q6zr22n9m7h/n/PhysDemo_zip
(Sorry about the lame download site, I still have no personal server space)
Arrows to move, space to jump, Down+Space to "drop-thru" the drop-thru walls.
There are 3 maps I was using to test different things. Rename the one you want to try to 'testmap.lmp'
Source available upon request.