2

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/

2 Answers2

2

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

bummzack
  • 22,646
  • 5
  • 61
  • 86
1

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

milkplus
  • 308
  • 3
  • 6