I am just finishing my chapter on sprites & want to include a little bit about collision detection. I can easily make a function using data from a Rect & 'Contains' ( using a Point ) but I wondered if the 'Intersect' function was more suitable. I assumed that I could use Intersect to find out if one Rect was intersecting another but can't get it to work. Can anyone who has down this let me know how to use it.
Thanks in advance
update: I have code working using the source sprite's x,y,width & depth to make a rect & then checking the other sprite's top left xy & bottom right xy as Points using Contains. Works fine & only a few lines.
You should include code for how to make a collision mask check to, I'm sure alot of people would find that useful..
Here is how to use the intersect method:
- Code:
Rect r1(...);
Rect r2(...);
Rect intersection(r1);
intersection.intersect(r2);
if (!intersection.empty())
{
// The rectangles intersect
}
Re: the post 2 above I will see if I can think of a neat way to incorporate this or at least explain the technique. As the Blit function uses a transparent Pixel drawing approach there could be a simple way to get the accuracy of a mask approach without creating separate masks of each image. If the transparent color was always Black then any overlap of the filled part of the bitmaps would give a result.
Another alternative is to use the intersection of circles / rectangles from a given hot spot.
Thanks for the info on intersect function
The intersect feature works fine & I also found the move function for Rect's. This is great for updating a sprite's collision Rect with it's current position.
My article is almost complete & I just need to clean up the source so I am happy to release it.
( I think I have now read through the entire source so as to discover what's there so I think I'm quite close to having treied or at least understood most functions )
I plan 2 more articles after the Sprite one and may also publish my 3D functions if I can make them work inside PocketFrog.
I am looking forward to read your next article!