I have a 2D level that I have built in my map editor, and I was wondering the best way to create a collision map for it.

I have a collision map layer in my map editor, which saves each node (for example, a rectangle would be just 0,0 0,50 50,60 0,60)

I was just wondering how I could take my node locations and convert it to a collision map for Unity.

Thanks! I feel this has to be able to be done, I can think of how I would do it in flash... but I am not sure how to manipulate it in Unity.

EDIT PICTURE: So I have a node at each one of these red dots (surrounding my terrain lets say) I want to build a collider from those nodes

nodes collision map

  • \$\begingroup\$ I have figured out how to do it in Flash and XNA... is it even possible in UnityPro? (I have basic) but this seems like a fundamental task for creating certain games... \$\endgroup\$ – bitbitbot Aug 10 '12 at 13:13
  • \$\begingroup\$ Check out gamedev.stackexchange.com/questions/33484/… \$\endgroup\$ – Mikael Högström Aug 11 '12 at 14:58
  • 1
    \$\begingroup\$ ...and just to be clear, its the part about interpolating between two vertices and checking the height value I think you should check out. \$\endgroup\$ – Mikael Högström Aug 11 '12 at 15:00
  • \$\begingroup\$ Thanks Mikael, I suppose I could just roll my own collision, not 100% what I would want, but I think it may be the best solution, see if anyone else has a better idea, if not, I will go with interpolating \$\endgroup\$ – bitbitbot Aug 14 '12 at 17:47
  • \$\begingroup\$ You might get more answers to unity specific questions at Unity Answers. \$\endgroup\$ – Darcara Aug 14 '12 at 18:18

I thought since these types of questions seem to show up now and then I'd explain myself a bit more in detail.

Assuming you have a 2D level from a heightmap like in the image without any "overhanging" geometry:

What we want to do is check if point p is below the line, in which case we have a collision at l_p. To accomplish this we interpolate v1.Y and v2.Y value to check the height of the line at p.X. If this l_p.Y is larger than p.Y then there is a collision.

  • Start by checking if v1.X <= p.X < v2.X
  • calculate interpolation amount (a), ie, how large part of v1.Y and v2.Y respectively should be used where a = (p.X - v1.X) / (v2.X - v1.X)
  • calculate interpolated Y by y = (1-a) * v1.Y + a * v2.Y
  • if y >= p.Y then there is a collision
  • If needed solve interpenetration by setting p.Y = y
  • IMPORTANT: If you apply an impulse to the body at this point make sure to check that there is actually a closing velocity ie that there is a velocity pointing downwards in this case. Otherwise you might get another collision reported next frame sending the body downwards again.

check if point p is below the line v1-v2


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.