Follow me on Twitter RSS FEED

Jorj

My and my friend, Nathan Wilson, started a band called "Jorj". Here's the official blog:

http://www.theadamgaskins.com/Jorj/

WordPress just got an update, but still hasn't fixed the auto-html-reformat feature/bug, so I'm going to stick with Blogger still. But since I'll pretty much only be posting music and words and maybe pictures [no code] then WordPress is better for Jorj's blog.

In Jorj, I'm referred to as "Brains Rider", and Nathan is referred to as "Sid Williams".

XNA Game Studio: Complex Polygon Collisions

I am working on an NHL shootout game in XNA Game Studio. I had the basic prototype down, with player movement, shooting and grabbing the puck, and a goal. The only thing left was to detect when the puck should bounce off the posts of the goal, and when the puck goes into the goal. I could have just used multiple Rectangle objects, but I decided to figure out how to do Polygon Collision Checking. It would be good to know how to do it, for future reference.

Some Google searches revealed this link. I copied and pasted the function, and made some tweaks. I put it in a Polygon class, which stores a generic List<> of Vector2s. Then I added a funtion:

public bool Intersects(Polygon other)
        {
            foreach (Vector2 point in other.points)
            {
                if(PointInPolygon(point))
                    return true;
            }
            return false;
        }

Then all I had to do was create the polygon collision bounds for all the objects, and say something like:

if (puckBounds.Intersects(goalBounds))
        {
            // Make puck bounce here
        }

I'm glad I learned this, because I'll be able to use my Polygon class for lots of things in the future. Maybe later I can upload the source and/or upload a compiled DLL.