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.

  • 3
    \$\begingroup\$ 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. \$\endgroup\$ – Patrick Hughes Mar 16 '12 at 22:34
  • 2
    \$\begingroup\$ 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 \$\endgroup\$ – Jeff Mar 16 '12 at 22:42
  • \$\begingroup\$ @Jeff +1 link it anyways, codeproject is a great reference =) \$\endgroup\$ – Patrick Hughes Mar 16 '12 at 22:46
  • \$\begingroup\$ @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) \$\endgroup\$ – G1i1ch Mar 16 '12 at 23:08
  • \$\begingroup\$ The linear algebra used in that post would probably only be covered in a week or two. Just google "vector cross product". \$\endgroup\$ – user10968 Mar 17 '12 at 3:47

This might not directly answer your question, but to make things much simpler for you I would recommend you use box 2d.

There are 2 implementation for javascript:

  • box2dweb which is the most up to date convert of a port.
  • box2d js which is the first (older direct port)

This lib is really good at 2d physics/collisions.

| improve this answer | |

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.