Hello,
I need some help casting shadows in a 2D game. Here's the dilemma:
All polygons are drawn in order of their verticies. This means that I can't use the algorithm I was thinking of using. Here's what I want to do:

This, however, doesn't work because of the nature of polygon drawing. Here's what the result actually looks like:


Here's the actual code for drawing the shadow (in C#):

Code:
    class ShadowCastingObject
    {
        public Vector2[] Polygon { get; set; }

        public Vector2[] GetShadow(float LightAngle, Vector2 LightPosition)
        {
            List<Vector2> Shadow = new List<Vector2>();
            for (int i = 0; i < Polygon.Length; i++)
            {
                float xdist = Polygon[i].X - LightPosition.X;
                float ydist = Polygon[i].Y - LightPosition.Y;
                Shadow.Add(new Vector2(Polygon[i].X + xdist, Polygon[i].Y + ydist));
            }
            for (int i = 0; i < Polygon.Length; i++)
            {
                Shadow.Add(Polygon[i]);
            }
            return Shadow.ToArray();
        }
    }

Any help here would be greatly appreciated.

Ideally, we should only have to cast these points:


But I don't know how to determine which points to cast the shadow from.
SirCmpwn wrote:


Code:
    class ShadowCastingObject
    {
        public Vector2[] Polygon { get; set; }

        public Vector2[] GetShadow(float LightAngle, Vector2 LightPosition)
        {
            List<Vector2> Shadow = new List<Vector2>();
            for (int i = 0; i < Polygon.Length; i++)
            {
                float xdist = Polygon[i].X - LightPosition.X;
                float ydist = Polygon[i].Y - LightPosition.Y;
                Shadow.Add(new Vector2(Polygon[i].X + xdist, Polygon[i].Y + ydist));
            }
        }
    }

Mathematically, doesn't this just make a vector with LightPosition.X and LightPosition.Y?
Nope.
Why can't you use the projection to figure out the virtual position of the shadow's vertices, and just draw it as if it was a polygon?
KermMartian wrote:
Why can't you use the projection to figure out the virtual position of the shadow's vertices, and just draw it as if it was a polygon?

I don't completely follow you here, could you elaborate?
KermMartian wrote:
Why can't you use the projection to figure out the virtual position of the shadow's vertices, and just draw it as if it was a polygon?

That's what I had thought as well. It would send a ray from the center to each vertex of the polygons and then place another polygon point at the edge of the screen. When it finishes doing that for the current polygon, it will draw it darkened. Does that make sense? x.x
Again, we hit the same problem of drawing polygons correctly. This is what I do now, and you can see the result in the second image. Ideally, for something like a box, we should have a six point polygon (see first image).
But you said it was a 2D game, did you not? I had assumed that would mean you would have rectangles instead of boxes, but clearly I made an incorrect assumption.
Couldn't you make a 6 point polygon by having it go around the polygon in a clockwise order starting at the beginning, and then go through and add the points on the edge starting from the end and going counter-clockwise? Like, for the box, it would go: bottom left, middle, right, top-right, top-middle, top-left.
KermMartian wrote:
But you said it was a 2D game, did you not? I had assumed that would mean you would have rectangles instead of boxes, but clearly I made an incorrect assumption.

2D polygon drawing is not supported. So what I do is triangulate the polygons into a series of triangles and render them in 3D, but do it such that the result appears 2D.

_player1537 wrote:
Couldn't you make a 6 point polygon by having it go around the polygon in a clockwise order starting at the beginning, and then go through and add the points on the edge starting from the end and going counter-clockwise? Like, for the box, it would go: bottom left, middle, right, top-right, top-middle, top-left.

What if the light was facing from the other direction? What if it isn't a box? I don't want to hard code shadows for every shape.
Why can't you just individually cast shadows from the triangles that you use to contruct the polygons, then? That seems to me like it would be fairly straighforward. I must be missing sometihng.
What do you mean? Casting the individual triangles produced by triangulation into shadows? If that is what you mean, I should look into it, but it will be inefficient as it will force me to triangulate twice per frame.
SirCmpwn wrote:
What do you mean? Casting the individual triangles produced by triangulation into shadows? If that is what you mean, I should look into it, but it will be inefficient as it will force me to triangulate twice per frame.
You mean twice per square? It's only (triangles per polygon) times more inefficient than trying to find the corner vertices of each polygon and casting those, imho.
That's a good point, it would be about as efficient. I'll look into how it could be done.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement