2015-09-02 03:41:21 +03:00
|
|
|
/*******************************************************************************************
|
|
|
|
*
|
|
|
|
* 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)
|
|
|
|
*
|
|
|
|
********************************************************************************************/
|
|
|
|
|
|
|
|
#include "raylib.h"
|
|
|
|
|
2019-05-20 17:36:42 +03:00
|
|
|
int main(void)
|
2015-09-02 03:41:21 +03:00
|
|
|
{
|
|
|
|
// Initialization
|
|
|
|
//--------------------------------------------------------------------------------------
|
2019-05-20 17:36:42 +03:00
|
|
|
const int screenWidth = 800;
|
|
|
|
const int screenHeight = 450;
|
2015-09-02 03:41:21 +03:00
|
|
|
|
|
|
|
InitWindow(screenWidth, screenHeight, "raylib [models] example - box collisions");
|
|
|
|
|
|
|
|
// Define the camera to look into our 3d world
|
2019-05-27 01:18:15 +03:00
|
|
|
Camera camera = { { 0.0f, 10.0f, 10.0f }, { 0.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f, 0 };
|
2019-05-20 17:36:42 +03:00
|
|
|
|
2016-01-16 14:52:55 +03:00
|
|
|
Vector3 playerPosition = { 0.0f, 1.0f, 2.0f };
|
|
|
|
Vector3 playerSize = { 1.0f, 2.0f, 1.0f };
|
2015-09-02 03:41:21 +03:00
|
|
|
Color playerColor = GREEN;
|
2019-05-20 17:36:42 +03:00
|
|
|
|
2016-01-16 14:52:55 +03:00
|
|
|
Vector3 enemyBoxPos = { -4.0f, 1.0f, 0.0f };
|
|
|
|
Vector3 enemyBoxSize = { 2.0f, 2.0f, 2.0f };
|
2019-05-20 17:36:42 +03:00
|
|
|
|
2016-01-16 14:52:55 +03:00
|
|
|
Vector3 enemySpherePos = { 4.0f, 0.0f, 0.0f };
|
2015-09-02 03:41:21 +03:00
|
|
|
float enemySphereSize = 1.5f;
|
2019-05-20 17:36:42 +03:00
|
|
|
|
2015-09-02 03:41:21 +03:00
|
|
|
bool collision = false;
|
|
|
|
|
2019-05-20 17:36:42 +03:00
|
|
|
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
|
2015-09-02 03:41:21 +03:00
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Main game loop
|
|
|
|
while (!WindowShouldClose()) // Detect window close button or ESC key
|
|
|
|
{
|
|
|
|
// Update
|
|
|
|
//----------------------------------------------------------------------------------
|
2019-05-20 17:36:42 +03:00
|
|
|
|
2015-09-02 03:41:21 +03:00
|
|
|
// Move player
|
|
|
|
if (IsKeyDown(KEY_RIGHT)) playerPosition.x += 0.2f;
|
|
|
|
else if (IsKeyDown(KEY_LEFT)) playerPosition.x -= 0.2f;
|
|
|
|
else if (IsKeyDown(KEY_DOWN)) playerPosition.z += 0.2f;
|
|
|
|
else if (IsKeyDown(KEY_UP)) playerPosition.z -= 0.2f;
|
2019-05-20 17:36:42 +03:00
|
|
|
|
2015-09-02 03:41:21 +03:00
|
|
|
collision = false;
|
2019-05-20 17:36:42 +03:00
|
|
|
|
2015-09-02 03:41:21 +03:00
|
|
|
// Check collisions player vs enemy-box
|
2016-03-01 22:54:02 +03:00
|
|
|
if (CheckCollisionBoxes(
|
2019-05-20 17:36:42 +03:00
|
|
|
(BoundingBox){(Vector3){ playerPosition.x - playerSize.x/2,
|
|
|
|
playerPosition.y - playerSize.y/2,
|
|
|
|
playerPosition.z - playerSize.z/2 },
|
2016-03-01 22:54:02 +03:00
|
|
|
(Vector3){ playerPosition.x + playerSize.x/2,
|
2019-05-20 17:36:42 +03:00
|
|
|
playerPosition.y + playerSize.y/2,
|
2016-03-01 22:54:02 +03:00
|
|
|
playerPosition.z + playerSize.z/2 }},
|
2019-05-20 17:36:42 +03:00
|
|
|
(BoundingBox){(Vector3){ enemyBoxPos.x - enemyBoxSize.x/2,
|
|
|
|
enemyBoxPos.y - enemyBoxSize.y/2,
|
|
|
|
enemyBoxPos.z - enemyBoxSize.z/2 },
|
2016-03-01 22:54:02 +03:00
|
|
|
(Vector3){ enemyBoxPos.x + enemyBoxSize.x/2,
|
2019-05-20 17:36:42 +03:00
|
|
|
enemyBoxPos.y + enemyBoxSize.y/2,
|
2016-03-01 22:54:02 +03:00
|
|
|
enemyBoxPos.z + enemyBoxSize.z/2 }})) collision = true;
|
2019-05-20 17:36:42 +03:00
|
|
|
|
2015-09-02 03:41:21 +03:00
|
|
|
// Check collisions player vs enemy-sphere
|
2016-03-01 22:54:02 +03:00
|
|
|
if (CheckCollisionBoxSphere(
|
2019-05-20 17:36:42 +03:00
|
|
|
(BoundingBox){(Vector3){ playerPosition.x - playerSize.x/2,
|
|
|
|
playerPosition.y - playerSize.y/2,
|
|
|
|
playerPosition.z - playerSize.z/2 },
|
2016-03-01 22:54:02 +03:00
|
|
|
(Vector3){ playerPosition.x + playerSize.x/2,
|
2019-05-20 17:36:42 +03:00
|
|
|
playerPosition.y + playerSize.y/2,
|
|
|
|
playerPosition.z + playerSize.z/2 }},
|
2016-03-01 22:54:02 +03:00
|
|
|
enemySpherePos, enemySphereSize)) collision = true;
|
2019-05-20 17:36:42 +03:00
|
|
|
|
2015-09-02 03:41:21 +03:00
|
|
|
if (collision) playerColor = RED;
|
|
|
|
else playerColor = GREEN;
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Draw
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
BeginDrawing();
|
|
|
|
|
|
|
|
ClearBackground(RAYWHITE);
|
|
|
|
|
2018-05-04 17:54:05 +03:00
|
|
|
BeginMode3D(camera);
|
2015-09-02 03:41:21 +03:00
|
|
|
|
|
|
|
// Draw enemy-box
|
|
|
|
DrawCube(enemyBoxPos, enemyBoxSize.x, enemyBoxSize.y, enemyBoxSize.z, GRAY);
|
|
|
|
DrawCubeWires(enemyBoxPos, enemyBoxSize.x, enemyBoxSize.y, enemyBoxSize.z, DARKGRAY);
|
2019-05-20 17:36:42 +03:00
|
|
|
|
2015-09-02 03:41:21 +03:00
|
|
|
// Draw enemy-sphere
|
|
|
|
DrawSphere(enemySpherePos, enemySphereSize, GRAY);
|
|
|
|
DrawSphereWires(enemySpherePos, enemySphereSize, 16, 16, DARKGRAY);
|
2019-05-20 17:36:42 +03:00
|
|
|
|
2015-09-02 03:41:21 +03:00
|
|
|
// Draw player
|
|
|
|
DrawCubeV(playerPosition, playerSize, playerColor);
|
|
|
|
|
2016-01-16 14:52:55 +03:00
|
|
|
DrawGrid(10, 1.0f); // Draw a grid
|
2015-09-02 03:41:21 +03:00
|
|
|
|
2018-05-04 17:54:05 +03:00
|
|
|
EndMode3D();
|
2019-05-20 17:36:42 +03:00
|
|
|
|
2015-09-02 03:41:21 +03:00
|
|
|
DrawText("Move player with cursors to collide", 220, 40, 20, GRAY);
|
|
|
|
|
|
|
|
DrawFPS(10, 10);
|
|
|
|
|
|
|
|
EndDrawing();
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
}
|
|
|
|
|
|
|
|
// De-Initialization
|
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
CloseWindow(); // Close window and OpenGL context
|
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|