fix camera a bit
This commit is contained in:
parent
e079e3743f
commit
b25478612d
@ -1,8 +1 @@
|
||||
package utils
|
||||
|
||||
const (
|
||||
MapWidth = 100
|
||||
MapHeight = 100
|
||||
TileSize = 32
|
||||
TileHeight = 2.0
|
||||
)
|
||||
|
203
main.go
203
main.go
@ -8,8 +8,8 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
MapWidth = 100
|
||||
MapHeight = 100
|
||||
MapWidth = 50
|
||||
MapHeight = 50
|
||||
TileSize = 32
|
||||
TileHeight = 2.0
|
||||
)
|
||||
@ -17,7 +17,7 @@ const (
|
||||
var (
|
||||
cameraDistance = float32(20.0)
|
||||
cameraYaw = float32(145.0)
|
||||
cameraPitch = float32(90.0)
|
||||
cameraPitch = float32(45.0) // Adjusted for a more overhead view
|
||||
)
|
||||
|
||||
type Tile struct {
|
||||
@ -42,7 +42,7 @@ func InitMap() [][]Tile {
|
||||
mapGrid[x][y] = Tile{
|
||||
X: x,
|
||||
Y: y,
|
||||
Height: 1.0 + float32(x%5), //float32((x + y) % 10), // Example height
|
||||
Height: 1.0 + float32(x%5), // Example height
|
||||
Walkable: true, // Set to false for obstacles
|
||||
}
|
||||
}
|
||||
@ -79,13 +79,11 @@ func DrawMap(mapGrid [][]Tile) {
|
||||
}
|
||||
|
||||
func DrawPlayer(player Player, mapGrid [][]Tile) {
|
||||
// Get current tile height
|
||||
currentTile := mapGrid[player.PosTile.X][player.PosTile.Y]
|
||||
// Draw player cube with height from the map
|
||||
// Draw the player based on its actual position (PosActual) and current tile height
|
||||
playerPos := rl.Vector3{
|
||||
X: float32(player.PosTile.X * TileSize),
|
||||
Y: currentTile.Height * TileHeight,
|
||||
Z: float32(player.PosTile.Y * TileSize),
|
||||
X: player.PosActual.X,
|
||||
Y: mapGrid[player.PosTile.X][player.PosTile.Y].Height * TileHeight,
|
||||
Z: player.PosActual.Z,
|
||||
}
|
||||
rl.DrawCube(playerPos, 16, 16, 16, rl.Green) // Draw player cube
|
||||
}
|
||||
@ -97,9 +95,12 @@ func GetTileAtMouse(mapGrid [][]Tile, camera *rl.Camera3D) (Tile, bool) {
|
||||
mouse := rl.GetMousePosition()
|
||||
|
||||
// Unproject mouse coordinates to 3D ray
|
||||
var ray rl.Ray = rl.GetMouseRay(mouse, *camera)
|
||||
ray := rl.GetMouseRay(mouse, *camera)
|
||||
|
||||
// Calculate the distance from the camera to the ray's intersection with the ground plane (Y=0)
|
||||
if ray.Direction.Y == 0 {
|
||||
return Tile{}, false
|
||||
}
|
||||
dist := -ray.Position.Y / ray.Direction.Y
|
||||
|
||||
// Calculate the intersection point
|
||||
@ -120,43 +121,29 @@ func GetTileAtMouse(mapGrid [][]Tile, camera *rl.Camera3D) (Tile, bool) {
|
||||
return Tile{}, false
|
||||
}
|
||||
|
||||
func UpdatePlayer(player *Player, deltaTime float32, mapGrid [][]Tile, camera *rl.Camera) {
|
||||
if len(player.TargetPath) > 0 {
|
||||
targetTile := player.TargetPath[0]
|
||||
|
||||
// Calculate the direction vector to the target
|
||||
direction := rl.Vector3{
|
||||
X: float32(targetTile.X*TileSize) - player.PosActual.X,
|
||||
Y: float32(targetTile.Y*TileSize) - player.PosActual.Y,
|
||||
func (player *Player) MoveTowards(target Tile, deltaTime float32, mapGrid [][]Tile) {
|
||||
// Calculate the direction vector to the target tile
|
||||
targetPos := rl.Vector3{
|
||||
X: float32(target.X * TileSize),
|
||||
Y: mapGrid[target.X][target.Y].Height * TileHeight,
|
||||
Z: float32(target.Y * TileSize),
|
||||
}
|
||||
|
||||
// Normalize the direction vector to get smooth movement
|
||||
dist := float32(math.Sqrt(float64(direction.X*direction.X + direction.Y*direction.Y)))
|
||||
if dist > 0 {
|
||||
direction.X /= dist
|
||||
direction.Y /= dist
|
||||
// Calculate direction and normalize it for smooth movement
|
||||
direction := rl.Vector3Subtract(targetPos, player.PosActual)
|
||||
distance := rl.Vector3Length(direction)
|
||||
if distance > 0 {
|
||||
direction = rl.Vector3Scale(direction, player.Speed*deltaTime/distance)
|
||||
}
|
||||
|
||||
// Move towards the target
|
||||
newPosX := player.PosActual.X + direction.X*player.Speed*deltaTime
|
||||
newPosY := player.PosActual.Y + direction.Y*player.Speed*deltaTime
|
||||
|
||||
// Check if the player is moving into a non-walkable tile
|
||||
if !mapGrid[int(newPosX)/TileSize][int(newPosY)/TileSize].Walkable {
|
||||
return
|
||||
}
|
||||
|
||||
// Update player's position
|
||||
player.PosActual.X = newPosX
|
||||
player.PosActual.Y = newPosY
|
||||
|
||||
// Check if the player reached the target tile
|
||||
if dist < 1.0 {
|
||||
player.PosActual.X = float32(targetTile.X * TileSize)
|
||||
player.PosActual.Y = float32(targetTile.Y * TileSize)
|
||||
player.PosTile = targetTile // Update the player's tile position
|
||||
player.TargetPath = player.TargetPath[1:] // Move to the next tile in the path
|
||||
}
|
||||
// Move the player towards the target tile
|
||||
if distance > 1.0 {
|
||||
player.PosActual = rl.Vector3Add(player.PosActual, direction)
|
||||
} else {
|
||||
// Snap to the target tile when close enough
|
||||
player.PosActual = targetPos
|
||||
player.PosTile = target // Update player's tile
|
||||
player.TargetPath = player.TargetPath[1:] // Move to next tile in path if any
|
||||
}
|
||||
}
|
||||
|
||||
@ -173,95 +160,99 @@ func HandleInput(player *Player, mapGrid [][]Tile, camera *rl.Camera) {
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
rl.InitWindow(800, 600, "RuneScape-like Game")
|
||||
defer rl.CloseWindow()
|
||||
|
||||
frameCnt := 0
|
||||
|
||||
mapGrid := InitMap()
|
||||
|
||||
player := Player{
|
||||
PosTile: Tile{X: 10, Y: 10},
|
||||
PosActual: rl.Vector3{X: float32(10 * TileSize), Y: float32(10 * TileSize), Z: float32(mapGrid[10][10].Height /* * TileHeight */)},
|
||||
Speed: 100.0, // pixels per second
|
||||
}
|
||||
|
||||
// Initialize 3D camera
|
||||
camera := rl.Camera3D{
|
||||
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)
|
||||
Up: rl.NewVector3(0, 1, 0), // Camera up vector (y-axis is up)
|
||||
Fovy: 45.0, // Field of view
|
||||
Projection: rl.CameraPerspective, // Perspective camera mode
|
||||
}
|
||||
|
||||
camera.Target = player.PosActual
|
||||
|
||||
rl.SetTargetFPS(60)
|
||||
|
||||
for !rl.WindowShouldClose() {
|
||||
deltaTime := rl.GetFrameTime()
|
||||
|
||||
frameCnt++
|
||||
frameCnt %= 60
|
||||
|
||||
func UpdateCamera(camera *rl.Camera3D, player rl.Vector3, deltaTime float32) {
|
||||
// Update camera based on mouse wheel
|
||||
wheelMove := rl.GetMouseWheelMove()
|
||||
if wheelMove != 0 {
|
||||
cameraDistance += -wheelMove * 5
|
||||
if cameraDistance < 1 {
|
||||
cameraDistance = 1
|
||||
} else if cameraDistance > 100 {
|
||||
cameraDistance = 100
|
||||
if cameraDistance < 10 {
|
||||
cameraDistance = 10
|
||||
}
|
||||
if cameraDistance > 250 {
|
||||
cameraDistance = 250
|
||||
}
|
||||
}
|
||||
|
||||
// Update camera based on arrow keys (camera orbits player)
|
||||
// Orbit camera around the player using arrow keys
|
||||
if rl.IsKeyDown(rl.KeyRight) {
|
||||
cameraYaw -= 60 * deltaTime // Rotate right
|
||||
cameraYaw += 100 * deltaTime
|
||||
}
|
||||
if rl.IsKeyDown(rl.KeyLeft) {
|
||||
cameraYaw += 60 * deltaTime // Rotate left
|
||||
cameraYaw -= 100 * deltaTime
|
||||
}
|
||||
if rl.IsKeyDown(rl.KeyUp) {
|
||||
cameraPitch += 60 * deltaTime // Tilt up
|
||||
cameraPitch -= 50 * deltaTime
|
||||
if cameraPitch < 20 {
|
||||
cameraPitch = 20
|
||||
}
|
||||
}
|
||||
if rl.IsKeyDown(rl.KeyDown) {
|
||||
cameraPitch -= 60 * deltaTime // Tilt down
|
||||
cameraPitch += 50 * deltaTime
|
||||
if cameraPitch > 85 {
|
||||
cameraPitch = 85
|
||||
}
|
||||
}
|
||||
|
||||
// Constrain camera pitch to avoid flipping
|
||||
if cameraPitch > 85.0 {
|
||||
cameraPitch = 85.0
|
||||
} else if cameraPitch < -10.0 {
|
||||
cameraPitch = -10.0
|
||||
// Calculate the new camera position using spherical coordinates
|
||||
cameraYawRad := float64(cameraYaw) * rl.Deg2rad
|
||||
cameraPitchRad := float64(cameraPitch) * rl.Deg2rad
|
||||
cameraPos := rl.Vector3{
|
||||
X: player.X + cameraDistance*float32(math.Cos(cameraYawRad))*float32(math.Cos(cameraPitchRad)),
|
||||
Y: player.Y + cameraDistance*float32(math.Sin(cameraPitchRad)),
|
||||
Z: player.Z + cameraDistance*float32(math.Sin(cameraYawRad))*float32(math.Cos(cameraPitchRad)),
|
||||
}
|
||||
|
||||
// Update the camera's position and target
|
||||
camera.Position = cameraPos
|
||||
camera.Target = rl.NewVector3(player.X, player.Y, player.Z)
|
||||
}
|
||||
|
||||
func main() {
|
||||
rl.InitWindow(800, 600, "goonscape")
|
||||
defer rl.CloseWindow()
|
||||
|
||||
mapGrid := InitMap()
|
||||
|
||||
player := Player{
|
||||
PosActual: rl.NewVector3(5*TileSize, 0, 5*TileSize),
|
||||
PosTile: mapGrid[5][5],
|
||||
Speed: 50.0,
|
||||
TargetPath: []Tile{},
|
||||
}
|
||||
|
||||
camera := rl.Camera3D{
|
||||
Position: rl.NewVector3(0, 10, 10), // Will be updated every frame
|
||||
Target: player.PosActual,
|
||||
Up: rl.NewVector3(0, 1, 0), // Y is up in 3D
|
||||
Fovy: 45.0,
|
||||
Projection: rl.CameraPerspective,
|
||||
}
|
||||
|
||||
rl.SetTargetFPS(60)
|
||||
|
||||
for !rl.WindowShouldClose() {
|
||||
// Time management
|
||||
deltaTime := rl.GetFrameTime()
|
||||
|
||||
// Handle input
|
||||
HandleInput(&player, mapGrid, &camera)
|
||||
UpdatePlayer(&player, deltaTime, mapGrid, &camera)
|
||||
|
||||
camera.Target = player.PosActual
|
||||
// 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.Y = player.PosActual.Y + cameraDistance*float32(math.Sin(float64(cameraPitch)*math.Pi/180.0))
|
||||
camera.Position.Z = player.PosActual.Z + cameraDistance*float32(math.Cos(float64(cameraPitch)*math.Pi/180.0))*float32(math.Cos(float64(cameraYaw)*math.Pi/180.0))
|
||||
// Update player
|
||||
if len(player.TargetPath) > 0 {
|
||||
player.MoveTowards(player.TargetPath[0], deltaTime, mapGrid)
|
||||
}
|
||||
|
||||
// Begin rendering
|
||||
// Update camera
|
||||
UpdateCamera(&camera, player.PosActual, deltaTime)
|
||||
|
||||
// Rendering
|
||||
rl.BeginDrawing()
|
||||
rl.ClearBackground(rl.RayWhite)
|
||||
rl.BeginMode3D(camera)
|
||||
|
||||
// Draw map and player
|
||||
DrawMap(mapGrid)
|
||||
DrawPlayer(player, mapGrid)
|
||||
|
||||
rl.EndMode3D()
|
||||
rl.EndDrawing()
|
||||
|
||||
if frameCnt == 0 {
|
||||
fmt.Printf("Player Pos: %v, Camera Pos: %v\n", player.PosActual, camera.Position)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user