mirror of
https://github.com/raysan5/raylib.git
synced 2025-09-06 11:28:17 +00:00
Review ResolveCollisionCubicmap()
This function needs to be redesigned or removed...
This commit is contained in:
71
src/models.c
71
src/models.c
@@ -40,7 +40,7 @@
|
|||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
// Defines and Macros
|
// Defines and Macros
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
#define CUBIC_MAP_HALF_BLOCK_SIZE 0.5
|
// ...
|
||||||
|
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
// Types and Structures Definition
|
// Types and Structures Definition
|
||||||
@@ -1542,8 +1542,11 @@ BoundingBox CalculateBoundingBox(Mesh mesh)
|
|||||||
|
|
||||||
// Detect and resolve cubicmap collisions
|
// Detect and resolve cubicmap collisions
|
||||||
// NOTE: player position (or camera) is modified inside this function
|
// NOTE: player position (or camera) is modified inside this function
|
||||||
|
// TODO: This functions needs to be completely reviewed!
|
||||||
Vector3 ResolveCollisionCubicmap(Image cubicmap, Vector3 mapPosition, Vector3 *playerPosition, float radius)
|
Vector3 ResolveCollisionCubicmap(Image cubicmap, Vector3 mapPosition, Vector3 *playerPosition, float radius)
|
||||||
{
|
{
|
||||||
|
#define CUBIC_MAP_HALF_BLOCK_SIZE 0.5
|
||||||
|
|
||||||
Color *cubicmapPixels = GetImageData(cubicmap);
|
Color *cubicmapPixels = GetImageData(cubicmap);
|
||||||
|
|
||||||
// Detect the cell where the player is located
|
// Detect the cell where the player is located
|
||||||
@@ -1555,15 +1558,15 @@ Vector3 ResolveCollisionCubicmap(Image cubicmap, Vector3 mapPosition, Vector3 *p
|
|||||||
locationCellX = floor(playerPosition->x - mapPosition.x + CUBIC_MAP_HALF_BLOCK_SIZE);
|
locationCellX = floor(playerPosition->x - mapPosition.x + CUBIC_MAP_HALF_BLOCK_SIZE);
|
||||||
locationCellY = floor(playerPosition->z - mapPosition.z + CUBIC_MAP_HALF_BLOCK_SIZE);
|
locationCellY = floor(playerPosition->z - mapPosition.z + CUBIC_MAP_HALF_BLOCK_SIZE);
|
||||||
|
|
||||||
if (locationCellX >= 0 && locationCellY >= 0 && locationCellX < cubicmap.width && locationCellY < cubicmap.height)
|
if ((locationCellX >= 0) && (locationCellY >= 0) && (locationCellX < cubicmap.width) && (locationCellY < cubicmap.height))
|
||||||
{
|
{
|
||||||
// Multiple Axis --------------------------------------------------------------------------------------------
|
// Multiple Axis --------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
// Axis x-, y-
|
// Axis x-, y-
|
||||||
if (locationCellX > 0 && locationCellY > 0)
|
if ((locationCellX > 0) && (locationCellY > 0))
|
||||||
{
|
{
|
||||||
if ((cubicmapPixels[locationCellY * cubicmap.width + (locationCellX - 1)].r != 0) &&
|
if ((cubicmapPixels[locationCellY*cubicmap.width + (locationCellX - 1)].r != 0) &&
|
||||||
(cubicmapPixels[(locationCellY - 1) * cubicmap.width + (locationCellX)].r != 0))
|
(cubicmapPixels[(locationCellY - 1)*cubicmap.width + (locationCellX)].r != 0))
|
||||||
{
|
{
|
||||||
if (((playerPosition->x - mapPosition.x + CUBIC_MAP_HALF_BLOCK_SIZE) - locationCellX < radius) &&
|
if (((playerPosition->x - mapPosition.x + CUBIC_MAP_HALF_BLOCK_SIZE) - locationCellX < radius) &&
|
||||||
((playerPosition->z - mapPosition.z + CUBIC_MAP_HALF_BLOCK_SIZE) - locationCellY < radius))
|
((playerPosition->z - mapPosition.z + CUBIC_MAP_HALF_BLOCK_SIZE) - locationCellY < radius))
|
||||||
@@ -1576,10 +1579,10 @@ Vector3 ResolveCollisionCubicmap(Image cubicmap, Vector3 mapPosition, Vector3 *p
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Axis x-, y+
|
// Axis x-, y+
|
||||||
if (locationCellX > 0 && locationCellY < cubicmap.height - 1)
|
if ((locationCellX > 0) && (locationCellY < cubicmap.height - 1))
|
||||||
{
|
{
|
||||||
if ((cubicmapPixels[locationCellY * cubicmap.width + (locationCellX - 1)].r != 0) &&
|
if ((cubicmapPixels[locationCellY*cubicmap.width + (locationCellX - 1)].r != 0) &&
|
||||||
(cubicmapPixels[(locationCellY + 1) * cubicmap.width + (locationCellX)].r != 0))
|
(cubicmapPixels[(locationCellY + 1)*cubicmap.width + (locationCellX)].r != 0))
|
||||||
{
|
{
|
||||||
if (((playerPosition->x - mapPosition.x + CUBIC_MAP_HALF_BLOCK_SIZE) - locationCellX < radius) &&
|
if (((playerPosition->x - mapPosition.x + CUBIC_MAP_HALF_BLOCK_SIZE) - locationCellX < radius) &&
|
||||||
((playerPosition->z - mapPosition.z + CUBIC_MAP_HALF_BLOCK_SIZE) - locationCellY > 1 - radius))
|
((playerPosition->z - mapPosition.z + CUBIC_MAP_HALF_BLOCK_SIZE) - locationCellY > 1 - radius))
|
||||||
@@ -1592,10 +1595,10 @@ Vector3 ResolveCollisionCubicmap(Image cubicmap, Vector3 mapPosition, Vector3 *p
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Axis x+, y-
|
// Axis x+, y-
|
||||||
if (locationCellX < cubicmap.width - 1 && locationCellY > 0)
|
if ((locationCellX < cubicmap.width - 1) && (locationCellY > 0))
|
||||||
{
|
{
|
||||||
if ((cubicmapPixels[locationCellY * cubicmap.width + (locationCellX + 1)].r != 0) &&
|
if ((cubicmapPixels[locationCellY*cubicmap.width + (locationCellX + 1)].r != 0) &&
|
||||||
(cubicmapPixels[(locationCellY - 1) * cubicmap.width + (locationCellX)].r != 0))
|
(cubicmapPixels[(locationCellY - 1)*cubicmap.width + (locationCellX)].r != 0))
|
||||||
{
|
{
|
||||||
if (((playerPosition->x - mapPosition.x + CUBIC_MAP_HALF_BLOCK_SIZE) - locationCellX > 1 - radius) &&
|
if (((playerPosition->x - mapPosition.x + CUBIC_MAP_HALF_BLOCK_SIZE) - locationCellX > 1 - radius) &&
|
||||||
((playerPosition->z - mapPosition.z + CUBIC_MAP_HALF_BLOCK_SIZE) - locationCellY < radius))
|
((playerPosition->z - mapPosition.z + CUBIC_MAP_HALF_BLOCK_SIZE) - locationCellY < radius))
|
||||||
@@ -1608,10 +1611,10 @@ Vector3 ResolveCollisionCubicmap(Image cubicmap, Vector3 mapPosition, Vector3 *p
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Axis x+, y+
|
// Axis x+, y+
|
||||||
if (locationCellX < cubicmap.width - 1 && locationCellY < cubicmap.height - 1)
|
if ((locationCellX < cubicmap.width - 1) && (locationCellY < cubicmap.height - 1))
|
||||||
{
|
{
|
||||||
if ((cubicmapPixels[locationCellY * cubicmap.width + (locationCellX + 1)].r != 0) &&
|
if ((cubicmapPixels[locationCellY*cubicmap.width + (locationCellX + 1)].r != 0) &&
|
||||||
(cubicmapPixels[(locationCellY + 1) * cubicmap.width + (locationCellX)].r != 0))
|
(cubicmapPixels[(locationCellY + 1)*cubicmap.width + (locationCellX)].r != 0))
|
||||||
{
|
{
|
||||||
if (((playerPosition->x - mapPosition.x + CUBIC_MAP_HALF_BLOCK_SIZE) - locationCellX > 1 - radius) &&
|
if (((playerPosition->x - mapPosition.x + CUBIC_MAP_HALF_BLOCK_SIZE) - locationCellX > 1 - radius) &&
|
||||||
((playerPosition->z - mapPosition.z + CUBIC_MAP_HALF_BLOCK_SIZE) - locationCellY > 1 - radius))
|
((playerPosition->z - mapPosition.z + CUBIC_MAP_HALF_BLOCK_SIZE) - locationCellY > 1 - radius))
|
||||||
@@ -1628,7 +1631,7 @@ Vector3 ResolveCollisionCubicmap(Image cubicmap, Vector3 mapPosition, Vector3 *p
|
|||||||
// Axis x-
|
// Axis x-
|
||||||
if (locationCellX > 0)
|
if (locationCellX > 0)
|
||||||
{
|
{
|
||||||
if (cubicmapPixels[locationCellY * cubicmap.width + (locationCellX - 1)].r != 0)
|
if (cubicmapPixels[locationCellY*cubicmap.width + (locationCellX - 1)].r != 0)
|
||||||
{
|
{
|
||||||
if ((playerPosition->x - mapPosition.x + CUBIC_MAP_HALF_BLOCK_SIZE) - locationCellX < radius)
|
if ((playerPosition->x - mapPosition.x + CUBIC_MAP_HALF_BLOCK_SIZE) - locationCellX < radius)
|
||||||
{
|
{
|
||||||
@@ -1640,7 +1643,7 @@ Vector3 ResolveCollisionCubicmap(Image cubicmap, Vector3 mapPosition, Vector3 *p
|
|||||||
// Axis x+
|
// Axis x+
|
||||||
if (locationCellX < cubicmap.width - 1)
|
if (locationCellX < cubicmap.width - 1)
|
||||||
{
|
{
|
||||||
if (cubicmapPixels[locationCellY * cubicmap.width + (locationCellX + 1)].r != 0)
|
if (cubicmapPixels[locationCellY*cubicmap.width + (locationCellX + 1)].r != 0)
|
||||||
{
|
{
|
||||||
if ((playerPosition->x - mapPosition.x + CUBIC_MAP_HALF_BLOCK_SIZE) - locationCellX > 1 - radius)
|
if ((playerPosition->x - mapPosition.x + CUBIC_MAP_HALF_BLOCK_SIZE) - locationCellX > 1 - radius)
|
||||||
{
|
{
|
||||||
@@ -1652,7 +1655,7 @@ Vector3 ResolveCollisionCubicmap(Image cubicmap, Vector3 mapPosition, Vector3 *p
|
|||||||
// Axis y-
|
// Axis y-
|
||||||
if (locationCellY > 0)
|
if (locationCellY > 0)
|
||||||
{
|
{
|
||||||
if (cubicmapPixels[(locationCellY - 1) * cubicmap.width + (locationCellX)].r != 0)
|
if (cubicmapPixels[(locationCellY - 1)*cubicmap.width + (locationCellX)].r != 0)
|
||||||
{
|
{
|
||||||
if ((playerPosition->z - mapPosition.z + CUBIC_MAP_HALF_BLOCK_SIZE) - locationCellY < radius)
|
if ((playerPosition->z - mapPosition.z + CUBIC_MAP_HALF_BLOCK_SIZE) - locationCellY < radius)
|
||||||
{
|
{
|
||||||
@@ -1664,7 +1667,7 @@ Vector3 ResolveCollisionCubicmap(Image cubicmap, Vector3 mapPosition, Vector3 *p
|
|||||||
// Axis y+
|
// Axis y+
|
||||||
if (locationCellY < cubicmap.height - 1)
|
if (locationCellY < cubicmap.height - 1)
|
||||||
{
|
{
|
||||||
if (cubicmapPixels[(locationCellY + 1) * cubicmap.width + (locationCellX)].r != 0)
|
if (cubicmapPixels[(locationCellY + 1)*cubicmap.width + (locationCellX)].r != 0)
|
||||||
{
|
{
|
||||||
if ((playerPosition->z - mapPosition.z + CUBIC_MAP_HALF_BLOCK_SIZE) - locationCellY > 1 - radius)
|
if ((playerPosition->z - mapPosition.z + CUBIC_MAP_HALF_BLOCK_SIZE) - locationCellY > 1 - radius)
|
||||||
{
|
{
|
||||||
@@ -1677,11 +1680,11 @@ Vector3 ResolveCollisionCubicmap(Image cubicmap, Vector3 mapPosition, Vector3 *p
|
|||||||
// Diagonals -------------------------------------------------------------------------------------------------------
|
// Diagonals -------------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
// Axis x-, y-
|
// Axis x-, y-
|
||||||
if (locationCellX > 0 && locationCellY > 0)
|
if ((locationCellX > 0) && (locationCellY > 0))
|
||||||
{
|
{
|
||||||
if ((cubicmapPixels[locationCellY * cubicmap.width + (locationCellX - 1)].r == 0) &&
|
if ((cubicmapPixels[locationCellY*cubicmap.width + (locationCellX - 1)].r == 0) &&
|
||||||
(cubicmapPixels[(locationCellY - 1) * cubicmap.width + (locationCellX)].r == 0) &&
|
(cubicmapPixels[(locationCellY - 1)*cubicmap.width + (locationCellX)].r == 0) &&
|
||||||
(cubicmapPixels[(locationCellY - 1) * cubicmap.width + (locationCellX - 1)].r != 0))
|
(cubicmapPixels[(locationCellY - 1)*cubicmap.width + (locationCellX - 1)].r != 0))
|
||||||
{
|
{
|
||||||
if (((playerPosition->x - mapPosition.x + CUBIC_MAP_HALF_BLOCK_SIZE) - locationCellX < radius) &&
|
if (((playerPosition->x - mapPosition.x + CUBIC_MAP_HALF_BLOCK_SIZE) - locationCellX < radius) &&
|
||||||
((playerPosition->z - mapPosition.z + CUBIC_MAP_HALF_BLOCK_SIZE) - locationCellY < radius))
|
((playerPosition->z - mapPosition.z + CUBIC_MAP_HALF_BLOCK_SIZE) - locationCellY < radius))
|
||||||
@@ -1700,11 +1703,11 @@ Vector3 ResolveCollisionCubicmap(Image cubicmap, Vector3 mapPosition, Vector3 *p
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Axis x-, y+
|
// Axis x-, y+
|
||||||
if (locationCellX > 0 && locationCellY < cubicmap.height - 1)
|
if ((locationCellX > 0) && (locationCellY < cubicmap.height - 1))
|
||||||
{
|
{
|
||||||
if ((cubicmapPixels[locationCellY * cubicmap.width + (locationCellX - 1)].r == 0) &&
|
if ((cubicmapPixels[locationCellY*cubicmap.width + (locationCellX - 1)].r == 0) &&
|
||||||
(cubicmapPixels[(locationCellY + 1) * cubicmap.width + (locationCellX)].r == 0) &&
|
(cubicmapPixels[(locationCellY + 1)*cubicmap.width + (locationCellX)].r == 0) &&
|
||||||
(cubicmapPixels[(locationCellY + 1) * cubicmap.width + (locationCellX - 1)].r != 0))
|
(cubicmapPixels[(locationCellY + 1)*cubicmap.width + (locationCellX - 1)].r != 0))
|
||||||
{
|
{
|
||||||
if (((playerPosition->x - mapPosition.x + CUBIC_MAP_HALF_BLOCK_SIZE) - locationCellX < radius) &&
|
if (((playerPosition->x - mapPosition.x + CUBIC_MAP_HALF_BLOCK_SIZE) - locationCellX < radius) &&
|
||||||
((playerPosition->z - mapPosition.z + CUBIC_MAP_HALF_BLOCK_SIZE) - locationCellY > 1 - radius))
|
((playerPosition->z - mapPosition.z + CUBIC_MAP_HALF_BLOCK_SIZE) - locationCellY > 1 - radius))
|
||||||
@@ -1723,11 +1726,11 @@ Vector3 ResolveCollisionCubicmap(Image cubicmap, Vector3 mapPosition, Vector3 *p
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Axis x+, y-
|
// Axis x+, y-
|
||||||
if (locationCellX < cubicmap.width - 1 && locationCellY > 0)
|
if ((locationCellX < cubicmap.width - 1) && (locationCellY > 0))
|
||||||
{
|
{
|
||||||
if ((cubicmapPixels[locationCellY * cubicmap.width + (locationCellX + 1)].r == 0) &&
|
if ((cubicmapPixels[locationCellY*cubicmap.width + (locationCellX + 1)].r == 0) &&
|
||||||
(cubicmapPixels[(locationCellY - 1) * cubicmap.width + (locationCellX)].r == 0) &&
|
(cubicmapPixels[(locationCellY - 1)*cubicmap.width + (locationCellX)].r == 0) &&
|
||||||
(cubicmapPixels[(locationCellY - 1) * cubicmap.width + (locationCellX + 1)].r != 0))
|
(cubicmapPixels[(locationCellY - 1)*cubicmap.width + (locationCellX + 1)].r != 0))
|
||||||
{
|
{
|
||||||
if (((playerPosition->x - mapPosition.x + CUBIC_MAP_HALF_BLOCK_SIZE) - locationCellX > 1 - radius) &&
|
if (((playerPosition->x - mapPosition.x + CUBIC_MAP_HALF_BLOCK_SIZE) - locationCellX > 1 - radius) &&
|
||||||
((playerPosition->z - mapPosition.z + CUBIC_MAP_HALF_BLOCK_SIZE) - locationCellY < radius))
|
((playerPosition->z - mapPosition.z + CUBIC_MAP_HALF_BLOCK_SIZE) - locationCellY < radius))
|
||||||
@@ -1746,11 +1749,11 @@ Vector3 ResolveCollisionCubicmap(Image cubicmap, Vector3 mapPosition, Vector3 *p
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Axis x+, y+
|
// Axis x+, y+
|
||||||
if (locationCellX < cubicmap.width - 1 && locationCellY < cubicmap.height - 1)
|
if ((locationCellX < cubicmap.width - 1) && (locationCellY < cubicmap.height - 1))
|
||||||
{
|
{
|
||||||
if ((cubicmapPixels[locationCellY * cubicmap.width + (locationCellX + 1)].r == 0) &&
|
if ((cubicmapPixels[locationCellY*cubicmap.width + (locationCellX + 1)].r == 0) &&
|
||||||
(cubicmapPixels[(locationCellY + 1) * cubicmap.width + (locationCellX)].r == 0) &&
|
(cubicmapPixels[(locationCellY + 1)*cubicmap.width + (locationCellX)].r == 0) &&
|
||||||
(cubicmapPixels[(locationCellY + 1) * cubicmap.width + (locationCellX + 1)].r != 0))
|
(cubicmapPixels[(locationCellY + 1)*cubicmap.width + (locationCellX + 1)].r != 0))
|
||||||
{
|
{
|
||||||
if (((playerPosition->x - mapPosition.x + CUBIC_MAP_HALF_BLOCK_SIZE) - locationCellX > 1 - radius) &&
|
if (((playerPosition->x - mapPosition.x + CUBIC_MAP_HALF_BLOCK_SIZE) - locationCellX > 1 - radius) &&
|
||||||
((playerPosition->z - mapPosition.z + CUBIC_MAP_HALF_BLOCK_SIZE) - locationCellY > 1 - radius))
|
((playerPosition->z - mapPosition.z + CUBIC_MAP_HALF_BLOCK_SIZE) - locationCellY > 1 - radius))
|
||||||
|
Reference in New Issue
Block a user