Log in

View Full Version : Help with tile-based collision detection and results?


Uhfgood
02-21-2003, 08:52 PM
Well, i'm truely stuck here guys. I've tried this stuff 10 different ways and I can't figure out how to make it all work perfectly all the time.

I'm using deltatime and i'm using floating point, and the sprite is a different size than the tiles. I know how to make a collision detection function, it merely tests to see if the bottom is touching an existing tile (in an array), otherwise it's not. However I can't, for the life of me, figure out how to make the sprite sit on top of the tile it's touching.

This is partly due to the fact that gravity is always pulling it down. Now I don't want you guys to get all physically correct on me, all I mean by gravity is a downward motion of the sprite where it gets faster and faster each time. (Basically I add a gravity variable onto the y velocity, which is probably not truely velocity either but it serves it's purpose).

Half the time I have trouble with it jumping a tile, and then also not properly sitting on top of the tile it touched.

Can *anyone* help me?

Thanks alot!

patrox
02-21-2003, 09:53 PM
Hi

Check that the deltaY is not higher than the size of a tile. if so then check the intermediate tiles.


pat.

Pyabo
02-22-2003, 04:23 AM
Please don't take this the wrong way, but it's probably much more productive to ask this sort of question in a forum that is geared toward programming. I suggest gamedev.net or flipcode.com.

That being said... you can always use a dedicated "collision sprite" instead of the actual character sprite in order to make collision detection more flexible.

Dan MacDonald
02-22-2003, 04:45 AM
Doesn't seem like it should be too hard…. Just make sure your checking for tile collision at the next spot your sprite will move to, not the spot it's currently in. If your doing Tile based collision you probably have a function that looks like this

bool IsSolid(int x, int y)

Where x and y are screen coordinates. In that function you convert screen coordinates to map coordinates then find the tile row and column those coordinates lie in and then check the solid property of the tile at that row and column.

Anyway. When your IsSolid() returns true (indicating the tile below the coords x,y is solid) you should do a small calculation. Take the row of the solid tile and multiply it by the tile height, now you know the y position of the top of the solid tile. Use that y position to update the y position were going to move the sprite to.

This isn't the "true" physics’ solution, but it should be close enough for your purposes.

Uhfgood
02-22-2003, 07:56 AM
Pyabo - You're probably right, and I did actually post in other forums, I was just thinking with all the smart people on here that they might tell me something I haven't thought of yet.

Dan - Thanks but in essence that's what i've tried in one of my 7-8 different versions of these functions. Someone told me I need to have a state machine for various physical aspects, so that when i'm on the ground I have a state for that, and when i'm jumping and falling i should have a state for those, to stop the character at the right time.

Thanks for the help

Keith