0
\$\begingroup\$

I am trying to add shadows to my 2D xna game. Currently I have created a "shadow" using the texture of the caster with a transparent gray color, rotated, and stretched using a matrix. This, though, does not look like a real shadow, obviously. What I'm pretty sure I have to do is individually move the vertices of the matrix somehow in order to create a parallelogram so that the top and bottom sides are always parallel to the ground.

So, here are my questions:

1) Is this the correct way to cast shadows in a 2D game?

2) If so, how do I make a parallelogram out of the matrix? I've tried a vertex shader but that didn't go very well. I've also seen some people talk about using bones and using DynamicVertexBuffer, but I have no clue how to use them and can't find a tutorial on them.

3) If it isn't the correct way of casting shadows, then how should I do it? I don't want shadows like in Catalin's method, I want them to look like the shadow caster.

EDIT: I also just learned that the transformation I think I need to do is skew the matrix. XNA unfortunately does not include methods to do that, though.

EDIT 2: Here's a picture of the type of affine transformation I want to do (skewing): Example of Skew

I have sort of solved the problem and in-case anyone else wants to know how to skew horizontally here is how:

Matrix skew = Matrix.Identity;
skew.M12 = (float)Math.Tan(Math.PI / 4); // replace Math.PI / 4 with the radian amount you want

I need to skew vertically (which I can do by rotating and rotating back, but is there a more efficient way?), though and would still like to know if this is the correct way of going about creating shadows in a 2D game, though

EDIT 3: Here's what I have done in my game, and this is how the shadows should look. I've used a bunch of math and transforming to achieve the matrix for the shadow. Hopefully I've done it the right way When you make the game, you are allowed to have as much of anything as you want :P

\$\endgroup\$
  • \$\begingroup\$ You should probably post a picture that shows what you are trying to achieve. "Affine" is a category of transformations (including things like rotation, translation), whereas "skew" is a specific type of affine transformation. See this answer for a discussion of how to create a skew matrix. \$\endgroup\$ – Andrew Russell Oct 20 '14 at 15:14
  • \$\begingroup\$ Thanks, I tried skewing using the method in the post but it won't work. I'm using Matrix s = Matrix.Identity; s.M12 = (float)Math.Tan(Math.PI / 2); EDIT: Yeah... I'm dumb. I was skewing it 180 degrees. Obviously it's going to dissapear. Thank you!! It works now \$\endgroup\$ – osum4est Oct 20 '14 at 15:22
  • \$\begingroup\$ What's wrong with the Catalin's method? \$\endgroup\$ – Omar Oct 21 '14 at 12:43
  • \$\begingroup\$ Catalins method creates no light behind the ahadow caster unless there is a light behind it, as if you were standing in a room with a light at waist level or so. What i want, though, is shadows that look like they are being created from the sun, like in real life. \$\endgroup\$ – osum4est Oct 21 '14 at 13:21
  • \$\begingroup\$ If that's the problem you can simply overcome it by setting an ambient light and then by increasing the alpha of the shadows given by the Catalin's method. So you get a real nice effect. \$\endgroup\$ – Omar Oct 22 '14 at 20:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.