# How to implement rolling hills in 2d game?

This topic is 3800 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello everyone. I'm new to game programming and am creating a 2d game. I'd like to have rolling hills, and uneven terrain, much like Sonic the Hedgehog. I'm not sure how to go about implementing the rolling hills. In addition, I'm not sure how to do collision detection with curves. I've read about the separating axis theorem and from my understanding that only works with convex polygons. Any help, a link to an article a tutorial, just something to set me down the right path, would be greatly appreciated. Thank you!

##### Share on other sites
I haven't tried the "rolling hills" thing, but I did do arbitrary collision detection with 2d terrain before.

One restriction, my terrain was, er, the mathematical word escapes me (driving me nuts, trying to think of the word), but it means, for any given x-value, there is exactly one y-value.

My terrain was a fractal, represented by a series of line segments, a sequence of (x,y) coords. So, I had maybe a few thousand line segments representing the terrain, sorted by x value.

Each object in the game kept track of an index into the terrain array of the segment directly below it. When checking for collision with the terrain, you already know which segment you were previously above (initially, the first time, a search is done) and for each tick of the game clock, the previous terrain segment is very likely to be the same as the current one, and this is easy, and fast to check. If not, then it's either one to the left, or one to the right, and whether left or right, this is also easy to know -- no long search required. The next test to do is check if both the y coords of the endpoints of the terrain segment are below the game object. If both are below, then the entire segment is below, and the object has not collided with the terrain. If both are above, then the object has collided with the terrain. if one is above, and one is below, then you have to do it "the hard way", and interpolate (by similar triangles, say) to see if a collision has occurred.

For a given pair of coords representing a terrain segment, (x1,y1), (x2,y2), and a game object at x, y, to figure if (x,y) is above or below the line segment, find (x3,y3) such that x3 == x, and see if y3 is above or below y.

Something like (GPL'ed (but trivial) code from wordwarvi follows:)

int interpolate(int x, int x1, int y1, int x2, int y2){ 	/* return corresponding y on line x1,y1,x2,y2 for value x */ 	/* 		(y2 -y1)/(x2 - x1) = (y - y1) / (x - x1)     by similar triangles. 		(x -x1) * (y2 -y1)/(x2 -x1) = y - y1	     a little algebra... 		y = (x - x1) * (y2 - y1) / (x2 -x1) + y1;    I think there's one more step 	                                                     which would optimize this a bit more. 							     but I forget how it goes. 	*/ 	if (x2 == x1) 		return y1; 	else 		return (x - x1) * (y2 - y1) / (x2 -x1) + y1;}

For generating rolling terrain... a seqnence of line segments conforming to some trig functions (sine, cosine, that sort of thing, springs to mind.)

Getting things to roll along the surface of the terrain is another matter which I can't really offer any advice about.

The above is just what I have done, there may be better ways, but that worked pretty well for me.

(Hmm, no preview? Hope this post works...)

-- steve

##### Share on other sites
Thanks smcameron! That helps with the uneven terrain, but I'm still wondering how to collide against curves.

##### Share on other sites
The N Tutorials address this, and much more.

1. 1
2. 2
3. 3
Rutin
15
4. 4
khawk
13
5. 5
frob
12

• 9
• 9
• 11
• 11
• 23
• ### Forum Statistics

• Total Topics
633665
• Total Posts
3013245
×