hi,

the last posts about collision are clear for me, but i don't know how to use that in my program. in my snake game, there is a snake that you can move around the screen, eat sum stuff, grow, etc. now i want to make levels for the game. that means: in the middle of the area the snake moves in, i want to place kind of borders of one/more colors. how can i detect if the snake did touch this borders. i mean, i don't want to store all the coordinates of the borders, and then check if they are the same as the snake's one.

all is in rectangles, that means, that the border is as big as one element of the snake.

thanks,
nop

btw: please read my post about loading gifs in DD in the "Main" section (or move it here, in this section). there i want to know how to load gifs to my backbuffer. i already tried exagone's giflib, but i've some problems with it.....
the gif post
Posted on 2001-12-23 03:44:40 by NOP-erator
Here's an algorithm to detect colission of two rectangles, with fixed (and both the same) size:


A block in the algo looks like this:
(x,y)
+--------------+
| width |
| h |
| e |
| i |
| g |
| h |
| t |
+--------------+


For the horizontal coordinates:
1. Check which block is most left, i.e. the one with the lowest X coordinate. When they are the same, just choose one.
2. The one with the lowest X coordinate is block1 (with X1), the other one is block2 (with X2).
3. tmpX = X1 + width
4. if (tmpX >= X2) { horizontal collision detected }

And the same way for the vertical coordinates:
1. Check which block is the one with the lowest Y coordinate. When they are the same, just choose one.
2. The one with the lowest Ycoordinate is block1 (with Y1), the other one is block2 (with Y2).
3. tmpY = Y1 + height
4. if (tmpY >= Y2) { vertical collision detected }

final step:
If horizontal AND vertical collsion is detected, both blocks overlap.

I think the algo is correct this way but maybe I forgot something.

Thomas
Posted on 2001-12-23 06:09:10 by Thomas
If the snakes movement is limited to a 2d grid, as it is for other snake games I've seen then you should store a 2d array in memory. In that you could represent barriers as 1 and an empty square as 0.

Then every turn you check the element in the array which coresponds to the location of the snakes head, if the square has a barrier in it then collision has occured.
Posted on 2001-12-23 06:59:38 by Eóin
ok, so i kind of store the whole level in an array. alright....thanks....

(but first i have to solve my gif problem)

nop
Posted on 2001-12-23 12:17:37 by NOP-erator