Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm currently developing a Flash game like 'Tiny Wings'. I have a lot of work done, but i'm currently working on placing the items ( coins and obstacles ) on the terrain.

My player it is moving on a auto-generated terrain (based on Emanuele Feronato's tutorials) so every time the player's x position is greater than (screenWidth + x) another hill is generated and so on.

I'm currently having problems placing the items in a correct angle and put 5 or more items together on a hill.

Could you please help me with this?

Thanks, Regards.

PS: This is the URL to the Emanuele Feronato post and the code to make the hills

share|improve this question
up vote 2 down vote accepted

Just find the hill-element that's at the position where you want to place your item and rotate it to match the orientation of the hill-element. You can get the angle of the element by doing something like this (please note that the angle will be in radians):

segment = vertexB - vertexA
angle = atan2(segment.y, segment.x)

Personally, I would just store the vertices (vectors) that make out the slope of the terrain in a separate data-structure, because reading them out from the box2d bodies can be cumbersome. To find vertexA and vertexB given a desired item-position itemX, you would iterate through the hill-vertices (assuming they are ordered from left to right) and stop whenever vertices[currentIndex].x is bigger than itemX. Then vertexB will be vertices[currentIndex] and vertexA will be vertices[currentIndex - 1].

share|improve this answer
Thanks for this answer. I'm trying to implement it as you describe. – Fede Alvarez Sep 18 '12 at 19:25

@bummzack is correct; use trigonometry. also worth noting is the 2d perpendicular vector to a segment is

(-segment.y, segment.x) or (segment.y, -segment.x)

notice the dot product with the segment in both cases is zero (because cos(90) = 0)

share|improve this answer

Your Answer


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.