GetRayBoxCells also returns exit point.

This commit is contained in:
jussi
2024-07-20 15:29:50 +03:00
parent 03e9226b5f
commit c6eb85b367
5 changed files with 35 additions and 17 deletions

6
API.md
View File

@@ -7989,11 +7989,11 @@ Get collision info between ray and quad
--- ---
> cells = RL.GetRayBoxCells( Ray ray, BoundingBox box, Vector3 cellSize ) > cells, exitPoint = RL.GetRayBoxCells( Ray ray, BoundingBox box, Vector3 cellSize )
Get cell positions inside box that intersect with the ray. Returns empty table if ray misses the box Get cell positions inside box that intersect with the ray. Also returns ray exit point. Returns empty table if ray misses the box
- Success return Vector3{} - Success return Vector3{}, RayCollision|nil
--- ---

View File

@@ -5137,12 +5137,13 @@ function RL.GetRayCollisionTriangle( ray, p1, p2, p3 ) end
---@return any rayCollision ---@return any rayCollision
function RL.GetRayCollisionQuad( ray, p1, p2, p3, p4 ) end function RL.GetRayCollisionQuad( ray, p1, p2, p3, p4 ) end
---Get cell positions inside box that intersect with the ray. Returns empty table if ray misses the box ---Get cell positions inside box that intersect with the ray. Also returns ray exit point. Returns empty table if ray misses the box
---- Success return Vector3{} ---- Success return Vector3{}, RayCollision|nil
---@param ray any ---@param ray any
---@param box any ---@param box any
---@param cellSize table ---@param cellSize table
---@return any cells ---@return any cells
---@return any exitPoint
function RL.GetRayBoxCells( ray, box, cellSize ) end function RL.GetRayBoxCells( ray, box, cellSize ) end
-- Audio - Audio device management functions -- Audio - Audio device management functions

View File

@@ -16,6 +16,7 @@ local drawCellSize = cellSize:clone()
local ray = nil local ray = nil
local rayCol = nil local rayCol = nil
local cells = {} local cells = {}
local exitPoint = {}
local guiMouseHover = false local guiMouseHover = false
local spinnerEdit = { local spinnerEdit = {
@@ -64,7 +65,7 @@ function RL.update( delta )
ray = RL.GetMouseRay( RL.GetMousePosition(), camera.camera ) ray = RL.GetMouseRay( RL.GetMousePosition(), camera.camera )
rayCol = box:getRayCollision( ray ) rayCol = box:getRayCollision( ray )
cells = RL.GetRayBoxCells( ray, box:maxToPos(), cellSize ) cells, exitPoint = RL.GetRayBoxCells( ray, box:maxToPos(), cellSize )
drawCellSize:setV( cellSize ) drawCellSize:setV( cellSize )
end end
end end
@@ -95,11 +96,12 @@ function RL.draw()
RL.DrawRay( ray, RL.BLUE ) RL.DrawRay( ray, RL.BLUE )
RL.DrawSphere( ray[1], 0.1, RL.GREEN ) RL.DrawSphere( ray[1], 0.1, RL.GREEN )
end end
if rayCol and rayCol.hit then if rayCol and rayCol.hit then
RL.DrawSphere( rayCol.point, 0.1, RL.RED ) RL.DrawSphere( rayCol.point, 0.1, RL.RED )
end end
if exitPoint.hit then
RL.DrawSphere( exitPoint.point, 0.1, RL.RED )
end
if cells then if cells then
for _, cell in ipairs( cells ) do for _, cell in ipairs( cells ) do
RL.DrawCubeWires( box.min + Vector3:tempT( cell ) * drawCellSize + drawCellSize:scale( 0.5 ), drawCellSize, RL.RED ) RL.DrawCubeWires( box.min + Vector3:tempT( cell ) * drawCellSize + drawCellSize:scale( 0.5 ), drawCellSize, RL.RED )

View File

@@ -2532,7 +2532,9 @@ Check if a mouse button is NOT being pressed
int lcoreIsMouseButtonUp( lua_State* L ) { int lcoreIsMouseButtonUp( lua_State* L ) {
int button = luaL_checkinteger( L, 1 ); int button = luaL_checkinteger( L, 1 );
lua_pushboolean( L, IsMouseButtonUp( button ) ); /* IsMouseButtonUp is broken. Review when fixed in raylib. */
lua_pushboolean( L, !IsMouseButtonDown( button ) );
// lua_pushboolean( L, IsMouseButtonUp( button ) );
return 1; return 1;
} }

View File

@@ -2547,11 +2547,11 @@ static inline bool isInsideBox( Vector3 position, BoundingBox box ) {
} }
/* /*
> cells = RL.GetRayBoxCells( Ray ray, BoundingBox box, Vector3 cellSize ) > cells, exitPoint = RL.GetRayBoxCells( Ray ray, BoundingBox box, Vector3 cellSize )
Get cell positions inside box that intersect with the ray. Returns empty table if ray misses the box Get cell positions inside box that intersect with the ray. Also returns ray exit point. Returns empty table if ray misses the box
- Success return Vector3{} - Success return Vector3{}, RayCollision|nil
*/ */
int lmodelsGetRayBoxCells( lua_State* L ) { int lmodelsGetRayBoxCells( lua_State* L ) {
Ray ray = uluaGetRay( L, 1 ); Ray ray = uluaGetRay( L, 1 );
@@ -2633,19 +2633,32 @@ int lmodelsGetRayBoxCells( lua_State* L ) {
absCell = Vector3Add( absCell, move ); absCell = Vector3Add( absCell, move );
cellPos = Vector3Add( cellPos, Vector3Multiply( move, signs ) ); cellPos = Vector3Add( cellPos, Vector3Multiply( move, signs ) );
if ( absCell.x < absBounds.x && absCell.y < absBounds.y && absCell.z < absBounds.z ) { float rayMoveScale = fmin( fmin( cellDis.x, cellDis.y ), cellDis.z );
absPos = Vector3Add( absPos, Vector3Scale( absDir, 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 ) {
uluaPushVector3( L, (Vector3){ round( cellPos.x ), round( cellPos.y ), round( cellPos.z ) } ); uluaPushVector3( L, (Vector3){ round( cellPos.x ), round( cellPos.y ), round( cellPos.z ) } );
lua_rawseti( L, -2, cellId ); lua_rawseti( L, -2, cellId );
cellId++; cellId++;
} }
else { else {
break; Vector3 exitPoint = Vector3Add( localRayPos, box.min );
uluaPushRayCollision( L, (RayCollision){
.hit = true,
.distance = Vector3Distance( ray.position, exitPoint ),
.point = exitPoint,
.normal = Vector3Multiply( move, Vector3Negate( signs ) )
} );
return 2;
} }
} }
} }
lua_pushnil( L );
return 1; return 2;
} }