PHP Code:
#include <windows.h>
#include <stdio.h>
#include <conio.h>
int main()
{
RECT SpriteOne; // Imaginary sprite
RECT SpriteTwo; // Imaginary sprite
RECT SpriteResult; // Sprite to hold overlapping area
SetRect(&SpriteOne,50,50,100,100); // A 50x50px sprite, with top-left @ (50,50)
SetRect(&SpriteTwo,300,300,425,350); // A 125x50px sprite, with top-left @ (300,300)
// Loop until they overlap
while(!IntersectRect(&SpriteResult,&SpriteOne,&SpriteTwo))
{
// Simulate movement of the sprites
SpriteOne.bottom += 1;
SpriteOne.left += 1;
SpriteOne.right += 1;
SpriteOne.top += 1;
SpriteTwo.bottom -= 1;
SpriteTwo.left -= 1;
SpriteTwo.right -= 1;
SpriteTwo.top -= 1;
}
// Output the coordinates of the area where the sprites overlap
printf("The rectangles have intersected!\nHere are the coords for pixel-perfect examination:\nLeft: %ldpx\nTop: %ldpx\nRight: %ldpx\nBottom: %ldpx\n\nPress any key to continue...",SpriteResult.left,SpriteResult.top,SpriteResult.right,SpriteResult.bottom);
getch();
return 0;
}
This would allow you to do pixel-perfect collision detection only where the sprites overlap. Don't forget that .bottom = .top + height, and .right = .left + width.