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.

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

share|improve this question
    
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... –  bitbitbot Aug 10 '12 at 13:13
    
1  
...and just to be clear, its the part about interpolating between two vertices and checking the height value I think you should check out. –  Mikael Högström Aug 11 '12 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 –  bitbitbot Aug 14 '12 at 17:47
    
You might get more answers to unity specific questions at Unity Answers. –  Darcara Aug 14 '12 at 18:18
show 1 more comment

1 Answer

up vote 2 down vote accepted
+25

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

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.