This project is read-only.

Matrix Hacks Plz!!!1!!11!!

Feb 1, 2012 at 5:35 AM
Edited Feb 1, 2012 at 5:36 AM

Okay so I have been trying everything under the sun to render a simple texture in the same matrix as the krypton engine's Matrix and nothing works.

None of my trial and error Matrices work.

This is what I have..

 

		public static Matrix ViewMatrix
		{
			get
			{
				return Matrix.CreateTranslation(-position.X, -position.Y, 0) * Matrix.CreateRotationZ(rotation) *
				Matrix.CreateScale(zoom, zoom, 1) * Matrix.CreateTranslation(graphicsDevice.Viewport.Width / 2, graphicsDevice.Viewport.Height / 2, 0);
			}
		}

		public static Matrix ProjectionMatrix
		{
			get { return Matrix.CreateOrthographic(graphicsDevice.Viewport.Width, graphicsDevice.Viewport.Height, 0, 1); }
		}

		public static Matrix WorldViewProjectionMatrix
		{
			get
			{
				return ViewMatrix * ProjectionMatrix;
			}
		}

 

Has anyone had success drawing Texture2D's with SpriteBatch in the same matrix as Krypton.

Feb 2, 2012 at 1:57 AM
Edited Feb 9, 2012 at 4:52 PM

Yep, krypton works quite well with 2d cameras as long as you feed it the right matrix..

public void CreateTransformationMatrix(out Matrix matrix){

    //create matrices
    var translate = Matrix.CreateTranslation(-Location.X, -Location.Y, 0);
    var offset = Matrix.CreateTranslation(viewWidth / 2, viewHeight / 2, 0);
    var scale = Matrix.CreateScale(zoom, zoom, 1);
    var rotation = Matrix.CreateRotationZ(Rotation);

    // transform them into one another
    matrix = translate;
    Matrix.Multiply(ref matrix, ref scale, out matrix);
    Matrix.Multiply(ref matrix, ref rotation, out matrix);
    Matrix.Multiply(ref matrix, ref offset, out matrix);
}

That is how my camera's overall transformation matrix is computed. Notice that I don't use a projection matrix here since SpriteBatch already does that for you. The only thing I had to make sure of was that I set spriteBatchCompatibilityMode = true on the krypton light renderer like so:

 

            lightingEngine.SpriteBatchCompatablityEnabled = true;
            lightingEngine.CullMode = CullMode.CullClockwiseFace;

 

That extra line is there because (atleast from what I can tell) the batch compatibility mode reverses the scale resulting in the quad thatthe sprite gets rendered on facing backwards.

 

EDIT: I noticed that I didn't set the out parameters to the "matrix" variable.. sorry about the silly mistake and I hope it clears up some stuff if anyone was confused :P.

Feb 2, 2012 at 2:21 AM
Edited Feb 2, 2012 at 2:26 AM

WOW that worked amazingly well thanks I spent a lot of time trying to figure it out and you solved it!

As a bonus to anyone interested heres a neat mousestate extension function

 

		public static Vector2 ToWorld(this MouseState mouseState)
		{
			return Vector2.Transform(new Vector2(mouseState.X, mouseState.Y), Matrix.Invert(Camera.TransformationMatrix));
		}

Where Camera.TranslationMatrix is M6's snippet put into a property

Thank you very much M6

Feb 9, 2012 at 12:48 AM
M6dEEp wrote:

Yep, krypton works quite well with 2d cameras as long as you feed it the right matrix..

public void CreateTransformationMatrix(out Matrix matrix){

    //create matrices
    var translate = Matrix.CreateTranslation(-Location.X, -Location.Y, 0);
    var offset = Matrix.CreateTranslation(viewWidth / 2, viewHeight / 2, 0);
    var scale = Matrix.CreateScale(zoom, zoom, 1);
    var rotation = Matrix.CreateRotationZ(Rotation);

    // transform them into one another
    matrix = translate;
    Matrix.Multiply(ref translation, ref scale, out translation);
    Matrix.Multiply(ref translation, ref rotation, out translation);
    Matrix.Multiply(ref translation, ref offset, out translation);
}

That is how my camera's overall transformation matrix is computed. Notice that I don't use a projection matrix here since SpriteBatch already does that for you. The only thing I had to make sure of was that I set spriteBatchCompatibilityMode = true on the krypton light renderer like so:

 

            lightingEngine.SpriteBatchCompatablityEnabled = true;
            lightingEngine.CullMode = CullMode.CullClockwiseFace;

 

That extra line is there because (atleast from what I can tell) the batch compatibility mode reverses the scale resulting in the quad thatthe sprite gets rendered on facing backwards.

would it be possible for you to show us a little more context to your sample?

Feb 9, 2012 at 4:59 PM

@Squizzle
Yeah no problem and I'm glad it helped. If you are interested I also have code that puts an axis-aligned bounding box around the camera making it perfect for cull testing even when rotating and zooming.

@bpoulson 

If you're talking about the CreateTransformMatrix method I accidentally copied the code incorrectly so it kind of doesn't make sense, if you look at my edited post it should probably be more clear, sorry about that. I'm currently at school and away from my code base but when I get back I will post up a project with the camera class and how to use it with krypton for you guys.

 

Feb 16, 2012 at 5:00 AM

hate to bump. sorry if you've been busy M6dEEp.

I'm looking for some help with getting the Farseer camera to work with this library.

Feb 19, 2012 at 7:08 PM

Not too long ago when I started  integrating farseer I took one look [at farseer's camera class] and turned my back on it in favor of writing the camera above, which works with it quite well.