May 3rd, 2014, 02:31 AM
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:
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.
/// Returns whether or not two sprites are
/// colliding based on their textures taking
/// rotation into account.
/// <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>
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 new Vector2(-1, -1);
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.
May 6th, 2014, 12:36 PM
Re: Explanation of short XNA method for collision detection
The class Matrix is a class located in System.Drawing.Drawing2D.
You can see the constructors here: http://msdn.microsoft.com/en-us/libr...2d.matrix.aspx.
It's a poor attempt at helping but let me know if it did the job.
May 7th, 2014, 05:58 PM
Re: Explanation of short XNA method for collision detection
sorry for the late reply i do a bit of xna so i hope i can help here.
ill try to explain it in practical manner as the code is as technical as you can get
in simple terms: this code assumes you have values and or are going to setup a matrix that,
stores your players position and rotation.
though i don't see were he does any offset translations here
so im guessing his tutorial deals with that before passing to this method ...anyways....
The matrix's store the rotation and translations that are to be applied to all vertices.
In your case when a image is used, the vertices can be thought of as the pixels.
so what this bit of code is doing is
taking the second matrix and offsetting its rotation by the first.
this is so he can linearly interpolate thru the first image and compare each pixel to the second.
as for the second images pixel, each must be repositioned before comparison so...
( this is done by calling transform aka. transforming the pixel positions by the matrix rotation ).
if two pixels that are compared are at the same position
do not have a pixel with a alpha amount or transparency of 0 then part of the two images valid portions are overlapping
in xna you can manually setup your matrix but there are helper methods for this you should use instead.
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
CreateRotationZ( 1f); were the parameter is ( the amount of rotation of the image )
matrix.CreateFromYawPitchRoll(0f, 0f , 1f);
the last parameter above is used in 2d or z axis rotation
in 3d it is preferable to use the quaternion version of CreateYawPitchRoll(...,...,...)
as for the other parameters these are arrays of type Color which is rgba pixel color data.
once you load a Texture2D you can use its SetData or GetData calls
to get the pixels data for a image into or out of a array of Type color
// I did not check this for typo's or test run it. most of it is pesudo code except at the bottom
// i personally dislike this type of collision check and that is because
// the easy way is so easy to mess up and make extremely inefficient
// this is the short as possible long version
// if you have a player class or gameobjects class whatever add the color array to it
// and you should have some form of class to check collisions against each other
public class Player
public Texture2D t;
public Color color_ary;
public float rotation = 0;
public Vector2 position = Vector2.Zero;
// .... ect ....
// in your game class you should either declare references to or instantiate player objects
Player player = new Player();
Player missle = new Player();
// in your players load method just under were you load your texture
t = Content.Load<Texture2d>("Image_of_Cannon");
// add this so you only need pull in your color array once from the texture at load this is performance important
color_ary = new Color[t.Width * t.Height];
// if your just loading it in the content method directly referencing the objects then
// cannon. or missle. goes in front of the t and color_ary object instance members shown above
// in the game1 load method itself when you have a load method in your player class which you really should
cannon.Load(Content, rotation, "Image_of_cannon".... plus anything else your passing in)
missle.Load(Content, rotation, "Image_of_missle".... plus anything else your passing in)
// now that all that is out of the way place... his method... in this method... your method
public bool CheckCollision(Player A,Player B,out Vector2 impactpixel)
impactpixel = IntersectPixelsRotated(A.color_ary, Matrix.CreateRotationZ(A.rotation), B.color_ary, Matrix.CreateRotationZ(B.rotation) );
if(impactpixel.X == -1)
// now when you use this method
Vector2 impact = Vector2.Zero;
// impact occured at the pixel in the impact variable .X and .Y
// no impact occured at all
you might want to bookmark the root of this msdn on xna for further reference
this was the official xna site were the developers of it hung out
you can find examples and tons of info by searching within this forum linked below
please note that xna is no longer being supported by ms
however MonoGame is essentially the same thing and the un-official continuation of it but cross platform.
to put a point to that,
code can in the simplest case's be directly copied over from xna to MonoGame and it will run.
under c++ the directXTK or tool kit is very similar to xna as well and will run on xbox one.
i never got past 10th grade and my advanced math is terrible so i have to break it all down to low level.
so take advantage of the help you have at school it lessens the time and effort to learn
Last edited by willmotil; May 18th, 2014 at 03:29 AM.
Reason: for completeness and clarification
Tags for this Thread
Click Here to Expand Forum to Full Width
This a Codeguru.com survey!