Advertisement

I need help on collision detection in simple game

Started by April 09, 2000 03:22 PM
2 comments, last by John P 24 years, 8 months ago
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 /////
///////www.the-dump.freeserve.co.uk//jporter@ukonline.co.uk/////
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
My HomepageSome shoot to kill, others shoot to mame. I say clear the chamber and let the lord decide. - Reno 911
Advertisement
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.
A polar bear is a rectangular bear after a coordinate transform.
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.

This topic is closed to new replies.

Advertisement