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'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 http://www.emanueleferonato.com/2011/10/04/create-a-terrain-like-the-one-in-tiny-wings-with-flash-and-box2d-%E2%80%93-adding-more-bumps/

share|improve this question
add comment

2 Answers

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. –  Programlocura Sep 18 '12 at 19:25
add comment

@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
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.