Advertisement

coding a racing game where the car turns with the road automatically

Started by December 21, 2021 03:20 AM
7 comments, last by poorya79 2 years, 10 months ago

Hello

This is my first post. I hope my question is not too vague. I want to code a racing game in the same style as one of my all time favorite racing games from my childhood: Buggy Boy.

I'm facing 2 challenges that I haven't been able to solve, but first, here's a video of the original game:

https://youtu.be/Ay4UM606gf0

And here are the challenges I have:

1- In the game, the car always turns with the road, meaning if the player doesn't turn, the car will always be on the road, no matter what direction the road is turning.

As you see in the video, sometimes the road turns to left or right, but the car remains on the road. At first I thought this is just an optical illusion, meaning since the road doesn't affect the car, the road is just a graphic element added and the car is always going straight, but if that's the case, how come the flags and all the obstacles on the road are turning with the road correctly? You can see when they approach the car, they come from the correct direction, dependent on the way the road is facing.

If that's not the case and the road is actually turning, then that means the car is being turned to face the road constantly, but how would the car know the correct angle to turn at every step?

2- How is the road made in a game like this? If the road is made of small blocks, then how are the turns programmed? I'm guessing this is related to the first issue.

Just for context, I'm not new to coding. I've done some game demos in Unity3d and C#, as you can see on my channel above, but I'm new to coding racing games.

Thanks in advance.

None

This is my favourite article on the subject.

I think the stuff about where the car is going relative to the road is just a small hurdle in understanding, you need to tackle.

There's no direction beyond what you decide to include. No turning unless tou come up with it. You can stick that sprite in the center if you like, but the norm is, as far as I can tell, to push the car sideways in a curve, and allow the player to counter this by pressing in the opposite direction.

poorya79 said:

Just for context, I'm not new to coding.

Very good!

Advertisement

I'm afraid the answer to most of those questions is “it depends”, and there is no good answer to many of them.

How it is drawn depends on the era of the game. Old games (70's, early to mid 80s) were drawn one one scanline at a time. That visual style was common because it was straightforward: draw the far away stuff one line at a time, draw the next line based on what was closer, the next line closer still. What was shown on the screen could be figured out by what is in the distance in the course data, and the orientation of the car on the road kept as separate data. In the 80's and early 90's a lot of graphics hardware was sprite and tile based, so rendering it could have been composed that way. Sprites for the road at various distances, sprites for the flags and the car, sprites for the mountains and backgrounds.

Modern graphics systems have a tremendous variety of ways to emulate the old appearance. There could be huge articles devoted to twenty different approaches and they'd only scratch the surface of the options.

I can think of several ways to store the data, several ways to draw the track, several ways to make the car stay inside the road, several ways to make the flags and rocks and other pieces of data move.

The easiest that comes to mind would just be storing a “turns in the track” array for graphics that moves over time, having the overlay map not being really tied to the data just colored as a percentage of the way through that data, and merely walking down the array as the course progresses. The road would then always be centered and the car's position have nothing to do with the turning of the track data, just a position relative to the screen. If the “turns in the track” data says -1, then the car moves -1 (probably slightly left) during that segment, if the data says +5 then it's a big turn (probably to the right) and the car moves +5 unless the player is turning. When rendering in the distance, you'd look at the next few items in the turns in the track array. If it says -1 you'd have a slight turn to the left. If it says +5 you'd have a hard turn to the right. Each one is sequentially up above the other, to the horizon line, slowly moving down as you progress through the array. But that's just one approach.

Apart from the strictly technical aspects of keeping the car on the road, there's the game design side: are you removing the challenge of staying on the road entirely, or only up to a certain point? Why should it be an improvement compared to allowing crashing or being disqualified for leaving the track? What difficulties (e.g. passing opponents or avoiding obstacles) remain to make the game challenging? These objectives are likely to inform technical solutions, for example clipping player position, let physics be damned, vs. gradually penalizing the player with a speed reduction for going offroad.

Omae Wa Mou Shindeiru

Thank you! Really appreciate all the suggestions.

@lorenzogatti The reason I need to make the car rotate with the road is, I wanna stay true to the original gameplay as much as possible ?

None

um… not sure if anybody mentioned already, but seems the curvy track is just an optical illusion. From the simulation perspective it's a straight line all the time.
So the question would be only how to create such illusion.

Advertisement

Do you want to copy the visual style of that game, or only the gameplay?

It looks to me that the visual effect of the curvature in that game is generated entirely by shifting scanlines left or right. The sprites are placed on top of that and shifted right or left to match the road. This is not exactly rocket science, but it does involve a fair bit of experimenting to get it right.

If you only want to copy the gameplay, there is an easier option. Just take any off-the-shelves game engine like Unity or Unreal and move an invisible entity along a predefined path in the middle of the road. Then parent the player's car to that entity, but allow it to move along the local x-axis.

@a light breeze that's a brilliant idea! I'm actually using Unity, so I'll give it a shot ? Thanks!

None

This topic is closed to new replies.

Advertisement