# Creating custom collision map for 2D

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 • 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... Aug 10, 2012 at 13:13
• Aug 11, 2012 at 14:58
• ...and just to be clear, its the part about interpolating between two vertices and checking the height value I think you should check out. Aug 11, 2012 at 15:00
• 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 Aug 14, 2012 at 17:47
• You might get more answers to unity specific questions at Unity Answers. Aug 14, 2012 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. 