Storing and rendering a terrain in a side-scroller (LibGDX)

I’m in the thinking phase of a new project and I’m stumped about one thing - if I wanted to have a varying height 2D terrain, what would be the best way to store the terrain? Would it take something as easy as storing the terrain as a polygon? If so, how would I be able to texture and collision check with it in LibGDX?
Thanks

A series of points connected by lines (more or less a polygon like you said) would work; to collision test, simply get the points on either side of what you’re testing, and interpolate between them to your current position, and check if your y-value is above the line:

Visual aid:

We want to “cast a shadow” down from our (player, yea lets say this is the player) onto the ground, and see if the shadow is above or below the player. If it’s above our player, that means our player is in the ground, and thus colliding.

To find this magical point, we need to calculate the test line, shown above in bright red:
This is simply the line between two points:

(Let p1 and p2 be the surrounding terrain points, as per the diagram, and t be the test point, or player)

[icode]f(x) = (p2.y - p1.y) / (p2.x - p1.x) * (x - p1.x) + p1.y[/icode]

Then our potential collision point is simply this: [icode](t.x, f(t.x))[/icode]

So just check if your player is below f(player.x) and if so, you’re colliding.

Thanks, I’ll look into this.

On the subject of collisions…
I’ve been thinking about the example you gave me. It’s definitely a step in the direction I want to be going but I have a few questions to ask.

  1. The shadow that you mentioned, how long should it be? Should it only be the y velocity of the player?
  2. If I wanted to test a collision in a situation like this:

And I had gotten so far as to collide with one point, what would be the best step to take from there? I have a couple of thoughts, one is that I would start to rotate the rectangle (for demonstration) until two points were contacting on the ground, that are “supporting” at least 50% of the size of the rectangle. What do you think?

Well, for more complex scenarios like that, since you’re using libGDX you might as well then use Box2D

This will save a lot of time in the long run if you’re doing much beyond simple up-down point collision tests like I showed above.

Yeah, you’re right. It’s probably also going to be a lot more accurate and reliable than anything I could make. Thanks for your help, I’ll read up on Box2D.