[SOLVED] Aligning 2d and 3d coordinate systems.

Hey gang… :sunglasses:

I am doing simple shadow-casting using simple ‘vector-position-color’ polygons…

The idea is having a few vertices at the base of the shadow-casting sprite, and moving the other few vertices around (based on the lights source) to create dynamic shadow… Simple stuff…

Now, when I draw these vertices, the coordinates I assign them have (0,0) as the CENTER of the screen, and a value of 1 represents the furthest edges of the screen… So its like we are talking ratios from the center of the screen, rather than game units, or pixels from the top left.

The problem is slightly compounded by the fact that my game scrolls…

Should I just do a little calculation to get a screen ratio from my object coordinates?

Or is there a view-matrix trick I can do, to align these 2 coordinate systems?..

hey monopalle,

You want your 3d vertices to match the spriteBatch projection?

Matrix spriteBatchProj = Matrix.CreateOrthographicOffCenter(0, viewport.Width, viewport.Height, 0, 0, 1)
                               * Matrix.CreateTranslation(-0.5f, -0.5f, 0);

I needed this formula so often that it would be great to have a convenient static property like:

Matrix spriteBatchProj = SpriteBatch.Projection;

To truly match SpriteBatch’ projection, the Z axis also needs to be flipped. See line 137 of SpriteBatch.cs.

1 Like

I’m not sure exactly if you want orthographic projection, but if you want the origin of the viewport to be the centre and the edges to be 1s it would be:

Matrix.CreateOrthographicOffCenter(-1, 1, 1, -1, 0, -1) * Matrix.CreateTranslation(-0.5f, -0.5f, 0);

If you want +Y to be “north” just flip the signs for the bottom and top parameters.

@LithiumToast @raizam Thank you very much guys I am sure this must be what I was looking for. I’ll get back to it when my election buzz wears off…

I am right to ‘subtract’ these shadow polygons from the light-source, right? Like using blend states normally, I can just draw these using the blend-state I want?

Or do they need to go on a render-target first?