Compare commits
2 Commits
a9c6298da8
...
6c8993981c
Author | SHA1 | Date | |
---|---|---|---|
6c8993981c | |||
92b0b13add |
78
main.go
78
main.go
@ -108,35 +108,69 @@ func DrawPlayer(player Player, model *rl.Model, mapGrid [][]Tile) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Helper function to test ray-box intersection (slab method)
|
||||||
|
func RayIntersectsBox(ray rl.Ray, boxMin, boxMax rl.Vector3) bool {
|
||||||
|
tmin := (boxMin.X - ray.Position.X) / ray.Direction.X
|
||||||
|
tmax := (boxMax.X - ray.Position.X) / ray.Direction.X
|
||||||
|
|
||||||
|
if tmin > tmax {
|
||||||
|
tmin, tmax = tmax, tmin
|
||||||
|
}
|
||||||
|
|
||||||
|
tymin := (boxMin.Z - ray.Position.Z) / ray.Direction.Z
|
||||||
|
tymax := (boxMax.Z - ray.Position.Z) / ray.Direction.Z
|
||||||
|
|
||||||
|
if tymin > tymax {
|
||||||
|
tymin, tymax = tymax, tymin
|
||||||
|
}
|
||||||
|
|
||||||
|
if (tmin > tymax) || (tymin > tmax) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
if tymin > tmin {
|
||||||
|
tmin = tymin
|
||||||
|
}
|
||||||
|
if tymax < tmax {
|
||||||
|
tmax = tymax
|
||||||
|
}
|
||||||
|
|
||||||
|
tzmin := (boxMin.Y - ray.Position.Y) / ray.Direction.Y
|
||||||
|
tzmax := (boxMax.Y - ray.Position.Y) / ray.Direction.Y
|
||||||
|
|
||||||
|
if tzmin > tzmax {
|
||||||
|
tzmin, tzmax = tzmax, tzmin
|
||||||
|
}
|
||||||
|
|
||||||
|
if (tmin > tzmax) || (tzmin > tmax) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
func GetTileAtMouse(mapGrid [][]Tile, camera *rl.Camera3D) (Tile, bool) {
|
func GetTileAtMouse(mapGrid [][]Tile, camera *rl.Camera3D) (Tile, bool) {
|
||||||
if !rl.IsMouseButtonPressed(rl.MouseLeftButton) {
|
if !rl.IsMouseButtonPressed(rl.MouseLeftButton) {
|
||||||
return Tile{}, false
|
return Tile{}, false
|
||||||
}
|
}
|
||||||
mouse := rl.GetMousePosition()
|
mouse := rl.GetMousePosition()
|
||||||
|
|
||||||
// Unproject mouse coordinates to 3D ray
|
|
||||||
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)
|
for x := 0; x < MapWidth; x++ {
|
||||||
if ray.Direction.Y == 0 {
|
for y := 0; y < MapHeight; y++ {
|
||||||
return Tile{}, false
|
tile := mapGrid[x][y]
|
||||||
|
|
||||||
|
// Define the bounding box for each tile based on its position and height
|
||||||
|
tilePos := rl.NewVector3(float32(x*TileSize), tile.Height*TileHeight, float32(y*TileSize))
|
||||||
|
boxMin := rl.Vector3Subtract(tilePos, rl.NewVector3(TileSize/2, TileHeight/2, TileSize/2))
|
||||||
|
boxMax := rl.Vector3Add(tilePos, rl.NewVector3(TileSize/2, TileHeight/2, TileSize/2))
|
||||||
|
|
||||||
|
// Check if the ray intersects the bounding box
|
||||||
|
if RayIntersectsBox(ray, boxMin, boxMax) {
|
||||||
|
fmt.Println("Clicked:", tile.X, tile.Y)
|
||||||
|
return tile, true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
dist := -ray.Position.Y / ray.Direction.Y
|
|
||||||
|
|
||||||
// Calculate the intersection point
|
|
||||||
intersection := rl.NewVector3(
|
|
||||||
ray.Position.X+ray.Direction.X*dist,
|
|
||||||
ray.Position.Y+ray.Direction.Y*dist,
|
|
||||||
ray.Position.Z+ray.Direction.Z*dist,
|
|
||||||
)
|
|
||||||
|
|
||||||
// Convert the intersection point to tile coordinates
|
|
||||||
tileX := int(intersection.X / float32(TileSize))
|
|
||||||
tileY := int(intersection.Z / float32(TileSize))
|
|
||||||
|
|
||||||
if tileX >= 0 && tileX < MapWidth && tileY >= 0 && tileY < MapHeight {
|
|
||||||
fmt.Println("Clicked:", tileX, tileY)
|
|
||||||
return mapGrid[tileX][tileY], true
|
|
||||||
}
|
}
|
||||||
return Tile{}, false
|
return Tile{}, false
|
||||||
}
|
}
|
||||||
@ -228,7 +262,7 @@ func UpdateCamera(camera *rl.Camera3D, player rl.Vector3, deltaTime float32) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
rl.InitWindow(800, 600, "GoonScape")
|
rl.InitWindow(1024, 768, "GoonScape")
|
||||||
defer rl.CloseWindow()
|
defer rl.CloseWindow()
|
||||||
rl.InitAudioDevice()
|
rl.InitAudioDevice()
|
||||||
defer rl.CloseAudioDevice()
|
defer rl.CloseAudioDevice()
|
||||||
|
BIN
output.mp4
Normal file
BIN
output.mp4
Normal file
Binary file not shown.
@ -1,7 +1,7 @@
|
|||||||
# Blender 3.6.0
|
# Blender 3.6.0
|
||||||
# www.blender.org
|
# www.blender.org
|
||||||
mtllib Coomer_one_exaggerat_1003075737.mtl
|
mtllib coomer.mtl
|
||||||
o Coomer_one_exaggerat_1003075737
|
o coomer
|
||||||
v 0.319145 -0.929950 0.071755
|
v 0.319145 -0.929950 0.071755
|
||||||
v 0.322944 -0.934938 0.079493
|
v 0.322944 -0.934938 0.079493
|
||||||
v 0.329399 -0.938953 0.075593
|
v 0.329399 -0.938953 0.075593
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
# Blender 3.6.0
|
# Blender 3.6.0
|
||||||
# www.blender.org
|
# www.blender.org
|
||||||
mtllib Minion_full_body_A__1002112559.mtl
|
mtllib goonion.mtl
|
||||||
o Minion_full_body_A__1002112559
|
o goonion
|
||||||
v -0.441385 -0.290769 -0.044101
|
v -0.441385 -0.290769 -0.044101
|
||||||
v -0.448696 -0.291602 -0.052330
|
v -0.448696 -0.291602 -0.052330
|
||||||
v -0.444993 -0.298477 -0.046398
|
v -0.444993 -0.298477 -0.046398
|
||||||
|
Loading…
x
Reference in New Issue
Block a user