Another day, another failure. I manage to get that X and Y of both sprites with view.
It without problem returns good value for standard Intersections check but it fails with PixelPerfect badly...Maybe I am too tired or it's too late but I can't think of anything that may help so please help me.
Below minimal code of needed functions:
To make a bitmask from sprite:
int StworzBitMaske(sfSprite* duch, bool** &Maska)
{
sfColor KolorPixela;
int i,j = NULL;
int x = (int)sfSprite_GetWidth(duch);
int y = (int)sfSprite_GetHeight(duch);
Maska =(bool**) malloc(x * sizeof (*Maska));
for (i = 0; i < x; i++)
{
Maska[i] =(bool*) malloc(y * sizeof(Maska));
}
for (i = 0; i < x; i++)
{
for (j = 0; j < y; j++)
{
KolorPixela = sfSprite_GetPixel(duch, i,j);
if (KolorPixela.a == 0.f)
{
Maska[i][j] = 0;
}
else
{
Maska[i][j] = 1;
}
}
}
return 1;
}
To change sprite to rect:
sfIntRect SpriteNaRect(sfSprite* &duch)
{
sfIntRect Prostokat;
int y,x;
x = (int)sfSprite_GetX(duch);
y = (int)sfSprite_GetY(duch);
//sfSprite_TransformToGlobal(duch, 0,0,&x, &y);
Prostokat.Top = (int)y;
Prostokat.Left = (int)x;
Prostokat.Bottom = (int)(y + sfSprite_GetHeight(duch));
Prostokat.Right = (int)(x + sfSprite_GetWidth(duch));
//std::cout << (int)x << std::endl;
//std::cout << (int)y << std::endl;
//system("PAUSE");
/*sfSprite_TransformToLocal(duch, 200, 30, &x,&y);
std::cout << x << y << std::endl;
system("PAUSE");*/
return Prostokat;
}
Checking function:
bool PixelPerfect(sfSprite* &Obiekt1, sfSprite* &Obiekt2,bool** &Maska, bool** &Maska2)
{
sfIntRect test1,test2,Kolizja;
test1 = SpriteNaRect(Obiekt1);
test2 = SpriteNaRect(Obiekt2);
if (sfIntRect_Intersects(&test1, &test2, &Kolizja))
{
WektorFloat Wektor1,Wektor2;
for (int licznik = Kolizja.Left; licznik < Kolizja.Right; licznik++)
{
for (int licznik2 = Kolizja.Top; licznik2 < Kolizja.Bottom; licznik2++)
{
sfSprite_TransformToLocal(Obiekt1,licznik,licznik2,&Wektor1.x,&Wektor1.y);
sfSprite_TransformToLocal(Obiekt2,licznik,licznik2,&Wektor2.x,&Wektor2.y);
if (Maska[(int)Wektor1.x][(int)Wektor1.y] * Maska2[(int)Wektor2.x][(int)Wektor2.y] == 1)
{
return true;
}
else
{
return false;
}
}
}
}
else
{
return false;
}
return false;
}
Wektor is a structure of x and y.
How I do maskcolor:
sfColor Kolor; //DO ANIMACJI
Kolor = sfColor_FromRGB(0,0,0); //DO ANIMACJI
bool** Macha;
bool** Macha2;
Kamien = sfImage_CreateFromFile("kamol2.png");
sfImage_CreateMaskFromColor(Kamien, Kolor, 0);
sfSprite_SetImage(sKamien, Kamien);
wsk->zdjecie = sfImage_CreateFromFile("Kubac.png");
sfImage_CreateMaskFromColor(wsk->zdjecie, Kolor, 0);
sfSprite_SetImage(wsk->duszek, wsk->zdjecie);
And how I use it in game loop:
std::cout << PixelPerfect(wsk->duszek, sKamien,Macha,Macha2) << std::endl;
Function always return No collision, please write some advices because I go to sleep now and I would like to finish this collision detection tomorrow .
Thanks.
P.S Some parts are commented out because I was testing different ways to solve it.
P.S2 Oh yeah both images have black backgrounds.