Snake Game Collision Detection

Started by
3 comments, last by Khaosk 14 years, 1 month ago
Hi, I'm just starting an Anaconda clone in XNA. This is the game I'm talking about:
I've been doing it all in primitives and have been trying to figure out a faster way to do the collision detection without checking the position of the head against a huge number of points every frame. I was thinking about checking that if a point in front of the head was any color other than black (the background color), then that would mean the snake has run into something. I figured that I'd have to make a 2D array storing all the colors of the current board, but was unsure how to do this. Can anyone help?
Advertisement
Collision detection between two bitmaps can be done pretty quickly. You use a 1 bit representation of both images. The trick is to use bit shifting and bitwise AND to test 16-32 bits at a time.
Better idea - if your snake is a long line of circles, group them. If you have a group of circles with radius r that are all within d distance from a particular one, you can just test the head against that circle as if it had radius r+d. This should eliminate a lot of checking against circles that are nowhere near the head.

I should also point out that a few thousand checks per frame is probably not a big performance issue so if you aren't actually getting slowdown you could just ignore it.
Well you could use a spatial hashmap. devide the playfield in rectangular areas and....well just read the gamedev article

SpatialHash
Thanks for the help guys.

This topic is closed to new replies.

Advertisement