summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--API.md8
-rw-r--r--ReiLua_API.lua8
-rw-r--r--changelog1
-rw-r--r--examples/ray_box_cells/main.lua116
-rw-r--r--examples/resources/lib/bounding_box.lua (renamed from examples/resources/lib/boundingBox.lua)107
-rw-r--r--include/models.h1
-rw-r--r--src/lua_core.c1
-rw-r--r--src/main.c2
-rw-r--r--src/models.c118
-rw-r--r--src/rgui.c2
10 files changed, 359 insertions, 5 deletions
diff --git a/API.md b/API.md
index fc19591..b6479ac 100644
--- a/API.md
+++ b/API.md
@@ -7989,6 +7989,14 @@ Get collision info between ray and quad
---
+> cells = 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
+
+- Success return Vector3{}
+
+---
+
## Audio - Audio device management functions
---
diff --git a/ReiLua_API.lua b/ReiLua_API.lua
index 5a69d0e..2038d70 100644
--- a/ReiLua_API.lua
+++ b/ReiLua_API.lua
@@ -5137,6 +5137,14 @@ function RL.GetRayCollisionTriangle( ray, p1, p2, p3 ) end
---@return any rayCollision
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
+---- Success return Vector3{}
+---@param ray any
+---@param box any
+---@param cellSize table
+---@return any cells
+function RL.GetRayBoxCells( ray, box, cellSize ) end
+
-- Audio - Audio device management functions
---Initialize audio device and context
diff --git a/changelog b/changelog
index e78d73e..3de5a02 100644
--- a/changelog
+++ b/changelog
@@ -66,6 +66,7 @@ DETAILED CHANGES:
- ADDED: Many Gui controls accept now nil for text.
- ADDED: Some raygui controls return 1 instead of 0 when pressed or scrolled.
- ADDED: DrawGridEx.
+ - ADDED: GetRayBoxCells.
------------------------------------------------------------------------
Release: ReiLua version 0.7.0 Using Raylib 5.0 and Forked Raygui 4.0
diff --git a/examples/ray_box_cells/main.lua b/examples/ray_box_cells/main.lua
new file mode 100644
index 0000000..9c7e54f
--- /dev/null
+++ b/examples/ray_box_cells/main.lua
@@ -0,0 +1,116 @@
+package.path = package.path..";"..RL.GetBasePath().."../resources/lib/?.lua"
+
+Util = require( "utillib" )
+Vector2 = require( "vector2" )
+Vector3 = require( "vector3" )
+Rectangle = require( "rectangle" )
+BoundinBox = require( "bounding_box" )
+Cam3D = require( "camera3d" )
+
+local monitor = 0
+local camera = {}
+
+local box = BoundinBox:new( { -8, 0, -8 }, { 16, 16, 16 } )
+local cellSize = Vector3:new( 1, 1, 1 )
+local drawCellSize = cellSize:clone()
+local ray = nil
+local rayCol = nil
+local cells = {}
+
+local guiMouseHover = false
+local spinnerEdit = {
+ x = false,
+ y = false,
+ z = false
+}
+
+function RL.init()
+ local mPos = Vector2:newT( RL.GetMonitorPosition( monitor ) )
+ local mSize = Vector2:newT( RL.GetMonitorSize( monitor ) )
+ local winSize = Vector2:new( 1028, 720 )
+
+ RL.SetWindowTitle( "Ray box cells" )
+ RL.SetWindowState( RL.FLAG_WINDOW_RESIZABLE )
+ RL.SetWindowState( RL.FLAG_VSYNC_HINT )
+ RL.SetWindowSize( winSize )
+ RL.SetWindowPosition( { mPos.x + mSize.x / 2 - winSize.x / 2, mPos.y + mSize.y / 2 - winSize.y / 2 } )
+ RL.SetTextLineSpacing( 26 )
+
+ camera = Cam3D:new()
+
+ camera:setPosition( { 0, 8, 16 } )
+ camera:setTarget( { 0, 0, 0 } )
+ camera:setUp( { 0, 1, 0 } )
+ camera.mode = camera.MODES.FREE
+
+ RL.GuiSetStyle( RL.DEFAULT, RL.TEXT_SIZE, 24 )
+end
+
+function RL.update( delta )
+ camera:update( delta )
+
+ if RL.IsKeyPressed( RL.KEY_SPACE ) then
+ if camera.mode == camera.MODES.FREE then
+ camera.mode = camera.MODES.FIRST_PERSON
+ else
+ camera.mode = camera.MODES.FREE
+ end
+ end
+
+ -- Raycast.
+
+ -- if not guiMouseHover then
+ if not guiMouseHover and RL.IsMouseButtonPressed( RL.MOUSE_BUTTON_LEFT ) then
+ ray = RL.GetMouseRay( RL.GetMousePosition(), camera.camera )
+ rayCol = box:getRayCollision( ray )
+
+ cells = RL.GetRayBoxCells( ray, box:maxToPos(), cellSize )
+ drawCellSize:setV( cellSize )
+ end
+end
+
+local function drawSpinner( axis, pos )
+ local result = 0
+ local bounds = Rectangle:temp( pos.x, pos.y, 96, 24 )
+
+ result, cellSize[ axis ] = RL.GuiSpinner( bounds, axis, cellSize[ axis ], 1, box.max[ axis ], spinnerEdit[ axis ] )
+
+ if result == 1 then
+ spinnerEdit[ axis ] = not spinnerEdit[ axis ]
+ end
+
+ if bounds:checkCollisionPoint( RL.GetMousePosition() ) then
+ guiMouseHover = true
+ end
+end
+
+function RL.draw()
+ RL.ClearBackground( RL.LIGHTGRAY )
+
+ camera:beginMode3D()
+ RL.DrawGrid( 16, 1 )
+ RL.DrawCubeWires( box.min + box.max:scale( 0.5 ), box.max, RL.WHITE )
+
+ if ray then
+ RL.DrawRay( ray, RL.BLUE )
+ RL.DrawSphere( ray[1], 0.1, RL.GREEN )
+ end
+
+ if rayCol and rayCol.hit then
+ RL.DrawSphere( rayCol.point, 0.1, RL.RED )
+ end
+
+ if cells then
+ for _, cell in ipairs( cells ) do
+ RL.DrawCubeWires( box.min + Vector3:tempT( cell ) * drawCellSize + drawCellSize:scale( 0.5 ), drawCellSize, RL.RED )
+ RL.DrawCube( box.min + Vector3:tempT( cell ) * drawCellSize + drawCellSize:scale( 0.5 ), drawCellSize, { 150, 200, 150, 150 } )
+ end
+ end
+ camera:endMode3D()
+
+ guiMouseHover = false
+
+ drawSpinner( "x", Vector2:temp( 16, 2 ) )
+ drawSpinner( "y", Vector2:temp( 16, 22 ) )
+ drawSpinner( "z", Vector2:temp( 16, 44 ) )
+end
diff --git a/examples/resources/lib/boundingBox.lua b/examples/resources/lib/bounding_box.lua
index cc29fab..3cf6787 100644
--- a/examples/resources/lib/boundingBox.lua
+++ b/examples/resources/lib/bounding_box.lua
@@ -3,7 +3,7 @@ if table.unpack == nil then
table.unpack = unpack
end
-local Vector3 = Vector3 or require( "vector3" )
+local Vector3 = require( "vector3" )
local BoundingBox = {}
local metatable = {
@@ -12,6 +12,30 @@ local metatable = {
return "{{"..tostring( b.min.x )..", "..tostring( b.min.y )..", "..tostring( b.min.z ).."}, {"
..tostring( b.max.x )..", "..tostring( b.max.y )..", "..tostring( b.max.z ).."}}"
end,
+ -- __add = function( v1, v2 )
+ -- return Vector2:new( v1.x + v2.x, v1.y + v2.y )
+ -- end,
+ -- __sub = function( v1, v2 )
+ -- return Vector2:new( v1.x - v2.x, v1.y - v2.y )
+ -- end,
+ -- __mul = function( v1, v2 )
+ -- return Vector2:new( v1.x * v2.x, v1.y * v2.y )
+ -- end,
+ -- __div = function( v1, v2 )
+ -- return Vector2:new( v1.x / v2.x, v1.y / v2.y )
+ -- end,
+ -- __mod = function( v, value )
+ -- return Vector2:new( math.fmod( v.x, value ), math.fmod( v.y, value ) )
+ -- end,
+ -- __pow = function( v, value )
+ -- return Vector2:new( v.x ^ value, v.y ^ value )
+ -- end,
+ -- __unm = function( v )
+ -- return Vector2:new( -v.x, -v.y )
+ -- end,
+ __eq = function( b1, b2 )
+ return b1.min == b2.min and b1.max == b2.max
+ end,
}
--- Expects format { ... }, { ... }
@@ -111,26 +135,103 @@ function BoundingBox:getPoints()
}
end
+-- Assumes max is used as size.
function BoundingBox:checkCollisionBox( b )
return RL.CheckCollisionBoxes( self:maxToPos(), b:maxToPos() )
end
+-- Assumes max is used as size.
function BoundingBox:checkCollisionSphere( center, radius )
return RL.CheckCollisionBoxSphere( self:maxToPos(), center, radius )
end
+-- Assumes max is used as size.
+function BoundingBox:checkCollisionPoint( point )
+ local max = self.min + self.max
+
+ return self.min.x <= point.x
+ and self.min.y <= point.y
+ and self.min.z <= point.z
+ and point.x <= max.x
+ and point.y <= max.y
+ and point.z <= max.z
+end
+
+-- Assumes max is used as size.
function BoundingBox:getRayCollision( ray )
return RL.GetRayCollisionBox( ray, self:maxToPos() )
end
-- Max to position from size.
function BoundingBox:maxToPos()
- return BoundingBox:new( self.min, self.min + self.max )
+ return BoundingBox:newV( self.min, self.min + self.max )
end
-- Max to size from position.
function BoundingBox:maxToSize()
- return BoundingBox:new( self.min, self.max - self.min )
+ return BoundingBox:newV( self.min, self.max - self.min )
+end
+
+-- Temp pre generated objects to avoid "slow" table generation.
+
+local TEMP_COUNT = 100
+local tempPool = {}
+local curTemp = 1
+
+for _ = 1, TEMP_COUNT do
+ table.insert( tempPool, BoundingBox:new( { 0, 0, 0 }, { 0, 0, 0 } ) )
+end
+
+--- Expects format { ... }, { ... }
+function BoundingBox:temp( min, max )
+ local object = tempPool[ curTemp ]
+
+ curTemp = curTemp < TEMP_COUNT and curTemp + 1 or 1
+
+ object.min = Vector3:tempT( min )
+ object.max = Vector3:tempT( max )
+
+ return object
+end
+
+--- Expects format { { ... }, { ... } }
+function BoundingBox:tempT( t )
+ local object = tempPool[ curTemp ]
+
+ curTemp = curTemp < TEMP_COUNT and curTemp + 1 or 1
+
+ object.min = Vector3:tempT( t[1] )
+ object.max = Vector3:tempT( t[2] )
+
+ return object
+end
+
+--- Expects format { Vector3, Vector3 }
+function BoundingBox:tempV( min, max )
+ local object = tempPool[ curTemp ]
+
+ curTemp = curTemp < TEMP_COUNT and curTemp + 1 or 1
+
+ object.min = Vector3:tempV( min )
+ object.max = Vector3:tempV( max )
+
+ return object
+end
+
+--- Expects format BoundingBox
+function BoundingBox:tempB( b )
+ local object = tempPool[ curTemp ]
+
+ curTemp = curTemp < TEMP_COUNT and curTemp + 1 or 1
+
+ object.min = Vector3:tempV( b.min )
+ object.max = Vector3:tempV( b.max )
+
+ return object
+end
+
+function BoundingBox:getTempId()
+ return curTemp
end
return BoundingBox
diff --git a/include/models.h b/include/models.h
index 6b35c43..01c560e 100644
--- a/include/models.h
+++ b/include/models.h
@@ -120,3 +120,4 @@ int lmodelsGetRayCollisionBox( lua_State* L );
int lmodelsGetRayCollisionMesh( lua_State* L );
int lmodelsGetRayCollisionTriangle( lua_State* L );
int lmodelsGetRayCollisionQuad( lua_State* L );
+int lmodelsGetRayBoxCells( lua_State* L );
diff --git a/src/lua_core.c b/src/lua_core.c
index 2d5ddcc..8071a0f 100644
--- a/src/lua_core.c
+++ b/src/lua_core.c
@@ -1819,6 +1819,7 @@ void luaRegister() {
assingGlobalFunction( "GetRayCollisionMesh", lmodelsGetRayCollisionMesh );
assingGlobalFunction( "GetRayCollisionTriangle", lmodelsGetRayCollisionTriangle );
assingGlobalFunction( "GetRayCollisionQuad", lmodelsGetRayCollisionQuad );
+ assingGlobalFunction( "GetRayBoxCells", lmodelsGetRayBoxCells );
/* Text. */
/* Font loading/unloading functions. */
diff --git a/src/main.c b/src/main.c
index 95184cf..e4fb569 100644
--- a/src/main.c
+++ b/src/main.c
@@ -2,7 +2,7 @@
#include "state.h"
#include "lua_core.h"
-inline static void printVersion() {
+static inline void printVersion() {
if ( VERSION_DEV ) {
TraceLog( LOG_INFO, "ReiLua %d.%d.%d-Dev", VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH );
}
diff --git a/src/models.c b/src/models.c
index 652a6c0..f16620c 100644
--- a/src/models.c
+++ b/src/models.c
@@ -2531,3 +2531,121 @@ int lmodelsGetRayCollisionQuad( lua_State* L ) {
return 1;
}
+
+static inline Vector3 Vector3Floor( Vector3 v ) {
+ return (Vector3){ (float)floor( v.x ), (float)floor( v.y ), (float)floor( v.z ) };
+}
+
+static inline Vector3 Vector3Ceil( Vector3 v ) {
+ return (Vector3){ (float)ceil( v.x ), (float)ceil( v.y ), (float)ceil( v.z ) };
+}
+
+static inline bool isInsideBox( Vector3 position, BoundingBox box ) {
+ return box.min.x <= position.x && position.x <= box.max.x
+ && box.min.y <= position.y && position.y <= box.max.y
+ && box.min.z <= position.z && position.z <= box.max.z;
+}
+
+/*
+> cells = 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
+
+- Success return Vector3{}
+*/
+int lmodelsGetRayBoxCells( lua_State* L ) {
+ Ray ray = uluaGetRay( L, 1 );
+ BoundingBox box = uluaGetBoundingBox( L, 2 );
+ Vector3 cellSize = uluaGetVector3( L, 3 );
+
+ /* To avoid possible div by 0 later. */
+ ray.direction.x == 0.0f ? EPSILON : ray.direction.x;
+ ray.direction.y == 0.0f ? EPSILON : ray.direction.y;
+ ray.direction.z == 0.0f ? EPSILON : ray.direction.z;
+
+ Vector3 boxSize = Vector3Subtract( box.max, box.min );
+ Vector3 boxSizeCells = Vector3Ceil( Vector3Divide( boxSize, cellSize ) );
+ Vector3 cellPos = { -1, -1, -1 };
+ Vector3 localRayPos = { 0, 0, 0 };
+
+ /* If camera is inside the box. */
+ if ( isInsideBox( ray.position, box ) ) {
+ localRayPos = Vector3Subtract( ray.position, box.min );
+ /* Nudge position a bit to try to get if away from {0, 0, 0}. */
+ localRayPos = Vector3Add( localRayPos, Vector3Scale( ray.direction, 0.0001f ) );
+ cellPos = Vector3Floor( Vector3Divide( localRayPos, cellSize ) );
+ }
+ /* Else check ray to box to see where we start. */
+ else {
+ RayCollision rayCol = GetRayCollisionBox( ray, box );
+
+ if ( rayCol.hit ) {
+ localRayPos = Vector3Subtract( rayCol.point, box.min );
+ /* Nudge inside the box. */
+ localRayPos = Vector3Add( localRayPos, Vector3Scale( Vector3Negate( rayCol.normal ), 0.0001f ) );
+ cellPos = Vector3Floor( Vector3Divide( localRayPos, cellSize ) );
+ }
+ }
+ lua_newtable( L );
+
+ /* Find cells along the ray. */
+ if ( 0 <= cellPos.x ) {
+ uluaPushVector3( L, cellPos );
+ lua_rawseti( L, -2, 1 );
+
+ Vector3 signs = {
+ 0.0f <= ray.direction.x ? 1.0f : -1.0f,
+ 0.0f <= ray.direction.y ? 1.0f : -1.0f,
+ 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 ),
+ fabsf( ray.direction.z )
+ };
+ Vector3 absPos = {
+ 0.0f < signs.x ? localRayPos.x - cellPos.x * cellSize.x : cellSize.x - ( localRayPos.x - cellPos.x * cellSize.x ),
+ 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 absCell = { 0, 0, 0 };
+ int cellId = 2; /* We already added first so we will start at 2. */
+
+ while ( true ) {
+ /* Distance to adjacent cell. */
+ Vector3 cellDis = {
+ ( cellSize.x - ( absPos.x - absCell.x * cellSize.x ) ) / absDir.x,
+ ( cellSize.y - ( absPos.y - absCell.y * cellSize.y ) ) / absDir.y,
+ ( cellSize.z - ( absPos.z - absCell.z * cellSize.z ) ) / absDir.z,
+ };
+ Vector3 move = {
+ cellDis.x <= cellDis.y && cellDis.x <= cellDis.z ? 1 : 0,
+ cellDis.y <= cellDis.x && cellDis.y <= cellDis.z ? 1 : 0,
+ cellDis.z <= cellDis.x && cellDis.z <= cellDis.y ? 1 : 0
+ };
+ /* Both relative and real cell pos needs to be moved. */
+ absCell = Vector3Add( absCell, move );
+ cellPos = Vector3Add( cellPos, Vector3Multiply( move, signs ) );
+
+ if ( absCell.x < absBounds.x && absCell.y < absBounds.y && absCell.z < absBounds.z ) {
+ absPos = Vector3Add( absPos, Vector3Scale( absDir, fmin( fmin( cellDis.x, cellDis.y ), cellDis.z ) ) );
+
+ uluaPushVector3( L, (Vector3){ round( cellPos.x ), round( cellPos.y ), round( cellPos.z ) } );
+ lua_rawseti( L, -2, cellId );
+
+ cellId++;
+ }
+ else {
+ break;
+ }
+ }
+ }
+
+ return 1;
+}
diff --git a/src/rgui.c b/src/rgui.c
index a17c096..0a4152d 100644
--- a/src/rgui.c
+++ b/src/rgui.c
@@ -7,7 +7,7 @@
#define RAYGUI_IMPLEMENTATION
#include "raygui.h"
-inline char* getTextOrNil( lua_State* L, int index ) {
+static inline char* getTextOrNil( lua_State* L, int index ) {
char* text = NULL;
if ( lua_isstring( L, index ) ) {