PDA

View Full Version : What is the best way to produce a race track in OpenGl?



rob4everever
04-23-2018, 08:39 AM
Hey guys I hope this is the right place to post this question.

I am currently learning OpenGL and feel I have a good grasp of the basics, so I want to put them into practice and make a simple top down 2d racing game.

I am having trouble with the track element of the game.

I am not sure the best way (or any way really) to generate a track. How could one do it? I could draw it in photoshop and load it in or perhaps I could generate it with primitive shapes in OpenGl.

I am just struggling where to start could someone offer some advice or provide some resources please?

Thanks

ProgrammerX
04-24-2018, 12:38 AM
I am not sure the best way (or any way really) to generate a track. How could one do it? I could draw it in photoshop and load it in or perhaps I could generate it with primitive shapes in OpenGl.

I am just struggling where to start could someone offer some advice or provide some resources please?


I doubt that there is any resource on "how to build a 2d race track". There are so many possible ways to achieve what you want and the best way is highly dependent on what you actually want to do. Well... I can at least tell you what my first idea was, when I read your question.

- Get an asphalt texture from somewhere, which can be repeated (don't know the correct term here)
- Learn about NURBS
- Use NURBS to create a closed curve that represents the center line of your track
- Define a track width and calculate the border NURBS of your track by applying half of the width to each side of your curve.
- Now you have an inner and an outer border. So calculate for each pixel if it is between the two curves or not. If yes, use the asphalt texture for your pixel. If not, use something else (dirt, grass, whatever you like)

The good thing about that approach is, that you can also use the boundary curves exactly the same way, to determine if the car is on the track or not and therefore use different grip parameters. Cant tell if this is the best approach, but it makes building race tracks quiet easy, a lot easier than putting different track segments together which you have to create in Photoshop first.

GClements
04-24-2018, 04:53 AM
- Define a track width and calculate the border NURBS of your track by applying half of the width to each side of your curve.

Note that this is far from straightforward. It isn't generally possible to define an offset curve as another curve of the same type and degree. E.g. an offset curve to a cubic Bézier curve is a rational curve of degree 10 (legacy OpenGL evaluators are only guaranteed to work up to order 8 = degree 7). Typically, you'd create an approximation based upon position, tangents and curvature; you'll get a closer approximation the smaller the angle of the curve.

A tessellation shader can be used to tessellate exact offset curves, but that limits you to OpenGL 4+ hardware.