PDA

View Full Version : 2D and Collision Detection



Michael Clease
01-02-2008, 21:04
This is a follow on from here

http://community.thinbasic.com/index.php?topic=947.0

I use a simple technique for collision at the moment, tile based.

think of it like this say I have a tile map of a screen 10x10 tiles



0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 1 3 0 0 0 0
0 0 0 1 2 2 3 0 0 0
0 1 2 2 2 2 2 2 3 0


I then make a collision map



0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 1 1 0 0 0 0
0 0 0 1 1 1 1 0 0 0
0 1 1 1 1 1 1 1 1 0


I use a collision map because I might want to have invisable platforms or graphics that are not platforms (sneaky).

So I know my player x and y positions using that I find my position on the collision map and test up, down, left, right and do whats required.

I am thinking about doubling up on my collision map that will improve detection.

This method is quicker than pixel perfect collision and most time you will get away with it.

Good luck


Mike

Petr Schreiber
02-02-2008, 00:07
Thanks for sharing,

Pixel perfect collision is ... pixel perfect but also can be quite slow.
Your approach looks good.

Numbers in tile map mean textures?


Thanks,
Petr

ErosOlmi
02-02-2008, 00:31
If there is some new functionality you need let me know.
For example, some matrix compare or array scan or whatever, even strange one but can be enough general to be a keyword.
This can speed up such data compare a lot.

Ciao
Eros

kryton9
02-02-2008, 07:10
That's a cool technique. Clever idea for sure!

Petr Schreiber
02-02-2008, 11:57
Such a collision maps could be useful in generall way,
for labyrinths and similar.

And it would be a bit too much to use ODE for it, as it would be like using tank to kill a mosquito.

TBGL collision arsenal is kind of poor now :), so I am open to any general purpose additions there.


Bye,
Petr

Michael Clease
02-02-2008, 20:09
@Petr. I keep it simple and use the texture number and tile numbering the same.

Tile based collision is an old idea that has been around since early games so I can't take credit for it.

I dont know if opengl can perform anything like collision.

I remember on the amiga you could do collision using the blitter chip.

First you make a mask of your character which is 1 bitplane(2 colour) and do the same for the level graphics then do the blit operation with a logical "AND" with the 2 channels and checking the ZERO FLAG for collision.

I dont know if the blitter function of modern video cards have this feature??

something to read

http://www.geocities.com/SiliconValley/Bay/9520/tutor.html
http://people.ifm.liu.se/ulfek/bitmask/

matthew
02-02-2008, 22:31
There was a tutorial on the 'bitmask' method of collision detection in an early edition of the Amiga Magazine 'Amiga Format'.

You can download the full tutorial here (http://matthew-4gl.wikispaces.com/space/showimage/The_Whole_Truth_about_Games_Programming.zip). (5.72 MB)

The collision detection section is in part 6.

Michael Clease
03-02-2008, 00:14
Ive been reviewing the opengl commands available and was wondering if something like this might work.

start with a clear frame

using a bitmask of the player and level gfx.
set glLogicOP to GL_AND
now only draw the quad directly below the player
now draw the player

this is where I dont see a way to check

If its worked when the player has overlapped the level gfx it should only set the those bits so you have a collision.

thats a rough idea what does everyone think?

I was wondering glFeedbackBuffer could be used but I dont really understand its use.

Petr Schreiber
03-02-2008, 13:10
Hi Abraxas,

OpenGL is just graphic library, so I doubt so. But I will check.
Thanks to both you and Abraxas for provided sources, I will study them very soon and report back.

There is a way to check how many pixels are on screen after the logical op, for example using glReadPixels, but this is sadly performance killer, and higher the resolution, bigger problem...

That one for VisualBASIC seems to be GDI oriented, so not usable for TBGL/OpenGL, but as I said, will check.
You gave me really interesting ideas :)


Thanks a lot,
Petr

Michael Clease
04-02-2008, 15:12
Petr I found this http://graphics.stanford.edu/~tolis/toli/software/asteroids/

read the section on collision detection I didnt think about using the depth buffer. I will look at that when I get time.

Petr Schreiber
04-02-2008, 15:49
Hi Abraxas,

that sounds smart, I will print it out.

Thanks!,
Petr