# Collision within a poly

For an html5 engine I'm making, for speed I'm using a path poly. I'm having trouble trying to find ways to get collision with the walls of the poly. To make it simple I just have a vector for the object and an array of vectors for the poly. I'm using Cartesian vectors and they're 2d.

Say poly = [[550,0],[169,523],[-444,323],[-444,-323],[169,-523]], it's just a pentagon I generated.

The object that will collide is object, object.pos is its position and object.vel is it's velocity. They're both 2d vectors too.

I've had some success to get it to find a collision, but it's just black box code I ripped from a c++ example. It's very obscure inside and all it does though is return true/false and doesn't return what vertices are collided or collision point, I'd really like to be able to understand this and make my own so I can have more meaningful collision. I'll tackle that later though.

Again the question is just how does one find a collision to walls of a poly given you know the poly vertices and the object's position + velocity? If more info is needed please let me know. And if all anyone can do is point me to the right direction that's great.

• Position and Position + Velocity/Time gives you a line segment. Each point of your poly when combined to the next also gives you a line segment. Given two line segments you can follow this stackoverflow.com/questions/563198/… to get where you want to go. – Patrick Hughes Mar 16 '12 at 22:34
• I was gonna link you to this tutorial before I saw Patrick's excellent comment: codeproject.com/Articles/15573/2D-Polygon-Collision-Detection. It's more indepth than what you need though – Jeff Mar 16 '12 at 22:42
• @Jeff +1 link it anyways, codeproject is a great reference =) – Patrick Hughes Mar 16 '12 at 22:46
• @PatrickHughes, I'm checking that out. It looks perfect. I've never taken linear algebra though. I understand mathematics better in a programming language form. Is there any way someone could put that in a pseudocode? (I know I should take classes, but currently very broke) – G1i1ch Mar 16 '12 at 23:08
• The linear algebra used in that post would probably only be covered in a week or two. Just google "vector cross product". – user10968 Mar 17 '12 at 3:47