Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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 it's 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.

share|improve this question
3  
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
2  
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". –  NeuroFuzzy Mar 17 '12 at 3:47
add comment

1 Answer

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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