forked from gen2brain/raylib-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
110 lines (82 loc) · 2.88 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package main
import (
"github.com/gen2brain/raylib-go/raylib"
)
func main() {
screenWidth := int32(800)
screenHeight := int32(450)
rl.InitWindow(screenWidth, screenHeight, "raylib [models] example - box collisions")
camera := rl.Camera{}
camera.Position = rl.NewVector3(0.0, 10.0, 10.0)
camera.Target = rl.NewVector3(0.0, 0.0, 0.0)
camera.Up = rl.NewVector3(0.0, 1.0, 0.0)
camera.Fovy = 45.0
camera.Type = rl.CameraPerspective
playerPosition := rl.NewVector3(0.0, 1.0, 2.0)
playerSize := rl.NewVector3(1.0, 2.0, 1.0)
playerColor := rl.Green
enemyBoxPos := rl.NewVector3(-4.0, 1.0, 0.0)
enemyBoxSize := rl.NewVector3(2.0, 2.0, 2.0)
enemySpherePos := rl.NewVector3(4.0, 0.0, 0.0)
enemySphereSize := float32(1.5)
collision := false
rl.SetTargetFPS(60)
for !rl.WindowShouldClose() {
// Update
// Move player
if rl.IsKeyDown(rl.KeyRight) {
playerPosition.X += 0.2
} else if rl.IsKeyDown(rl.KeyLeft) {
playerPosition.X -= 0.2
} else if rl.IsKeyDown(rl.KeyDown) {
playerPosition.Z += 0.2
} else if rl.IsKeyDown(rl.KeyUp) {
playerPosition.Z -= 0.2
}
collision = false
// Check collisions player vs enemy-box
if rl.CheckCollisionBoxes(
rl.NewBoundingBox(
rl.NewVector3(playerPosition.X-playerSize.X/2, playerPosition.Y-playerSize.Y/2, playerPosition.Z-playerSize.Z/2),
rl.NewVector3(playerPosition.X+playerSize.X/2, playerPosition.Y+playerSize.Y/2, playerPosition.Z+playerSize.Z/2)),
rl.NewBoundingBox(
rl.NewVector3(enemyBoxPos.X-enemyBoxSize.X/2, enemyBoxPos.Y-enemyBoxSize.Y/2, enemyBoxPos.Z-enemyBoxSize.Z/2),
rl.NewVector3(enemyBoxPos.X+enemyBoxSize.X/2, enemyBoxPos.Y+enemyBoxSize.Y/2, enemyBoxPos.Z+enemyBoxSize.Z/2)),
) {
collision = true
}
// Check collisions player vs enemy-sphere
if rl.CheckCollisionBoxSphere(
rl.NewBoundingBox(
rl.NewVector3(playerPosition.X-playerSize.X/2, playerPosition.Y-playerSize.Y/2, playerPosition.Z-playerSize.Z/2),
rl.NewVector3(playerPosition.X+playerSize.X/2, playerPosition.Y+playerSize.Y/2, playerPosition.Z+playerSize.Z/2)),
enemySpherePos,
enemySphereSize,
) {
collision = true
}
if collision {
playerColor = rl.Red
} else {
playerColor = rl.Green
}
// Draw
rl.BeginDrawing()
rl.ClearBackground(rl.RayWhite)
rl.BeginMode3D(camera)
// Draw enemy-box
rl.DrawCube(enemyBoxPos, enemyBoxSize.X, enemyBoxSize.Y, enemyBoxSize.Z, rl.Gray)
rl.DrawCubeWires(enemyBoxPos, enemyBoxSize.X, enemyBoxSize.Y, enemyBoxSize.Z, rl.DarkGray)
// Draw enemy-sphere
rl.DrawSphere(enemySpherePos, enemySphereSize, rl.Gray)
rl.DrawSphereWires(enemySpherePos, enemySphereSize, 16, 16, rl.DarkGray)
// Draw player
rl.DrawCubeV(playerPosition, playerSize, playerColor)
rl.DrawGrid(10, 1.0) // Draw a grid
rl.EndMode3D()
rl.DrawText("Move player with cursors to collide", 220, 40, 20, rl.Gray)
rl.DrawFPS(10, 10)
rl.EndDrawing()
}
rl.CloseWindow()
}