After many unsuccessful attempts at doing a simple perspective racing game I have finally figured out the algo.
Essentially you have a road in 3D vertexes. You only need as many vertexes to make the road look like it stretches into the distance.
The vertex z components never change for these and neither do the y. Only the x component changes.
This essentially splits the road into horizontal sections.
Ok what about curves? Well I thought about this forever and came up with this.
For any of you that have done the fire effect or any kind of texture cascading you will understand this. First you need to setup a loop that transfers one segment of road offset on x to the next segment. Basically you are handing the offset value off to the next segment.
The offset value is to simulate curves. After all if we are in 3D and all vertexes have the same x value....the road will be perfectly straight. The only thing that would cause a bend is if one vertex was offset on x some distance from the previous vertex x. And since everything scrolls from max z to min z (for forward movement) all you have to do is change the first vertex or the most distant one's offset. This will be transferred all the way down the chain.
So how to you store the road data?
Simple:
So in your editor for the game you simply allow the user to add in curves. All you must do is decide on a sampling size and find out how much each vertex is offset from the center of the curve.
- Neutral offset on x means you will have a straight road.
- Negative offset means the road is curving to the left, or recovering from a right turn
- Positive offset means the road is curving to the right, or recovering from a left turn
So one section of road might look like this in memory.
0 --> we start here on straight road
0 --> straight
0 --> straight
0 --> straight
0 --> straight
.1 --> road is curving to the right a bit
.4 --> road is curving more to the right
.7 --> road is prob at right side of screen
-.4 --> road is coming back to the left
-.1 --> road is still moving left
0 --> road is straight again
0
0
0
0
This would be a straight away with a sharp curve to the right followed by another straightaway.
You will move through the track data at player.speed/some_constant.
Code:Arraypos+=(player.speed)/some_constant; theroad(topvertex).myoffset=roaddata(Arraypos); theroad(topvertex).x+=theroad(topvertex).myoffset; for (int i=0;i<num_road_segs-1;i++) { theroad(i+1).myoffset=theroad(i).myoffset; }
I had to share that with the board because it was so simple it eluded me for years.
I will try to post a simple demo here soon of like Indy 500 track or something.
Of course with this method you won't be able to turn around....for that you need pure 3D.
If you wanted to add hills....simply change the y offset of the top vertex and transfer that y offset down the chain. The hill will gradually come to you. Don't alter the top vertex y though or you will get weird looking stuff. Only alter (1+top)->end of list.
Also one caveat of the algo is that you must move the same amount in the opposite direction to come out of a hill or curve. This will return all vertexes to their original positions. If you continue to move away from center...the road will go off screen and cause major issues.