
May 3rd, 2014, 01:31 AM
#1
Explanation of short XNA method for collision detection
Hello, I have a method below used to detect collisions between rotated rectangles, determine the actual pixel locations on the screen for the rotated rectangles using matrices, and return the point of intersection (if there is one). What I don't understand is how the matrices are used. School didn't help much, and like all of my math classes, it never discussed the fundamental ideas behind the topic; just how to use them. The method is listed below:
Code:
/// <summary>
/// Returns whether or not two sprites are
/// colliding based on their textures taking
/// rotation into account.
/// </summary>
/// <param name="tex1">The color data of the first texture.</param>
/// <param name="mat1">The matrix corresponding with texture 1.</param>
/// <param name="tex2">The color data of the second texture.</param>
/// <param name="mat2">The matrix corresponding with texture 2.</param>
/// <returns></returns>
private Vector2 IntersectPixelsRotated(Color[,] tex1, Matrix mat1, Color[,] tex2, Matrix mat2)
{
Matrix mat1to2 = mat1 * Matrix.Invert(mat2);
int width1 = tex1.GetLength(0);
int height1 = tex1.GetLength(1);
int width2 = tex2.GetLength(0);
int height2 = tex2.GetLength(1);
for (int x1 = 0; x1 < width1; x1++)
{
for (int y1 = 0; y1 < height1; y1++)
{
Vector2 pos1 = new Vector2(x1, y1);
Vector2 pos2 = Vector2.Transform(pos1, mat1to2);
int x2 = (int)pos2.X;
int y2 = (int)pos2.Y;
if ((x2 >= 0) && (x2 < width2))
{
if ((y2 >= 0) && (y2 < height2))
{
if (tex1[x1, y1].A > 0)
{
if (tex2[x2, y2].A > 0)
{
Vector2 screenPos = Vector2.Transform(pos1, mat1);
return screenPos;
}
}
}
}
}
}
return new Vector2(1, 1);
}
I suspect that the matrices are intended to store rotation values, but I'm not sure. If they are, I don't know what the range of the values are. I got this from this website: http://www.riemers.net/eng/Tutorials...n_Overview.php. I don't know how to format the data into matrices because my high school conveniently skipped over that essential bit. All of the tutorials for matrix theory or matrix algebra (even introductory matrix algebra) seem to be targeted to an audience that is comfortable with calculus. I am the inverse of that definition.
Basically, I'm asking if anybody can tell me what the arguments the above piece of code wants for matrices are, and how I might enter them. Any attempt is appreciated.
Tags for this Thread
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules

Click Here to Expand Forum to Full Width
This is a CodeGuru survey question.
Featured
