I followed this theory Here to implement a simple scrolling 2D terrain for my running game. I am used to dealing with collision via a tile system but am looking for a good theory on how to perform terrain collision (keeping the player on-top the land) with a midpoint displacement 2D terrain

I am not worried about the physics aspect but how to determine if a character intersects an array of points generated by the theory above. Example if I have an array of points


these points are used to draw lines to make a terrain. Now in my mind I would first find the player center. Take the center X and see which two points it is between. So if X is 4.6 lets say it is between points that lay on X=3 and X=6.

Player X = 4.6
Point1 X = 3, Y = 3
Point2 X = 6, Y = 5

So now knowing this I somehow need to calculate what Y would be giving that X = 4.6. And once I have Y ensure that the Player Y > CalculatedY. If it is not push the player up to what Y the player should be.

So I guess what I a saying is

X = 3, Y = 3
X = 6, Y = 5
X = 4.6, Solve for Y
  • \$\begingroup\$ Could you please give more details about the physics, code examples, etc.? I would love to give an answer, but I'd have to create values and concepts out of nowhere... \$\endgroup\$ Jul 11, 2014 at 14:34
  • 1
    \$\begingroup\$ I think code samples would make the question too specific. Keep it general and just describe at a high level the kind of geometry being created and how you're storing it. \$\endgroup\$
    – House
    Jul 11, 2014 at 14:45
  • \$\begingroup\$ updated with some more logic to try and clear things up \$\endgroup\$
    – PaulBinder
    Jul 11, 2014 at 14:49

1 Answer 1


I will assume that the player collides with the ground only on the middle point.

We can do an approximation of the height of the terrain at a given point with this:

double x = 4.6;
double relativeX = x / widthBetweenPoints;
int terrainIndex = Math.floor(x);
double ratio = relativeX - terrainIndex;
int y = Points[terrainIndex] * (1 - ratio) + Points[terrainIndex + 1] * ratio;


x is the x position of the Player, in pixels.

relativeX is the position of the player scaled down to the array, so that we know on which cell of the array he stands.

widthBetweenPoints is the width, in pixels, between the array points.

terrainIndex is the cell the player is standing on (1 in this case). In fact, the player stands between cell 1 and 2, but we round down to 1.

ratio is where the player stands between the two points (here, 1 and 2, and the ratio is 30%)

Points[] is the array containing the y values of the points or the terrain.

More in details:

  1. We start by scaling the x value of the player down to the size of the terrain. If the x value of the player is 4.6 and the width between 2 points of the terrain is 2, then the relative position is 1.3. The player between point 1 and 2 of the terrain (with index starting at 0) and is at 70% towards 1 and 30% towards 2. More visual: 1 - - P - - - - - - 2

  2. Then, we extract the current terrain index by getting the integer part of the relative x value and extract the ratio of "in between" by getting the floating part.

  3. We then multiply the y value of the terrain at cell 1 by 70% and the value of cell 2 by 30%.

Just watch out that terrainIndex + 1 does not go out of bounds of the array.

Then you can apply as much physics or collisions as you want.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .