-
Notifications
You must be signed in to change notification settings - Fork 1
/
models_box_collisions.php
160 lines (138 loc) · 4.96 KB
/
models_box_collisions.php
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
declare(strict_types=1);
use Nawarian\Raylib\Raylib;
use Nawarian\Raylib\Types\{BoundingBox, Camera3D, Color, Vector3};
use function Nawarian\Raylib\{
BeginDrawing,
BeginMode3D,
CheckCollisionBoxes,
CheckCollisionBoxSphere,
ClearBackground,
CloseWindow,
DrawCube,
DrawCubeV,
DrawCubeWires,
DrawFPS,
DrawGrid,
DrawSphere,
DrawSphereWires,
DrawText,
EndDrawing,
EndMode3D,
InitWindow,
IsKeyDown,
SetTargetFPS,
WindowShouldClose
};
require_once __DIR__ . '/../../vendor/autoload.php';
// Initialization
//--------------------------------------------------------------------------------------
$screenWidth = 800;
$screenHeight = 450;
InitWindow($screenWidth, $screenHeight, 'raylib [models] example - box collisions');
// Define the camera to look into our 3d world
$camera = new Camera3D(
new Vector3(0, 10, 10),
new Vector3(0, 0, 0),
new Vector3(0, 1, 0),
45,
Camera3D::PROJECTION_PERSPECTIVE,
);
$playerPosition = new Vector3(0, 1, 2);
$playerSize = new Vector3(1, 2, 1);
$enemyBoxPos = new Vector3(-4, 1, 0);
$enemyBoxSize = new Vector3(2, 2, 2);
$enemySpherePos = new Vector3(4, 0, 0);
$enemySphereSize = 1.5;
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose()) { // Detect window close button or ESC key
// Update
//----------------------------------------------------------------------------------
// Move player
if (IsKeyDown(Raylib::KEY_RIGHT)) {
$playerPosition->x += 0.2;
} elseif (IsKeyDown(Raylib::KEY_LEFT)) {
$playerPosition->x -= 0.2;
} elseif (IsKeyDown(Raylib::KEY_DOWN)) {
$playerPosition->z += 0.2;
} elseif (IsKeyDown(Raylib::KEY_UP)) {
$playerPosition->z -= 0.2;
}
$collision = false;
// Check collisions player vs enemy-box
if (
CheckCollisionBoxes(new BoundingBox(
new Vector3(
$playerPosition->x - $playerSize->x / 2,
$playerPosition->y - $playerSize->y / 2,
$playerPosition->z - $playerSize->z / 2,
),
new Vector3(
$playerPosition->x + $playerSize->x / 2,
$playerPosition->y + $playerSize->y / 2,
$playerPosition->z + $playerSize->z / 2,
),
), new BoundingBox(
new Vector3(
$enemyBoxPos->x - $enemyBoxSize->x / 2,
$enemyBoxPos->y - $enemyBoxSize->y / 2,
$enemyBoxPos->z - $enemyBoxSize->z / 2,
),
new Vector3(
$enemyBoxPos->x + $enemyBoxSize->x / 2,
$enemyBoxPos->y + $enemyBoxSize->y / 2,
$enemyBoxPos->z + $enemyBoxSize->z / 2,
),
))
) {
$collision = true;
}
// Check collisions player vs enemy-sphere
if (
CheckCollisionBoxSphere(new BoundingBox(
new Vector3(
$playerPosition->x - $playerSize->x / 2,
$playerPosition->y - $playerSize->y / 2,
$playerPosition->z - $playerSize->z / 2,
),
new Vector3(
$playerPosition->x + $playerSize->x / 2,
$playerPosition->y + $playerSize->y / 2,
$playerPosition->z + $playerSize->z / 2
),
), $enemySpherePos, $enemySphereSize)
) {
$collision = true;
}
if ($collision) {
$playerColor = Color::red();
} else {
$playerColor = Color::green();
}
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(Color::rayWhite());
BeginMode3D($camera);
// Draw enemy-box
DrawCube($enemyBoxPos, $enemyBoxSize->x, $enemyBoxSize->y, $enemyBoxSize->z, Color::gray());
DrawCubeWires($enemyBoxPos, $enemyBoxSize->x, $enemyBoxSize->y, $enemyBoxSize->z, Color::darkGray());
// Draw enemy-sphere
DrawSphere($enemySpherePos, $enemySphereSize, Color::gray());
DrawSphereWires($enemySpherePos, $enemySphereSize, 16, 16, Color::darkGray());
// Draw player
DrawCubeV($playerPosition, $playerSize, $playerColor);
DrawGrid(10, 1.0); // Draw a grid
EndMode3D();
DrawText('Move player with cursors to collide', 220, 40, 20, Color::gray());
DrawFPS(10, 10);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------