Jump to content
  • Advertisement
Sign in to follow this  
valkrin

How to implement rolling hills in 2d game?

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!