Archived

This topic is now archived and is closed to further replies.

John P

I need help on collision detection in simple game

Recommended Posts

I am having trouble getting my collision detection function to work. What should happen when run is that it will check weather one 16x16 square has crossed another 16x16 square. A slimed down version of the code I have so far is, typedef struct CO_ORD { int x,y; }; int detectleafeaten(CO_ORD snakexy,CO_ORD leafxy) { if(snakexy.x >= leafxy.x && snakexy.x <= leafxy.x+16 && snakexy.y >= leafxy.y && snakexy.y <= leafxy.y+16)return 1; else return 0; } snakexy being the top left hand pixle in the 16x16 snake square and leafxy being the top left hand pixle of the 16x16 leaf square. My problem is that it dosn''t all ways seem to work and I don''t know why. I will be grateful for any help! ///// //www.the-dump.freeserve.co.uk //jporter@ukonline.co.uk /////

Share this post


Link to post
Share on other sites
it looks like you''re testing to see if each sprite is on top of the other. it''s not working because the x1 has to equal the x2 and the y1 has to equal the y2 for it to return true. try testing each corner of each sprite with one another. I think this is what you were trying to do. lookup bounding box collision detection for more indepth info.
~Julio

Share this post


Link to post
Share on other sites
If both your snake and your leaf has a size, you should do a rectangle-rectangle collision test instead of a pixel-rectangle test, which is what you''re doing now.
(You''re checking if the top left pixel of the snake is inside the leaf. You should check if ANY pixel of the snake is inside the leaf.)

A polar bear is a rectangular bear after a coordinate transform.

Share this post


Link to post
Share on other sites
You might want to check pixel by pixel of the overlapping region. If you''ve defined one color as transparent, you can test if the pixels in the same screen position are both non-transparent. If so, they collide. This would require that you translate from sprite coordinates into screen coordinates.

Making friends one burger at a time.

Share this post


Link to post
Share on other sites