This commit is contained in:
bdnugget 2024-10-01 11:56:42 +02:00
parent 39dce85312
commit e079e3743f

36
main.go
View File

@ -15,9 +15,9 @@ const (
) )
var ( var (
cameraDistance = float32(64.0) cameraDistance = float32(20.0)
cameraYaw = float32(45.0) cameraYaw = float32(145.0)
cameraPitch = float32(30.0) cameraPitch = float32(90.0)
) )
type Tile struct { type Tile struct {
@ -157,12 +157,6 @@ func UpdatePlayer(player *Player, deltaTime float32, mapGrid [][]Tile, camera *r
player.PosTile = targetTile // Update the player's tile position player.PosTile = targetTile // Update the player's tile position
player.TargetPath = player.TargetPath[1:] // Move to the next tile in the path player.TargetPath = player.TargetPath[1:] // Move to the next tile in the path
} }
// Update camera target and position
camera.Target = player.PosActual
camera.Position.X = player.PosActual.X
camera.Position.Y = player.PosActual.Y
camera.Position.Z = player.PosActual.Z + cameraDistance
} }
} }
@ -194,19 +188,16 @@ func main() {
} }
// Initialize 3D camera // Initialize 3D camera
cameraDistance := float32(64.0)
cameraYaw := float32(45.0)
cameraPitch := float32(30.0)
camera := rl.Camera3D{ camera := rl.Camera3D{
Position: rl.NewVector3(player.PosActual.X, player.PosActual.Y, player.PosActual.Z+cameraDistance), // Updated camera position Position: rl.NewVector3(player.PosActual.X+cameraDistance, player.PosActual.Y+cameraDistance, player.PosActual.Z+cameraDistance),
Target: player.PosActual, // The point the camera looks at (player) Target: player.PosActual, // The point the camera looks at (player)
Up: rl.NewVector3(0, 1, 0), // Camera up vector (y-axis is up) Up: rl.NewVector3(0, 1, 0), // Camera up vector (y-axis is up)
Fovy: 45.0, // Field of view Fovy: 45.0, // Field of view
Projection: rl.CameraPerspective, // Perspective camera mode Projection: rl.CameraPerspective, // Perspective camera mode
} }
camera.Target = player.PosActual
rl.SetTargetFPS(60) rl.SetTargetFPS(60)
for !rl.WindowShouldClose() { for !rl.WindowShouldClose() {
@ -215,23 +206,17 @@ func main() {
frameCnt++ frameCnt++
frameCnt %= 60 frameCnt %= 60
HandleInput(&player, mapGrid, &camera)
UpdatePlayer(&player, deltaTime, mapGrid, &camera)
// Update camera based on mouse wheel // Update camera based on mouse wheel
wheelMove := rl.GetMouseWheelMove() wheelMove := rl.GetMouseWheelMove()
if wheelMove != 0 { if wheelMove != 0 {
cameraDistance += wheelMove * 5 cameraDistance += -wheelMove * 5
if cameraDistance < 10 { if cameraDistance < 1 {
cameraDistance = 10 cameraDistance = 1
} else if cameraDistance > 100 { } else if cameraDistance > 100 {
cameraDistance = 100 cameraDistance = 100
} }
} }
// Update camera position directly above the player
camera.Target = player.PosActual // Ensure the camera always looks at the player
// Update camera based on arrow keys (camera orbits player) // Update camera based on arrow keys (camera orbits player)
if rl.IsKeyDown(rl.KeyRight) { if rl.IsKeyDown(rl.KeyRight) {
cameraYaw -= 60 * deltaTime // Rotate right cameraYaw -= 60 * deltaTime // Rotate right
@ -252,7 +237,10 @@ func main() {
} else if cameraPitch < -10.0 { } else if cameraPitch < -10.0 {
cameraPitch = -10.0 cameraPitch = -10.0
} }
HandleInput(&player, mapGrid, &camera)
UpdatePlayer(&player, deltaTime, mapGrid, &camera)
camera.Target = player.PosActual
// Convert spherical coordinates to cartesian to calculate the camera's position // Convert spherical coordinates to cartesian to calculate the camera's position
camera.Position.X = player.PosActual.X + cameraDistance*float32(math.Cos(float64(cameraPitch)*math.Pi/180.0))*float32(math.Sin(float64(cameraYaw)*math.Pi/180.0)) camera.Position.X = player.PosActual.X + cameraDistance*float32(math.Cos(float64(cameraPitch)*math.Pi/180.0))*float32(math.Sin(float64(cameraYaw)*math.Pi/180.0))
camera.Position.Y = player.PosActual.Y + cameraDistance*float32(math.Sin(float64(cameraPitch)*math.Pi/180.0)) camera.Position.Y = player.PosActual.Y + cameraDistance*float32(math.Sin(float64(cameraPitch)*math.Pi/180.0))