Below is a simple implementation of a 3D terrain generator with a movable camera in Go. This example uses basic math library for vector calculations and does not rely on any external graphics libraries. The project structure is as follows:
terrain-generator/
|---- main.go
|---- terrain.go
|---- camera.go
|---- vector.go
|---- utils.go
vector.go
This file contains basic vector operations.
package main
import (
"math"
)
type Vector3 struct {
X, Y, Z float64
}
func (v *Vector3) Add(other *Vector3) *Vector3 {
return &Vector3{v.X + other.X, v.Y + other.Y, v.Z + other.Z}
}
func (v *Vector3) Sub(other *Vector3) *Vector3 {
return &Vector3{v.X - other.X, v.Y - other.Y, v.Z - other.Z}
}
func (v *Vector3) Mul(scalar float64) *Vector3 {
return &Vector3{v.X * scalar, v.Y * scalar, v.Z * scalar}
}
func (v *Vector3) Dot(other *Vector3) float64 {
return v.X*other.X + v.Y*other.Y + v.Z*other.Z
}
func (v *Vector3) Magnitude() float64 {
return math.Sqrt(v.X*v.X + v.Y*v.Y + v.Z*v.Z)
}
func (v *Vector3) Normalize() *Vector3 {
mag := v.Magnitude()
return &Vector3{v.X / mag, v.Y / mag, v.Z / mag}
}
func (v *Vector3) Cross(other *Vector3) *Vector3 {
return &Vector3{
v.Y*other.Z - v.Z*other.Y,
v.Z*other.X - v.X*other.Z,
v.X*other.Y - v.Y*other.X,
}
}
camera.go
This file contains camera related operations.
package main
type Camera struct {
Position *Vector3
Direction *Vector3
Up *Vector3
Right *Vector3
}
func NewCamera(position *Vector3) *Camera {
direction := &Vector3{0, 0, -1}
up := &Vector3{0, 1, 0}
right := direction.Cross(up).Normalize()
return &Camera{
Position: position,
Direction: direction,
Up: up,
Right: right,
}
}
func (c *Camera) Move(front, right, up float64) {
c.Position = c.Position.Add(c.Direction.Mul(front))
c.Position = c.Position.Add(c.Right.Mul(right))
c.Position = c.Position.Add(c.Up.Mul(up))
}
func (c *Camera) Rotate(pitch, yaw float64) {
// Simple rotation, might need more complex math for full 3D rotation
c.Direction.X = math.Sin(yaw) * math.Cos(pitch)
c.Direction.Y = math.Sin(pitch)
c.Direction.Z = -math.Cos(yaw) * math.Cos(pitch)
c.Right = c.Direction.Cross(c.Up).Normalize()
}
terrain.go
This file generates a simple 3D terrain.
package main
import (
"math/rand"
"time"
)
type Terrain struct {
Width, Height int
Scale float64
}
func NewTerrain(width, height int, scale float64) *Terrain {
return &Terrain{
Width: width,
Height: height,
Scale: scale,
}
}
func (t *Terrain) Generate() [][]float64 {
rand.Seed(time.Now().UnixNano())
terrain := make([][]float64, t.Width)
for x := range terrain {
terrain[x] = make([]float64, t.Height)
for y := range terrain[x] {
terrain[x][y] = rand.Float64() * t.Scale
}
}
return terrain
}
utils.go
Some utility functions for rendering.
package main
import (
"fmt"
)
func ClearScreen() {
fmt.Print("\033[H\033[2J")
}
func DrawTerrain(terrain [][]float64, camera *Camera) {
ClearScreen()
for y, row := range terrain {
for x, height := range row {
// Simple projection, real 3D would require more complex math
projectedX := int(float64(x) * 10 / (10 - camera.Position.Z)) + 20
projectedY := int(float64(y) * 10 / (10 - camera.Position.Z)) + 10
if projectedX >= 0 && projectedX < 40 && projectedY >= 0 && projectedY < 20 {
fmt.Printf("\033[%d;%dH*\033[0m", projectedY, projectedX)
}
}
}
}
main.go
The main function.
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
terrain := NewTerrain(20, 20, 5)
generatedTerrain := terrain.Generate()
camera := NewCamera(&Vector3{0, 0, 0})
reader := bufio.NewReader(os.Stdin)
for {
DrawTerrain(generatedTerrain, camera)
fmt.Println("Enter command (W/A/S/D, P/Y to rotate, Q to quit):")
command, _, _ := reader.ReadLine()
switch string(command) {
case "w":
camera.Move(0.1, 0, 0)
case "s":
camera.Move(-0.1, 0, 0)
case "a":
camera.Move(0, -0.1, 0)
case "d":
camera.Move(0, 0.1, 0)
case "p":
camera.Rotate(0.1, 0)
case "y":
camera.Rotate(0, 0.1)
case "q":
return
default:
fmt.Println("Invalid command")
}
}
}
This code provides a simple text-based 3D terrain generator with a movable camera. The terrain is represented by asterisks (*) on the screen, and you can move the camera using W, A, S, D keys and rotate it using P and Y keys. The Q key is used to quit the program.
This is a very basic implementation and doesn't handle many edge cases or provide a perfect 3D rendering. For a full-featured 3D graphics application, consider using a dedicated graphics library.