[java] making collision checks more efficient

Started by
5 comments, last by m_wherrett 21 years, 10 months ago
Hey hey chaps, I''m becoming quite a regular here now. The worst bit is that I''ve got even more questions to come. Anyway, on with the next one? When doing my collision check between a player and all the platforms in a level how do I prevent the engine checking every single platform? How do you efficiently filter out the ones that aren''t going to be of relevance? I''ve got the collision working, it''s just that it checks ever single platform repeatedly which is ludicrously inneficient. Any suggestions?
Advertisement
Hi a good solution might be to use one of the many types spatial partioning trees, like quad trees, bsp,etc.
There are plenty of docs on these.
Can''t really say more without some more details about what your doing.
Harley.
knackers. I forgot to mention that I''m only dealing with a 2d platform game. I''ve currently got a screen full of blocks to jump onto (stored in an array). With every player update I check all the blocks for collision. It works fine but I was thinking that if I were to add scrolling and introduce numerous more blocks, how could I limit the collision checks to only those in the general area. Should I check every blocks location first and then the ones that are close or is there a more efficient procedure.
You have your current position, so convert that into the tile position by dividing by the tile size. That''ll give you the current tile in your array that you are over. Then, depending on your player dimensions and tile dimensions, check all the tiles in the array surrounding that point (like, say, the tiles above, below, left and right one tile).

Depending on your player movement you may be able to simplify it even more to just the tiles infront of the character...
(oops, forgot password above )

by the way, you can still use quadtrees and bsp trees in 2d, but in this case its probably overkill..
You could separate the level into blocks and provide a list within each block of the platforms that are partially in that block.

Then when the player is moving, you only check the blocks that the player is inside or overlapping.

I used that method in a space shootemup - I split the playfield into a 10x10 cube array with linked list references to all objects within each cube, and checked only those collisions against each other, and I got a 3x frame rate increase (on a 200 MHz machine, a few years back)
It's not what you're taught, it's what you learn.
I recently coded a simple scrolling jump''n''run game and encountered the same problem. I was thinking about taking the approach explained by waverider but in the end, I just checked if the object in question was on the screen before performing coll detection. If it''s fast enough for you, you might just keep it that simple.

This topic is closed to new replies.

Advertisement