kellyson escreveu
Fala Pessoal, aproveitando o assunto de Sprites gostaria de perguntar aos mais experientes...é possivel fazer colisão por pixel de sprites animados ? Se não, qual a tecnica mais indicada ? Se possivel me postem um exemplo ou tutorial que eu possa analisar. Valeu. |
public static class PerPixelCollision
{
///<summary>
/// Determines if there is overlap of the non-transparent pixels
/// between two sprites.
///</summary>
///<param name="rectangleA">Bounding rectangle of the first sprite</param>
///<param name="dataA">Pixel data of the first sprite</param>
///<param name="rectangleB">Bouding rectangle of the second sprite</param>
///<param name="dataB">Pixel data of the second sprite</param>
///<returns>True if non-transparent pixels overlap; false otherwise</returns>
public static bool IntersectPixels(Rectangle rectangleA, Color[] dataA,
Rectangle rectangleB, Color[] dataB)
{
// Find the bounds of the rectangle intersection
int top = Math.Max(rectangleA.Top, rectangleB.Top);
int bottom = Math.Min(rectangleA.Bottom, rectangleB.Bottom);
int left = Math.Max(rectangleA.Left, rectangleB.Left);
int right = Math.Min(rectangleA.Right, rectangleB.Right);
// Check every point within the intersection bounds
for (int y = top; y < bottom; y++)
{
for (int x = left; x < right; x++)
{
// Get the color of both pixels at this point
Color colorA = dataA[(x - rectangleA.Left) +
(y - rectangleA.Top) * rectangleA.Width];
Color colorB = dataB[(x - rectangleB.Left) +
(y - rectangleB.Top) * rectangleB.Width];
// If both pixels are not completely transparent,
if (colorA.A != 0 && colorB.A != 0)
{
// then an intersection has been found
return true;
}
}
}
// No intersection found
return false;
}
}
Considerando que o cenário aqui seja: animação de sprites com sprite Sheet.
Será preciso obter o ”SourceRectangle” do Frame Atual da Animação. Entenda SourceRectangle por região da imagem que está sendo renderizada.
Com essa região em mãos, será preciso obter as informações dos texels dessa região da textura. Mas como obter isso?
Uma forma de obter isso é utilizar a sobrecarga do método GetData<T>, Texture2D class, aonde pode-se definir a seção da imagem a ser copiada.
Veja a assinatura desse método abaixo:
public void GetData<T> (
int level,
Nullable<Rectangle> rect,
T[] data,
int startIndex,
int elementCount
)
Se tiver alguma dificuldade ao tentar botar isso pra funcionar, é só avisar!
Abraços