Pārlūkot izejas kodu

Corrected camera system

pull/23/head
Marc Palau pirms 10 gadiem
vecāks
revīzija
3cb4edcbc3
3 mainītis faili ar 322 papildinājumiem un 282 dzēšanām
  1. +83
    -50
      src/core.c
  2. +236
    -231
      src/models.c
  3. +3
    -1
      src/raylib.h

+ 83
- 50
src/core.c Parādīt failu

@ -261,7 +261,6 @@ static Vector2 cameraMousePosition = { 0, 0 };
static Vector2 cameraMouseVariation = { 0, 0 };
static int cameraMovementCounter = 0;
static bool cameraUseGravity = true;
static Vector3 cameraPosition = { 2, 0, 2 }; // Player
// Shaders variables
static bool enabledPostpro = false;
@ -316,7 +315,7 @@ static int32_t InputCallback(struct android_app *app, AInputEvent *event); //
static void CommandCallback(struct android_app *app, int32_t cmd); // Process Android activity lifecycle commands
#endif
static void ProcessCamera(Camera *camera);
static void ProcessCamera(Camera *camera, Vector3 *playerPosition);
//----------------------------------------------------------------------------------
// Module Functions Definition - Window and OpenGL Context Functions
@ -535,10 +534,7 @@ void BeginDrawing(void)
currentTime = GetTime(); // Number of elapsed seconds since InitTimer() was called
updateTime = currentTime - previousTime;
previousTime = currentTime;
// Calculate camera
if (cameraMode != CAMERA_CUSTOM) ProcessCamera(&internalCamera);
if (enabledPostpro) rlEnableFBO();
rlClearScreenBuffers();
@ -707,9 +703,46 @@ void ShowLogo(void)
void SetCameraMode(int mode)
{
if ((cameraMode == CAMERA_FIRST_PERSON) && (mode == CAMERA_FREE))
{
cameraMode = CAMERA_THIRD_PERSON;
cameraTargetDistance = 5;
cameraAngle.y = -40 * DEG2RAD;
ProcessCamera(&internalCamera, &internalCamera.position);
}
else if ((cameraMode == CAMERA_FIRST_PERSON) && (mode == CAMERA_ORBITAL))
{
cameraMode = CAMERA_THIRD_PERSON;
cameraTargetDistance = 5;
cameraAngle.y = -40 * DEG2RAD;
ProcessCamera(&internalCamera, &internalCamera.position);
}
else if ((cameraMode == CAMERA_CUSTOM) && (mode == CAMERA_FREE))
{
cameraMode = CAMERA_THIRD_PERSON;
cameraTargetDistance = 5;
internalCamera.position = (Vector3){ -1, 1, -1 };
internalCamera.target = (Vector3){ 3, 0, 3};
ProcessCamera(&internalCamera, &internalCamera.position);
}
else if ((cameraMode == CAMERA_CUSTOM) && (mode == CAMERA_ORBITAL))
{
cameraMode = CAMERA_THIRD_PERSON;
cameraTargetDistance = 5;
cameraAngle.x = 45 * DEG2RAD;
cameraAngle.y = -20 * DEG2RAD;
ProcessCamera(&internalCamera, &internalCamera.position);
}
cameraMode = mode;
}
void UpdateCamera(Vector3 *playerPosition)
{
// Calculate camera
if (cameraMode != CAMERA_CUSTOM) ProcessCamera(&internalCamera, playerPosition);
}
//----------------------------------------------------------------------------------
// Module Functions Definition - Input (Keyboard, Mouse, Gamepad) Functions
//----------------------------------------------------------------------------------
@ -1026,6 +1059,32 @@ Vector2 GetTouchPosition(void)
}*/
#endif
// Initialize OpenGL graphics
void InitGraphics(void)
{
rlglInit(); // Init rlgl
rlglInitGraphics(renderOffsetX, renderOffsetY, renderWidth, renderHeight); // Init graphics (OpenGL stuff)
ClearBackground(RAYWHITE); // Default background color for raylib games :P
#if defined(PLATFORM_ANDROID)
windowReady = true; // IMPORTANT!
#endif
}
void InitPostShader(void)
{
rlglInitPostpro();
enabledPostpro = true;
}
void SetPostShader(unsigned int shader)
{
fboShader = shader;
}
//----------------------------------------------------------------------------------
// Module specific Functions Definition
//----------------------------------------------------------------------------------
@ -1287,32 +1346,6 @@ static void InitDisplay(int width, int height)
#endif
}
// Initialize OpenGL graphics
void InitGraphics(void)
{
rlglInit(); // Init rlgl
rlglInitGraphics(renderOffsetX, renderOffsetY, renderWidth, renderHeight); // Init graphics (OpenGL stuff)
ClearBackground(RAYWHITE); // Default background color for raylib games :P
#if defined(PLATFORM_ANDROID)
windowReady = true; // IMPORTANT!
#endif
}
void InitPostShader(void)
{
rlglInitPostpro();
enabledPostpro = true;
}
void SetPostShader(unsigned int shader)
{
fboShader = shader;
}
#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_WEB)
// GLFW3 Error Callback, runs on GLFW3 error
static void ErrorCallback(int error, const char *description)
@ -2299,8 +2332,8 @@ static void LogoAnimation(void)
}
// Process desired camera mode and controls
static void ProcessCamera(Camera *camera)
{
static void ProcessCamera(Camera *camera, Vector3 *playerPosition)
{
// Mouse movement detection
if (fullscreen)
{
@ -2408,16 +2441,16 @@ static void ProcessCamera(Camera *camera)
// Keyboard inputs
if (IsKeyDown('W'))
{
cameraPosition.x -= sin(cameraAngle.x) / PLAYER_MOVEMENT_DIVIDER;
cameraPosition.z -= cos(cameraAngle.x) / PLAYER_MOVEMENT_DIVIDER;
playerPosition->x -= sin(cameraAngle.x) / PLAYER_MOVEMENT_DIVIDER;
playerPosition->z -= cos(cameraAngle.x) / PLAYER_MOVEMENT_DIVIDER;
if (!cameraUseGravity) camera->position.y += sin(cameraAngle.y) / PLAYER_MOVEMENT_DIVIDER;
isMoving = true;
}
else if (IsKeyDown('S'))
{
cameraPosition.x += sin(cameraAngle.x) / PLAYER_MOVEMENT_DIVIDER;
cameraPosition.z += cos(cameraAngle.x) / PLAYER_MOVEMENT_DIVIDER;
playerPosition->x += sin(cameraAngle.x) / PLAYER_MOVEMENT_DIVIDER;
playerPosition->z += cos(cameraAngle.x) / PLAYER_MOVEMENT_DIVIDER;
if (!cameraUseGravity) camera->position.y -= sin(cameraAngle.y) / PLAYER_MOVEMENT_DIVIDER;
isMoving = true;
@ -2425,26 +2458,26 @@ static void ProcessCamera(Camera *camera)
if (IsKeyDown('A'))
{
cameraPosition.x -= cos(cameraAngle.x) / PLAYER_MOVEMENT_DIVIDER;
cameraPosition.z += sin(cameraAngle.x) / PLAYER_MOVEMENT_DIVIDER;
playerPosition->x -= cos(cameraAngle.x) / PLAYER_MOVEMENT_DIVIDER;
playerPosition->z += sin(cameraAngle.x) / PLAYER_MOVEMENT_DIVIDER;
isMoving = true;
}
else if (IsKeyDown('D'))
{
cameraPosition.x += cos(cameraAngle.x) / PLAYER_MOVEMENT_DIVIDER;
cameraPosition.z -= sin(cameraAngle.x) / PLAYER_MOVEMENT_DIVIDER;
playerPosition->x += cos(cameraAngle.x) / PLAYER_MOVEMENT_DIVIDER;
playerPosition->z -= sin(cameraAngle.x) / PLAYER_MOVEMENT_DIVIDER;
isMoving = true;
}
if (IsKeyDown('E'))
{
if (!cameraUseGravity) cameraPosition.y += 1 / PLAYER_MOVEMENT_DIVIDER;
if (!cameraUseGravity) playerPosition->y += 1 / PLAYER_MOVEMENT_DIVIDER;
}
else if (IsKeyDown('Q'))
{
if (!cameraUseGravity) cameraPosition.y -= 1 / PLAYER_MOVEMENT_DIVIDER;
if (!cameraUseGravity) playerPosition->y -= 1 / PLAYER_MOVEMENT_DIVIDER;
}
if (cameraMode == CAMERA_THIRD_PERSON)
@ -2465,9 +2498,9 @@ static void ProcessCamera(Camera *camera)
if (cameraTargetDistance < THIRD_PERSON_DISTANCE_CLAMP) cameraTargetDistance = THIRD_PERSON_DISTANCE_CLAMP;
// Camera is always looking at player
camera->target.x = cameraPosition.x + THIRD_PERSON_OFFSET.x * cos(cameraAngle.x) + THIRD_PERSON_OFFSET.z * sin(cameraAngle.x);
camera->target.y = cameraPosition.y + PLAYER_HEIGHT * FIRST_PERSON_HEIGHT_RELATIVE_EYES_POSITION + THIRD_PERSON_OFFSET.y;
camera->target.z = cameraPosition.z + THIRD_PERSON_OFFSET.z * sin(cameraAngle.x) - THIRD_PERSON_OFFSET.x * sin(cameraAngle.x);
camera->target.x = playerPosition->x + THIRD_PERSON_OFFSET.x * cos(cameraAngle.x) + THIRD_PERSON_OFFSET.z * sin(cameraAngle.x);
camera->target.y = playerPosition->y + PLAYER_HEIGHT * FIRST_PERSON_HEIGHT_RELATIVE_EYES_POSITION + THIRD_PERSON_OFFSET.y;
camera->target.z = playerPosition->z + THIRD_PERSON_OFFSET.z * sin(cameraAngle.x) - THIRD_PERSON_OFFSET.x * sin(cameraAngle.x);
// Camera position update
camera->position.x = sin(cameraAngle.x) * cameraTargetDistance * cos(cameraAngle.y) + camera->target.x;
@ -2495,9 +2528,9 @@ static void ProcessCamera(Camera *camera)
camera->target.y = camera->position.y + sin(cameraAngle.y) * FIRST_PERSON_FOCUS_DISTANCE;
camera->target.z = camera->position.z - cos(cameraAngle.x) * FIRST_PERSON_FOCUS_DISTANCE;
camera->position.x = cameraPosition.x;
camera->position.y = (cameraPosition.y + PLAYER_HEIGHT * FIRST_PERSON_HEIGHT_RELATIVE_EYES_POSITION) - sin(cameraMovementCounter / FIRST_PERSON_STEP_TRIGONOMETRIC_DIVIDER) / FIRST_PERSON_STEP_DIVIDER;
camera->position.z = cameraPosition.z;
camera->position.x = playerPosition->x;
camera->position.y = (playerPosition->y + PLAYER_HEIGHT * FIRST_PERSON_HEIGHT_RELATIVE_EYES_POSITION) - sin(cameraMovementCounter / FIRST_PERSON_STEP_TRIGONOMETRIC_DIVIDER) / FIRST_PERSON_STEP_DIVIDER;
camera->position.z = playerPosition->z;
camera->up.x = sin(cameraMovementCounter / (FIRST_PERSON_STEP_TRIGONOMETRIC_DIVIDER * 2)) / FIRST_PERSON_WAVING_DIVIDER;
camera->up.z = -sin(cameraMovementCounter / (FIRST_PERSON_STEP_TRIGONOMETRIC_DIVIDER * 2)) / FIRST_PERSON_WAVING_DIVIDER;

+ 236
- 231
src/models.c Parādīt failu

@ -1335,6 +1335,242 @@ void DrawBillboardRec(Camera camera, Texture2D texture, Rectangle sourceRec, Vec
rlDisableTexture();
}
bool CheckCollisionSpheres(Vector3 centerA, float radiusA, Vector3 centerB, float radiusB)
{
bool collision = false;
float dx = centerA.x - centerB.x; // X distance between centers
float dy = centerA.y - centerB.y; // Y distance between centers
float dz = centerA.z - centerB.z; // Y distance between centers
float distance = sqrt(dx*dx + dy*dy + dz*dz); // Distance between centers
if (distance <= (radiusA + radiusB)) collision = true;
return collision;
}
bool CheckCollisionBoxes(Vector3 minBBox1, Vector3 maxBBox1, Vector3 minBBox2, Vector3 maxBBox2)
{
/*
// Get min and max vertex to construct bounds (AABB)
Vector3 minVertex = tempVertices[0];
Vector3 maxVertex = tempVertices[0];
for (int i = 1; i < tempVertices.Count; i++)
{
minVertex = Vector3.Min(minVertex, tempVertices[i]);
maxVertex = Vector3.Max(maxVertex, tempVertices[i]);
}
bounds = new BoundingBox(minVertex, maxVertex);
*/
bool collision = true;
if ((maxBBox1.x >= minBBox2.x) && (minBBox1.x <= maxBBox2.x))
{
if ((maxBBox1.y < minBBox2.y) || (minBBox1.y > maxBBox2.y)) collision = false;
if ((maxBBox1.z < minBBox2.z) || (minBBox1.z > maxBBox2.z)) collision = false;
}
else collision = false;
return collision;
}
bool CheckCollisionBoxSphere(Vector3 minBBox, Vector3 maxBBox, Vector3 centerSphere, float radiusSphere)
{
bool collision = false;
if ((centerSphere.x - minBBox.x > radiusSphere) && (centerSphere.y - minBBox.y > radiusSphere) && (centerSphere.z - minBBox.z > radiusSphere) &&
(maxBBox.x - centerSphere.x > radiusSphere) && (maxBBox.y - centerSphere.y > radiusSphere) && (maxBBox.z - centerSphere.z > radiusSphere))
{
collision = true;
}
else
{
float dmin = 0;
if (centerSphere.x - minBBox.x <= radiusSphere)
dmin += (centerSphere.x - minBBox.x) * (centerSphere.x - minBBox.x);
else if (maxBBox.x - centerSphere.x <= radiusSphere)
dmin += (centerSphere.x - maxBBox.x) * (centerSphere.x - maxBBox.x);
if (centerSphere.y - minBBox.y <= radiusSphere)
dmin += (centerSphere.y - minBBox.y) * (centerSphere.y - minBBox.y);
else if (maxBBox.y - centerSphere.y <= radiusSphere)
dmin += (centerSphere.y - maxBBox.y) * (centerSphere.y - maxBBox.y);
if (centerSphere.z - minBBox.z <= radiusSphere)
dmin += (centerSphere.z - minBBox.z) * (centerSphere.z - minBBox.z);
else if (maxBBox.z - centerSphere.z <= radiusSphere)
dmin += (centerSphere.z - maxBBox.z) * (centerSphere.z - maxBBox.z);
if (dmin <= radiusSphere * radiusSphere) collision = true;
}
return collision;
}
// TODO
//BoundingBox GetCollisionArea(BoundingBox box1, BoundingBox box2)
// Detect and resolve cubicmap collisions
// NOTE: player position (or camera) is modified inside this function
void ResolveCollisionCubicmap(Image cubicmap, Vector3 mapPosition, Vector3 *playerPosition)
{
// Detect the cell where the player is located
int locationCellX = 0;
int locationCellY = 0;
locationCellX = floor(playerPosition->x + mapPosition.x + 0.5);
locationCellY = floor(playerPosition->z + mapPosition.z + 0.5);
// Multiple Axis --------------------------------------------------------------------------------------------
// Axis x-, y-
if ((cubicmap.pixels[locationCellY * cubicmap.width + (locationCellX - 1)].r != 0) &&
(cubicmap.pixels[(locationCellY - 1) * cubicmap.width + (locationCellX)].r != 0))
{
if (((playerPosition->x + 0.5f) - locationCellX < 0.3) &&
((playerPosition->z + 0.5f) - locationCellY < 0.3))
{
playerPosition->x = locationCellX - 0.2;
playerPosition->z = locationCellY - 0.2;
}
}
// Axis x-, y+
if ((cubicmap.pixels[locationCellY * cubicmap.width + (locationCellX - 1)].r != 0) &&
(cubicmap.pixels[(locationCellY + 1) * cubicmap.width + (locationCellX)].r != 0))
{
if (((playerPosition->x + 0.5f) - locationCellX < 0.3) &&
((playerPosition->z + 0.5f) - locationCellY > 0.7))
{
playerPosition->x = locationCellX - 0.2;
playerPosition->z = locationCellY + 0.2;
}
}
// Axis x+, y-
if ((cubicmap.pixels[locationCellY * cubicmap.width + (locationCellX + 1)].r != 0) &&
(cubicmap.pixels[(locationCellY - 1) * cubicmap.width + (locationCellX)].r != 0))
{
if (((playerPosition->x + 0.5f) - locationCellX > 0.7) &&
((playerPosition->z + 0.5f) - locationCellY < 0.3))
{
playerPosition->x = locationCellX + 0.2;
playerPosition->z = locationCellY - 0.2;
}
}
// Axis x+, y+
if ((cubicmap.pixels[locationCellY * cubicmap.width + (locationCellX + 1)].r != 0) &&
(cubicmap.pixels[(locationCellY + 1) * cubicmap.width + (locationCellX)].r != 0))
{
if (((playerPosition->x + 0.5f) - locationCellX > 0.7) &&
((playerPosition->z + 0.5f) - locationCellY > 0.7))
{
playerPosition->x = locationCellX + 0.2f;
playerPosition->z = locationCellY + 0.2f;
}
}
// Single Axis ---------------------------------------------------------------------------------------------------
// Axis x-
if (cubicmap.pixels[locationCellY * cubicmap.width + (locationCellX - 1)].r != 0)
{
if ((playerPosition->x + 0.5f) - locationCellX < 0.3)
{
playerPosition->x = locationCellX - 0.2;
}
}
// Axis x+
if (cubicmap.pixels[locationCellY * cubicmap.width + (locationCellX + 1)].r != 0)
{
if ((playerPosition->x + 0.5f) - locationCellX > 0.7)
{
playerPosition->x = locationCellX + 0.2;
}
}
// Axis y-
if (cubicmap.pixels[(locationCellY - 1) * cubicmap.width + (locationCellX)].r != 0)
{
if ((playerPosition->z + 0.5f) - locationCellY < 0.3)
{
playerPosition->z = locationCellY - 0.2;
}
}
// Axis y+
if (cubicmap.pixels[(locationCellY + 1) * cubicmap.width + (locationCellX)].r != 0)
{
if ((playerPosition->z + 0.5f) - locationCellY > 0.7)
{
playerPosition->z = locationCellY + 0.2;
}
}
// Diagonals -------------------------------------------------------------------------------------------------------
// Axis x-, y-
if ((cubicmap.pixels[locationCellY * cubicmap.width + (locationCellX - 1)].r == 0) &&
(cubicmap.pixels[(locationCellY - 1) * cubicmap.width + (locationCellX)].r == 0) &&
(cubicmap.pixels[(locationCellY - 1) * cubicmap.width + (locationCellX - 1)].r != 0))
{
if (((playerPosition->x + 0.5f) - locationCellX < 0.3) &&
((playerPosition->z + 0.5f) - locationCellY < 0.3))
{
if (((playerPosition->x + 0.5f) - locationCellX) > ((playerPosition->z + 0.5f) - locationCellY)) playerPosition->x = locationCellX - 0.2;
else playerPosition->z = locationCellY - 0.2;
}
}
// Axis x-, y+
if ((cubicmap.pixels[locationCellY * cubicmap.width + (locationCellX - 1)].r == 0) &&
(cubicmap.pixels[(locationCellY + 1) * cubicmap.width + (locationCellX)].r == 0) &&
(cubicmap.pixels[(locationCellY + 1) * cubicmap.width + (locationCellX - 1)].r != 0))
{
if (((playerPosition->x + 0.5f) - locationCellX < 0.3) &&
((playerPosition->z + 0.5f) - locationCellY > 0.7))
{
if (((playerPosition->x + 0.5f) - locationCellX) > (1 - ((playerPosition->z + 0.5f) - locationCellY))) playerPosition->x = locationCellX - 0.2;
else playerPosition->z = locationCellY + 0.2;
}
}
// Axis x+, y-
if ((cubicmap.pixels[locationCellY * cubicmap.width + (locationCellX + 1)].r == 0) &&
(cubicmap.pixels[(locationCellY - 1) * cubicmap.width + (locationCellX)].r == 0) &&
(cubicmap.pixels[(locationCellY - 1) * cubicmap.width + (locationCellX + 1)].r != 0))
{
if (((playerPosition->x + 0.5f) - locationCellX > 0.7) &&
((playerPosition->z + 0.5f) - locationCellY < 0.3))
{
if (((playerPosition->x + 0.5f) - locationCellX) < (1 - ((playerPosition->z + 0.5f) - locationCellY))) playerPosition->x = locationCellX + 0.2;
else playerPosition->z = locationCellY - 0.2;
}
}
// Axis x+, y+
if ((cubicmap.pixels[locationCellY * cubicmap.width + (locationCellX + 1)].r == 0) &&
(cubicmap.pixels[(locationCellY + 1) * cubicmap.width + (locationCellX)].r == 0) &&
(cubicmap.pixels[(locationCellY + 1) * cubicmap.width + (locationCellX + 1)].r != 0))
{
if (((playerPosition->x + 0.5f) - locationCellX > 0.7) &&
((playerPosition->z + 0.5f) - locationCellY > 0.7))
{
if (((playerPosition->x + 0.5f) - locationCellX) < ((playerPosition->z + 0.5f) - locationCellY)) playerPosition->x = locationCellX + 0.2;
else playerPosition->z = locationCellY + 0.2;
}
}
}
//----------------------------------------------------------------------------------
// Module specific Functions Definition
//----------------------------------------------------------------------------------
// Get current vertex y altitude (proportional to pixel colors in grayscale)
static float GetHeightValue(Color pixel)
{
@ -1592,234 +1828,3 @@ static VertexData LoadOBJ(const char *fileName)
return vData;
}
bool CheckCollisionSpheres(Vector3 centerA, float radiusA, Vector3 centerB, float radiusB)
{
bool collision = false;
float dx = centerA.x - centerB.x; // X distance between centers
float dy = centerA.y - centerB.y; // Y distance between centers
float dz = centerA.z - centerB.z; // Y distance between centers
float distance = sqrt(dx*dx + dy*dy + dz*dz); // Distance between centers
if (distance <= (radiusA + radiusB)) collision = true;
return collision;
}
bool CheckCollisionBoxes(Vector3 minBBox1, Vector3 maxBBox1, Vector3 minBBox2, Vector3 maxBBox2)
{
/*
// Get min and max vertex to construct bounds (AABB)
Vector3 minVertex = tempVertices[0];
Vector3 maxVertex = tempVertices[0];
for (int i = 1; i < tempVertices.Count; i++)
{
minVertex = Vector3.Min(minVertex, tempVertices[i]);
maxVertex = Vector3.Max(maxVertex, tempVertices[i]);
}
bounds = new BoundingBox(minVertex, maxVertex);
*/
bool collision = true;
if ((maxBBox1.x >= minBBox2.x) && (minBBox1.x <= maxBBox2.x))
{
if ((maxBBox1.y < minBBox2.y) || (minBBox1.y > maxBBox2.y)) collision = false;
if ((maxBBox1.z < minBBox2.z) || (minBBox1.z > maxBBox2.z)) collision = false;
}
else collision = false;
return collision;
}
bool CheckCollisionBoxSphere(Vector3 minBBox, Vector3 maxBBox, Vector3 centerSphere, float radiusSphere)
{
bool collision = false;
if ((centerSphere.x - minBBox.x > radiusSphere) && (centerSphere.y - minBBox.y > radiusSphere) && (centerSphere.z - minBBox.z > radiusSphere) &&
(maxBBox.x - centerSphere.x > radiusSphere) && (maxBBox.y - centerSphere.y > radiusSphere) && (maxBBox.z - centerSphere.z > radiusSphere))
{
collision = true;
}
else
{
float dmin = 0;
if (centerSphere.x - minBBox.x <= radiusSphere)
dmin += (centerSphere.x - minBBox.x) * (centerSphere.x - minBBox.x);
else if (maxBBox.x - centerSphere.x <= radiusSphere)
dmin += (centerSphere.x - maxBBox.x) * (centerSphere.x - maxBBox.x);
if (centerSphere.y - minBBox.y <= radiusSphere)
dmin += (centerSphere.y - minBBox.y) * (centerSphere.y - minBBox.y);
else if (maxBBox.y - centerSphere.y <= radiusSphere)
dmin += (centerSphere.y - maxBBox.y) * (centerSphere.y - maxBBox.y);
if (centerSphere.z - minBBox.z <= radiusSphere)
dmin += (centerSphere.z - minBBox.z) * (centerSphere.z - minBBox.z);
else if (maxBBox.z - centerSphere.z <= radiusSphere)
dmin += (centerSphere.z - maxBBox.z) * (centerSphere.z - maxBBox.z);
if (dmin <= radiusSphere * radiusSphere) collision = true;
}
return collision;
}
// TODO
//BoundingBox GetCollisionArea(BoundingBox box1, BoundingBox box2)
// Detect and resolve cubicmap collisions
// NOTE: player position (or camera) is modified inside this function
void ResolveCollisionCubicmap(Image cubicmap, Vector3 mapPosition, Vector3 *playerPosition)
{
// Detect the cell where the player is located
int locationCellX = 0;
int locationCellY = 0;
locationCellX = floor(playerPosition->x + mapPosition.x + 0.5);
locationCellY = floor(playerPosition->z + mapPosition.z + 0.5);
// Multiple Axis --------------------------------------------------------------------------------------------
// Axis x-, y-
if ((cubicmap.pixels[locationCellY * cubicmap.width + (locationCellX - 1)].r != 0) &&
(cubicmap.pixels[(locationCellY - 1) * cubicmap.width + (locationCellX)].r != 0))
{
if (((playerPosition->x + 0.5f) - locationCellX < 0.3) &&
((playerPosition->z + 0.5f) - locationCellY < 0.3))
{
playerPosition->x = locationCellX - 0.2;
playerPosition->z = locationCellY - 0.2;
}
}
// Axis x-, y+
if ((cubicmap.pixels[locationCellY * cubicmap.width + (locationCellX - 1)].r != 0) &&
(cubicmap.pixels[(locationCellY + 1) * cubicmap.width + (locationCellX)].r != 0))
{
if (((playerPosition->x + 0.5f) - locationCellX < 0.3) &&
((playerPosition->z + 0.5f) - locationCellY > 0.7))
{
playerPosition->x = locationCellX - 0.2;
playerPosition->z = locationCellY + 0.2;
}
}
// Axis x+, y-
if ((cubicmap.pixels[locationCellY * cubicmap.width + (locationCellX + 1)].r != 0) &&
(cubicmap.pixels[(locationCellY - 1) * cubicmap.width + (locationCellX)].r != 0))
{
if (((playerPosition->x + 0.5f) - locationCellX > 0.7) &&
((playerPosition->z + 0.5f) - locationCellY < 0.3))
{
playerPosition->x = locationCellX + 0.2;
playerPosition->z = locationCellY - 0.2;
}
}
// Axis x+, y+
if ((cubicmap.pixels[locationCellY * cubicmap.width + (locationCellX + 1)].r != 0) &&
(cubicmap.pixels[(locationCellY + 1) * cubicmap.width + (locationCellX)].r != 0))
{
if (((playerPosition->x + 0.5f) - locationCellX > 0.7) &&
((playerPosition->z + 0.5f) - locationCellY > 0.7))
{
playerPosition->x = locationCellX + 0.2f;
playerPosition->z = locationCellY + 0.2f;
}
}
// Single Axis ---------------------------------------------------------------------------------------------------
// Axis x-
if (cubicmap.pixels[locationCellY * cubicmap.width + (locationCellX - 1)].r != 0)
{
if ((playerPosition->x + 0.5f) - locationCellX < 0.3)
{
playerPosition->x = locationCellX - 0.2;
}
}
// Axis x+
if (cubicmap.pixels[locationCellY * cubicmap.width + (locationCellX + 1)].r != 0)
{
if ((playerPosition->x + 0.5f) - locationCellX > 0.7)
{
playerPosition->x = locationCellX + 0.2;
}
}
// Axis y-
if (cubicmap.pixels[(locationCellY - 1) * cubicmap.width + (locationCellX)].r != 0)
{
if ((playerPosition->z + 0.5f) - locationCellY < 0.3)
{
playerPosition->z = locationCellY - 0.2;
}
}
// Axis y+
if (cubicmap.pixels[(locationCellY + 1) * cubicmap.width + (locationCellX)].r != 0)
{
if ((playerPosition->z + 0.5f) - locationCellY > 0.7)
{
playerPosition->z = locationCellY + 0.2;
}
}
// Diagonals -------------------------------------------------------------------------------------------------------
// Axis x-, y-
if ((cubicmap.pixels[locationCellY * cubicmap.width + (locationCellX - 1)].r == 0) &&
(cubicmap.pixels[(locationCellY - 1) * cubicmap.width + (locationCellX)].r == 0) &&
(cubicmap.pixels[(locationCellY - 1) * cubicmap.width + (locationCellX - 1)].r != 0))
{
if (((playerPosition->x + 0.5f) - locationCellX < 0.3) &&
((playerPosition->z + 0.5f) - locationCellY < 0.3))
{
if (((playerPosition->x + 0.5f) - locationCellX) > ((playerPosition->z + 0.5f) - locationCellY)) playerPosition->x = locationCellX - 0.2;
else playerPosition->z = locationCellY - 0.2;
}
}
// Axis x-, y+
if ((cubicmap.pixels[locationCellY * cubicmap.width + (locationCellX - 1)].r == 0) &&
(cubicmap.pixels[(locationCellY + 1) * cubicmap.width + (locationCellX)].r == 0) &&
(cubicmap.pixels[(locationCellY + 1) * cubicmap.width + (locationCellX - 1)].r != 0))
{
if (((playerPosition->x + 0.5f) - locationCellX < 0.3) &&
((playerPosition->z + 0.5f) - locationCellY > 0.7))
{
if (((playerPosition->x + 0.5f) - locationCellX) > (1 - ((playerPosition->z + 0.5f) - locationCellY))) playerPosition->x = locationCellX - 0.2;
else playerPosition->z = locationCellY + 0.2;
}
}
// Axis x+, y-
if ((cubicmap.pixels[locationCellY * cubicmap.width + (locationCellX + 1)].r == 0) &&
(cubicmap.pixels[(locationCellY - 1) * cubicmap.width + (locationCellX)].r == 0) &&
(cubicmap.pixels[(locationCellY - 1) * cubicmap.width + (locationCellX + 1)].r != 0))
{
if (((playerPosition->x + 0.5f) - locationCellX > 0.7) &&
((playerPosition->z + 0.5f) - locationCellY < 0.3))
{
if (((playerPosition->x + 0.5f) - locationCellX) < (1 - ((playerPosition->z + 0.5f) - locationCellY))) playerPosition->x = locationCellX + 0.2;
else playerPosition->z = locationCellY - 0.2;
}
}
// Axis x+, y+
if ((cubicmap.pixels[locationCellY * cubicmap.width + (locationCellX + 1)].r == 0) &&
(cubicmap.pixels[(locationCellY + 1) * cubicmap.width + (locationCellX)].r == 0) &&
(cubicmap.pixels[(locationCellY + 1) * cubicmap.width + (locationCellX + 1)].r != 0))
{
if (((playerPosition->x + 0.5f) - locationCellX > 0.7) &&
((playerPosition->z + 0.5f) - locationCellY > 0.7))
{
if (((playerPosition->x + 0.5f) - locationCellX) < ((playerPosition->z + 0.5f) - locationCellY)) playerPosition->x = locationCellX + 0.2;
else playerPosition->z = locationCellY + 0.2;
}
}
}

+ 3
- 1
src/raylib.h Parādīt failu

@ -343,6 +343,8 @@ int GetRandomValue(int min, int max); // Returns a random
Color Fade(Color color, float alpha); // Color fade-in or fade-out, alpha goes from 0.0f to 1.0f
void SetCameraMode(int mode); // Multiple camera modes available
void UpdateCamera(Vector3 *playerPosition);
void SetConfigFlags(char flags); // Enable some window configurations
void ShowLogo(void); // Activates raylib logo at startup (can be done with flags)
@ -498,7 +500,7 @@ unsigned int LoadCustomShader(char *vsFileName, char *fsFileName);
bool CheckCollisionSpheres(Vector3 centerA, float radiusA, Vector3 centerB, float radiusB);
bool CheckCollisionBoxes(Vector3 minBBox1, Vector3 maxBBox1, Vector3 minBBox2, Vector3 maxBBox2);
bool CheckCollisionBoxSphere(Vector3 minBBox, Vector3 maxBBox, Vector3 centerSphere, float radiusSphere);
//void ResolveCollisionCubicmap(Image cubicmap, Vector3 mapPosition, Vector3 *playerPosition);
void f">ResolveCollisionCubicmap(Image cubicmap, Vector3 mapPosition, Vector3 *playerPosition);
//------------------------------------------------------------------------------------
// Audio Loading and Playing Functions (Module: audio)

Notiek ielāde…
Atcelt
Saglabāt