이 페이지는 아직 한국어로 번역되지 않아 영문 원문을 표시하고 있습니다.
가이드Racing 3D Template
The racing3d template is a small 3D racing game: two laps of a closed circuit against two computer-driven rivals, from the back of the grid, with a countdown, checkpoints, a ghost of your best lap and a results screen. It plays with a keyboard, a gamepad or touch, on the desktop, phones and the web.
go run github.com/nsgo-dev/game/cmd/game@latest new -template racing3d myracer
cd myracer
go tool nsgo dev
Templates describes game new and nsgo init -template, which writes the same project from the module github.com/nsgo-dev/game/template/racing3d.
What it shows
- Raycast cars from
vehicle3don aphysics3dworld, every car tuned by onevehicle3d.Configfile. - Laps and checkpoints with
vehicle3d.Race: a lap counts only when a car passes every checkpoint in order, so cutting the track does not pay. - A three-second countdown, a HUD with the lap, the position and the lap clock, and a results screen. Finishing first wins; finishing lower, or not finishing before both rivals do, loses.
- Rivals that follow a racing line: they steer toward a point ahead on the line and hold a speed planned to brake for every corner, scaled by each rival's pace.
- A ghost of your best flying lap, recorded with
vehicle3d.GhostRecorder, raced against on every lap after the first and kept between launches in the app's storage as the YAMLvehicle3d.EncodeGhostwrites. Lap 1 starts on the grid behind the line, so it is neither saved as the ghost nor raced against one, and a one-lap race has no ghost. The saved ghost names a hash of the track file, so editing anything indata/track.yamlretires it. - A scene on a GPU view drawn with
render3d's lit PBR pipeline: the road, curbs, posts and cars are meshes built in code, and the crates beside the road are the placeholder pack'scrate.glb, read withgltf. - One input map in
game.yamlfor the keyboard, gamepads and touch, read withinput.Actionsandvehicle3d.Controls, and aninput.TouchControlslayer built fromcontrols.touch.yamlplaced inside the safe area (visible from the first frame on phones and on desktop once touched, and hidden again on keyboard or controller input). The root scope requests landscape orientation. - Tests that play whole races through the app with each kind of input, reaching a win and a loss.
Controls
| Keyboard | Gamepad | Touch | |
|---|---|---|---|
| Accelerate | Up or W | Right trigger or A | GAS |
| Brake, then reverse | Down or S | Left trigger or X | BRAKE |
| Steer | Left and Right, or A and D | Left stick or the D-pad | the stick, anywhere on the lower left |
| Handbrake | Space | B | |
| Back on the road | R | Y | R |
| Race again | Enter | Start | Race again |
How it is organised
| File | What it holds |
|---|---|
game.yaml |
The project file: the window and the input map, with the actions throttle, brake, steerLeft, steerRight, handbrake, respawn and confirm and the steer axis over the keys and the left stick. |
controls.touch.yaml |
The on-screen touch layout (input.TouchControls): steering stick, GAS, BRAKE and respawn buttons anchored inside the safe area. |
data/track.yaml |
The circuit: its centerline points, width, laps, checkpoints, starting grid, and the rivals' names, colors, pace and lanes. |
data/car.yaml |
The car's tuning, a vehicle3d.Config. |
track.go |
The track file's format: ParseTrack refuses unknown fields and out-of-range values, and EncodeTrack writes it back unchanged. |
course.go |
The track turned into geometry: samples every couple of meters along the centerline, the checkpoints, the grid, and the racing line's speed at each sample. |
race.go |
The race, with no drawing: the physics world, the cars, the countdown, the laps, the standings, the results and the ghost. |
driver.go |
The racing-line driver the rivals use. |
scene.go, meshes.go, renderer.go |
The meshes, the lights and the GPU drawing. |
game.go |
The root component: the GPU view, the HUD, the results screen, the touch controls, the chase camera and the game.Loop. |
game_test.go, race_test.go, track_test.go |
Races played through the app with a gamepad, the keyboard and touch; races run on the simulation alone; the track format. |
The race is plain Go over physics3d and vehicle3d with no drawing, so race_test.go runs whole races in milliseconds. The app's tests start it with nsgotest.NoGPU, as nsgotest draws a GPU view with a software rasterizer far too slow for minutes of racing; TestRaceViewDraws checks the drawing on its own at a small size.
Where to change it
- The circuit: edit
points,widthandstartindata/track.yaml, keeping every point within 300 m of the origin, where the ground is. The road, curbs, checkpoints, grid and racing line all follow the points, andTestCourseGeometrychecks the result. - The rivals: each rival's
pacemakes it faster or slower, and the file takes up to seven rivals.lapssets the race's length. - The car: edit
data/car.yaml; vehicle3d describes each field. - Collisions between cars: the cars pass through each other so that a crowded corner never wrecks the race. Add
layerCarto the chassis mask innewRace, inrace.go, to make them collide. - The controls: change the bindings in
game.yaml; the code reads only the action and axis names. - The art: load your own glTF models with
gltf.LoadFSinsceneMeshesand add their primitives as parts, as the crates are.