In my clone, the lander's horizontal (x) position on the screen will remain constant and when the lander moves in the x direction, the lunar terrain will scroll to the left or right. I need a scrolling terrain map that wraps around and this problem is what I want to talk about in this blog.
My terrain map will be a list of x and y points. The x difference (dx) between the points will be variable. Only a certain portion of the map will be drawn on the screen at any given time. Think of it like a camera panning to the left or right where the camera x coordinate is the same as the lander's.
You could make the map so large that the user won't be able to hit the ends of the map in a reasonable time. But what I want is to have a small amount of points that wrap around, i.e. when the right side ends, it appears on the left side of the screen (and vice versa).
The algorithm is as follows (when the lander moves to the left by landerDx amount):
- Increase the x values of all map points by the landerDx
- if the rightmost map point x coordinate is smaller than the rightmost coordinate shown on screen
- remove the rightmost point from list
- create a new point which has the same y value as the removed rightmost point. The x coordinate of this new point will be (leftmost point).x - dx. Since the x distance between the points are variable we have to have a separate list to determine dx for that particular point pair. Note: In the beginning, since you know all x coordinates, you can generate the dx list by subtracting the x values of consequent points. However, dx between rightmost and left most points is undetermined. You have to separately set it to a value and add that value to the end of dx list. I usually choose that value as the average x distance between all points.
- Draw all map points. Only the points that fit the screen coordinates will be visible.
No comments:
Post a Comment