# XNA matrices

This topic is 3163 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am having some trouble with matrices. I think its simply the order of operation's that I am using for the matrices.
Matrix thisSprite =
Matrix.CreateTranslation(this.Origin.X,-this.Origin.Y,0)*//0,0,0
Matrix.CreateScale(this.Scale.X, this.Scale.Y, 1) *//1,1,1
Matrix.CreateTranslation(this.Position.X,this.Position.Y,0);//500,300,0
Matrix invertSprite = Matrix.Invert(thisSprite);
Vector2 pos1 = new Vector2(mouseX, mouseY);
Vector2 pos2 = Vector2.Transform(pos1, invertSprite);


If I don't rotate the sprite then the matrix work's fine. So what am I doing wrong with this matrix?

##### Share on other sites
1. Translate by -Origin [x as well as y]
2. Rotate
3. Scale
4. Translate to +Position [that is if the origin is supposed to move to the position; otherwise translate by +Origin before

The error should be in your first translation, where you translate by -Y, but +X, which seems odd. Try -X.

I don't get why you're inverting the matrix, but I'm sure you have your reasons. But just for the sake of it, try it without the inverting and see if it works for you.

Hope I could help :-)

##### Share on other sites
Well in the first translation It's all 0,0,0 so even if its negative x it wouldn't matter.

And I am inverting the matrix so I could take a screen coordinate and multiple it by the inverse matrix to find where it would land on the original image for perfect pixel collision.

##### Share on other sites
It actually does matter if the value goes negative. Just because you don't see it doesn't mean it isn't there. And the fact that it isn't working properly is either the inverted matrix or the wrong origin-translation. Check if the origin really always is 0,0.
If you don't do the first translation correctly, the rotation will not be as expected. I assume your "Origin"-value is the point in the object around which it should be rotated afterwards.

If you want to rotate an object around the origin 50,50 you would approach it like this:
1. Translate by -50,-50
2. Rotate
3. Translate by 50,50
This way the object will be rotated around the point 50,50 but will still be at position 0,0. Then scale it and transform it to the position you want it to be.

Maybe you should explain in detail which effect you're trying to achieve.

##### Share on other sites
No, I meant that origin.x is equal to 0 so -0=0.

What I am trying to achieve is pixel perfect collision detection.
Quote:
 For a point c on the screen: { Find point c that is rendered in image A. If the pixel is non transparent { COLLISION! } }

Basically I just use the inverse of the image to translate point c back into the color array of image A. Then I check the color array and if its not transparent then there's a collision.

Basically its Riemer's tutorial, but reduced down to one point only.

http://www.riemers.net/eng/Tutorials/XNA/Csharp/Series2D/Coll_Detection_Overview.php

##### Share on other sites
Riemer has the best tutorials if you're just getting into XNA :D I love his stuff!
Anyways, I think you should scale before rotating. If it still doesn't work, tell us what exactly is off. Try to set rotation to 0, then update it every frame to see how it's going, then tell us how the fault makes itself recognizable. That's how I always go about the matrices problems when I encounter them :3

Have you tried posting in his forums? Most of the time Riemer himself is a big help, or another member of his forums.

1. 1
2. 2
3. 3
Rutin
25
4. 4
5. 5
khawk
14

• 11
• 11
• 23
• 10
• 9
• ### Forum Statistics

• Total Topics
633649
• Total Posts
3013117
×