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