From b011b2ca4e161fea2a742cb9b1673cd84cf0eba5 Mon Sep 17 00:00:00 2001 From: jussi Date: Mon, 5 Aug 2024 23:35:57 +0300 Subject: GetRayBoxCells fix. --- src/lua_core.c | 1 + src/models.c | 27 +++++++++++++++++---------- src/platforms/core_desktop.c | 9 +++++---- src/textures.c | 2 +- 4 files changed, 24 insertions(+), 15 deletions(-) (limited to 'src') diff --git a/src/lua_core.c b/src/lua_core.c index 8071a0f..41796ef 100644 --- a/src/lua_core.c +++ b/src/lua_core.c @@ -755,6 +755,7 @@ static void defineGlobals() { assignGlobalColor( RAYWHITE, "RAYWHITE" ); // My own White (raylib logo) /* Math */ assignGlobalFloat( PI, "PI" ); // Pi + assignGlobalFloat( EPSILON, "EPSILON" ); // Epsilon assignGlobalFloat( DEG2RAD, "DEG2RAD" ); // Degrees to radians assignGlobalFloat( RAD2DEG, "RAD2DEG" ); // Radians to degrees /* Gui control state */ diff --git a/src/models.c b/src/models.c index d33e130..24474ab 100644 --- a/src/models.c +++ b/src/models.c @@ -2516,7 +2516,7 @@ int lmodelsGetRayCollisionTriangle( lua_State* L ) { /* > rayCollision = RL.GetRayCollisionQuad( Ray ray, Vector3 p1, Vector3 p2, Vector3 p3, Vector3 p4 ) -Get collision info between ray and quad +Get collision info between ray and quad. NOTE: The points are expected to be in counter-clockwise winding - Success return RayCollision */ @@ -2599,11 +2599,6 @@ int lmodelsGetRayBoxCells( lua_State* L ) { 0.0f <= ray.direction.z ? 1.0f : -1.0f }; /* We transform everything to absolute space to make this simpler. */ - Vector3 absBounds = { - 0.0f < signs.x ? boxSizeCells.x - cellPos.x : cellPos.x + 1.0f, - 0.0f < signs.y ? boxSizeCells.y - cellPos.y : cellPos.y + 1.0f, - 0.0f < signs.z ? boxSizeCells.z - cellPos.z : cellPos.z + 1.0f - }; Vector3 absDir = { fabsf( ray.direction.x ), fabsf( ray.direction.y ), @@ -2614,6 +2609,21 @@ int lmodelsGetRayBoxCells( lua_State* L ) { 0.0f < signs.y ? localRayPos.y - cellPos.y * cellSize.y : cellSize.y - ( localRayPos.y - cellPos.y * cellSize.y ), 0.0f < signs.z ? localRayPos.z - cellPos.z * cellSize.z : cellSize.z - ( localRayPos.z - cellPos.z * cellSize.z ) }; + Vector3 absBounds = { + 0.0f < signs.x ? boxSize.x - localRayPos.x : localRayPos.x, + 0.0f < signs.y ? boxSize.y - localRayPos.y : localRayPos.y, + 0.0f < signs.z ? boxSize.z - localRayPos.z : localRayPos.z + }; + absBounds = Vector3Add( absBounds, absPos ); + + Vector3 exitDis = { + ( absBounds.x - absPos.x ) / absDir.x, + ( absBounds.y - absPos.y ) / absDir.y, + ( absBounds.z - absPos.z ) / absDir.z + }; + float exitScale = fmin( fmin( exitDis.x, exitDis.y ), exitDis.z ); + Vector3 exitPoint = Vector3Add( Vector3Scale( ray.direction, exitScale ), Vector3Add( localRayPos, box.min ) ); + Vector3 absCell = { 0, 0, 0 }; int cellId = 2; /* We already added first so we will start at 2. */ @@ -2636,17 +2646,14 @@ int lmodelsGetRayBoxCells( lua_State* L ) { float rayMoveScale = fmin( fmin( cellDis.x, cellDis.y ), cellDis.z ); absPos = Vector3Add( absPos, Vector3Scale( absDir, rayMoveScale ) ); - localRayPos = Vector3Add( localRayPos, Vector3Scale( ray.direction, rayMoveScale ) ); - if ( absCell.x < absBounds.x && absCell.y < absBounds.y && absCell.z < absBounds.z ) { + if ( absPos.x < absBounds.x && absPos.y < absBounds.y && absPos.z < absBounds.z ) { uluaPushVector3( L, (Vector3){ round( cellPos.x ), round( cellPos.y ), round( cellPos.z ) } ); lua_rawseti( L, -2, cellId ); cellId++; } else { - Vector3 exitPoint = Vector3Add( localRayPos, box.min ); - uluaPushRayCollision( L, (RayCollision){ .hit = true, .distance = Vector3Distance( ray.position, exitPoint ), diff --git a/src/platforms/core_desktop.c b/src/platforms/core_desktop.c index 6d774ee..8d9f01c 100644 --- a/src/platforms/core_desktop.c +++ b/src/platforms/core_desktop.c @@ -99,6 +99,7 @@ int lcoreGetKeyScancode( lua_State* L ) { Called when the window is resized. Type GLFW_WINDOW_SIZE_EVENT */ static void windowSizeEvent( GLFWwindow* window, int width, int height ) { +// GLFWwindowsizefun windowSizeEvent( GLFWwindow* window, int width, int height ) { /* Pass through to raylib callback. */ state->raylibWindowSizeCallback( window, width, height ); @@ -617,7 +618,7 @@ static void penTabletProximityEvent( int proxState ) { static void platformRegisterEvents() { /* Window events. */ - state->raylibWindowSizeCallback = glfwSetWindowSizeCallback( GetWindowHandle(), windowSizeEvent ); + state->raylibWindowSizeCallback = glfwSetWindowSizeCallback( GetWindowHandle(), (GLFWwindowsizefun)windowSizeEvent ); #if !defined( PLATFORM_WEB ) state->raylibWindowMaximizeCallback = glfwSetWindowMaximizeCallback( GetWindowHandle(), windowMaximizeEvent ); #endif @@ -634,9 +635,9 @@ static void platformRegisterEvents() { state->raylibCursorEnterCallback = glfwSetCursorEnterCallback( GetWindowHandle(), cursorEnterInputEvent ); state->raylibJoystickCallback = glfwSetJoystickCallback( joystickEvent ); /* NOTE! Experimental. Needs glfw PR https://github.com/glfw/glfw/pull/1445 */ - // state->glfwtabletDataCallback = glfwSetPenTabletDataCallback( penTabletDataEvent ); - // state->glfwtabletCursorCallback = glfwSetPenTabletCursorCallback( penTabletCursorEvent ); - // state->glfwtabletProximityCallback = glfwSetPenTabletProximityCallback( penTabletProximityEvent ); + // state->glfwTabletDataCallback = glfwSetPenTabletDataCallback( penTabletDataEvent ); + // state->glfwTabletCursorCallback = glfwSetPenTabletCursorCallback( penTabletCursorEvent ); + // state->glfwTabletProximityCallback = glfwSetPenTabletProximityCallback( penTabletProximityEvent ); } void luaPlatformRegister() { diff --git a/src/textures.c b/src/textures.c index 9d7221a..467485b 100644 --- a/src/textures.c +++ b/src/textures.c @@ -255,7 +255,7 @@ Generate image: plain color */ int ltexturesGenImageColor( lua_State* L ) { Vector2 size = uluaGetVector2( L, 1 ); - Color color = uluaGetColor( L, 1 ); + Color color = uluaGetColor( L, 2 ); uluaPushImage( L, GenImageColor( size.x, size.y, color ) ); -- cgit v1.2.3