This repository was archived by the owner on Dec 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels_box_collisions.php
More file actions
116 lines (89 loc) · 4.66 KB
/
models_box_collisions.php
File metadata and controls
116 lines (89 loc) · 4.66 KB
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
/*******************************************************************************************
*
* raylib [models] example - Detect basic 3d collisions (box vs sphere vs box)
*
* This example has been created using raylib 1.3 (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
*
* Copyright (c) 2015 Ramon Santamaria (@raysan5)
*
********************************************************************************************/
<?php
// Initialization
//--------------------------------------------------------------------------------------
$screenWidth = 800;
$screenHeight = 450;
InitWindow($screenWidth, $screenHeight, "raylib [models] example - box collisions");
// Define the camera to look into our 3d world
$camera = Camera3D(Vector3(0.0, 10.0, 10.0), Vector3(0.0, 0.0, 0.0), Vector3(0.0, 1.0, 0.0), 45.0, 0);
$playerPosition = Vector3(0.0, 1.0, 2.0);
$playerSize = Vector3(1.0, 2.0, 1.0);
$playerColor = GREEN();
$enemyBoxPos = Vector3(-4.0, 1.0, 0.0 );
$enemyBoxSize = Vector3(2.0, 2.0, 2.0 );
$enemySpherePos = Vector3(4.0, 0.0, 0.0 );
$enemySphereSize = 1.5;
$collision = false;
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(RL_KEY_RIGHT)) $playerPosition->x = $playerPosition->x + 0.2;
else if (IsKeyDown(RL_KEY_LEFT)) $playerPosition->x =$playerPosition->x - 0.2;
else if (IsKeyDown(RL_KEY_DOWN)) $playerPosition->z = $playerPosition->z + 0.2;
else if (IsKeyDown(RL_KEY_UP)) $playerPosition->z = $playerPosition->z - 0.2;
$collision = false;
// Check collisions player vs enemy-box
if (CheckCollisionBoxes(
BoundingBox(Vector3($playerPosition->x - $playerSize->x/2,
$playerPosition->y - $playerSize->y/2,
$playerPosition->z - $playerSize->z/2 ),
Vector3($playerPosition->x + $playerSize->x/2,
$playerPosition->y + $playerSize->y/2,
$playerPosition->z + $playerSize->z/2 )),
BoundingBox(Vector3($enemyBoxPos->x - $enemyBoxSize->x/2,
$enemyBoxPos->y - $enemyBoxSize->y/2,
$enemyBoxPos->z - $enemyBoxSize->z/2 ),
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(
BoundingBox(Vector3($playerPosition->x - $playerSize->x/2,
$playerPosition->y - $playerSize->y/2,
$playerPosition->z - $playerSize->z/2),
Vector3($playerPosition->x + $playerSize->x/2,
$playerPosition->y + $playerSize->y/2,
$playerPosition->z + $playerSize->z/2)),
$enemySpherePos, $enemySphereSize)) $collision = true;
if ($collision) $playerColor = RED();
else $playerColor = GREEN();
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(RAYWHITE());
BeginMode3D($camera);
// Draw enemy-box
DrawCube($enemyBoxPos, $enemyBoxSize->x, $enemyBoxSize->y, $enemyBoxSize->z, GRAY());
DrawCubeWires($enemyBoxPos, $enemyBoxSize->x, $enemyBoxSize->y, $enemyBoxSize->z, DARKGRAY());
// Draw enemy-sphere
DrawSphere($enemySpherePos, $enemySphereSize, GRAY());
DrawSphereWires($enemySpherePos, $enemySphereSize, 16, 16, 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, GRAY());
DrawFPS(10, 10);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------