From 03e9226b5f6c3fe4d113759b3a023ee92d7b2d4f Mon Sep 17 00:00:00 2001 From: jussi Date: Fri, 19 Jul 2024 22:57:31 +0300 Subject: GetRayBoxCells. --- examples/ray_box_cells/main.lua | 116 ++++++++++++++++ examples/resources/lib/boundingBox.lua | 136 ------------------ examples/resources/lib/bounding_box.lua | 237 ++++++++++++++++++++++++++++++++ 3 files changed, 353 insertions(+), 136 deletions(-) create mode 100644 examples/ray_box_cells/main.lua delete mode 100644 examples/resources/lib/boundingBox.lua create mode 100644 examples/resources/lib/bounding_box.lua (limited to 'examples') 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/boundingBox.lua deleted file mode 100644 index cc29fab..0000000 --- a/examples/resources/lib/boundingBox.lua +++ /dev/null @@ -1,136 +0,0 @@ --- For luaJit compatibility. -if table.unpack == nil then - table.unpack = unpack -end - -local Vector3 = Vector3 or require( "vector3" ) - -local BoundingBox = {} -local metatable = { - __index = BoundingBox, - __tostring = function( b ) - 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, -} - ---- Expects format { ... }, { ... } -function BoundingBox:new( min, max ) - local object = setmetatable( {}, metatable ) - - object.min = Vector3:newT( min ) - object.max = Vector3:newT( max ) - - return object -end - ---- Expects format { { ... }, { ... } } -function BoundingBox:newT( t ) - local object = setmetatable( {}, metatable ) - - object.min = Vector3:newT( t[1] ) - object.max = Vector3:newT( t[2] ) - - return object -end - ---- Expects format { Vector3, Vector3 } -function BoundingBox:newV( min, max ) - local object = setmetatable( {}, metatable ) - - object.min = min:clone() - object.max = max:clone() - - return object -end - ---- Expects format BoundingBox -function BoundingBox:newB( b ) - local object = setmetatable( {}, metatable ) - - object.min = b.min:clone() - object.max = b.max:clone() - - return object -end - -function BoundingBox:serialize() - return table.concat( { "BoundingBox:new({", self.min.x, ",", self.min.y, ",", self.min.z, "},{", self.max.x, ",", self.max.y, ",", self.max.z, "})" } ) -end - ---- Expects format { ... }, { ... } -function BoundingBox:set( min, max ) - self.min:setT( min ) - self.max:setT( max ) -end - ---- Expects format { { ... }, { ... } } -function BoundingBox:setT( t ) - self.min:setT( t[1] ) - self.max:setT( t[2] ) -end - ---- Expects format { Vector3, Vector3 } -function BoundingBox:setV( min, max ) - self.min:setV( min ) - self.max:setV( max ) -end - ---- Expects format BoundingBox -function BoundingBox:setB( b ) - self.min:setV( b.min ) - self.max:setV( b.max ) -end - -function BoundingBox:arr() - return { { self.min.x, self.min.y, self.min.z }, { self.max.x, self.max.y, self.max.z } } -end - -function BoundingBox:unpack() - return self.min, self.max -end - -function BoundingBox:clone() - return BoundingBox:newB( self ) -end - -function BoundingBox:scale( scalar ) - return BoundingBox:newV( self.min, self.max:scale( scalar ) ) -end - -function BoundingBox:getPoints() - return { - self.min:clone(), -- Down back left. - Vector3:new( self.max.x, self.min.y, self.min.z ), -- Down back right. - Vector3:new( self.min.x, self.min.y, self.max.z ), -- Down front left. - Vector3:new( self.max.x, self.min.y, self.max.z ), -- Down front right. - Vector3:new( self.min.x, self.max.y, self.min.z ), -- Up back left. - Vector3:new( self.max.x, self.max.y, self.min.z ), -- Up back right. - Vector3:new( self.min.x, self.max.y, self.max.z ), -- Up front left. - self.max:clone(), -- Up front right. - } -end - -function BoundingBox:checkCollisionBox( b ) - return RL.CheckCollisionBoxes( self:maxToPos(), b:maxToPos() ) -end - -function BoundingBox:checkCollisionSphere( center, radius ) - return RL.CheckCollisionBoxSphere( self:maxToPos(), center, radius ) -end - -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 ) -end - --- Max to size from position. -function BoundingBox:maxToSize() - return BoundingBox:new( self.min, self.max - self.min ) -end - -return BoundingBox diff --git a/examples/resources/lib/bounding_box.lua b/examples/resources/lib/bounding_box.lua new file mode 100644 index 0000000..3cf6787 --- /dev/null +++ b/examples/resources/lib/bounding_box.lua @@ -0,0 +1,237 @@ +-- For luaJit compatibility. +if table.unpack == nil then + table.unpack = unpack +end + +local Vector3 = require( "vector3" ) + +local BoundingBox = {} +local metatable = { + __index = BoundingBox, + __tostring = function( b ) + 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 { ... }, { ... } +function BoundingBox:new( min, max ) + local object = setmetatable( {}, metatable ) + + object.min = Vector3:newT( min ) + object.max = Vector3:newT( max ) + + return object +end + +--- Expects format { { ... }, { ... } } +function BoundingBox:newT( t ) + local object = setmetatable( {}, metatable ) + + object.min = Vector3:newT( t[1] ) + object.max = Vector3:newT( t[2] ) + + return object +end + +--- Expects format { Vector3, Vector3 } +function BoundingBox:newV( min, max ) + local object = setmetatable( {}, metatable ) + + object.min = min:clone() + object.max = max:clone() + + return object +end + +--- Expects format BoundingBox +function BoundingBox:newB( b ) + local object = setmetatable( {}, metatable ) + + object.min = b.min:clone() + object.max = b.max:clone() + + return object +end + +function BoundingBox:serialize() + return table.concat( { "BoundingBox:new({", self.min.x, ",", self.min.y, ",", self.min.z, "},{", self.max.x, ",", self.max.y, ",", self.max.z, "})" } ) +end + +--- Expects format { ... }, { ... } +function BoundingBox:set( min, max ) + self.min:setT( min ) + self.max:setT( max ) +end + +--- Expects format { { ... }, { ... } } +function BoundingBox:setT( t ) + self.min:setT( t[1] ) + self.max:setT( t[2] ) +end + +--- Expects format { Vector3, Vector3 } +function BoundingBox:setV( min, max ) + self.min:setV( min ) + self.max:setV( max ) +end + +--- Expects format BoundingBox +function BoundingBox:setB( b ) + self.min:setV( b.min ) + self.max:setV( b.max ) +end + +function BoundingBox:arr() + return { { self.min.x, self.min.y, self.min.z }, { self.max.x, self.max.y, self.max.z } } +end + +function BoundingBox:unpack() + return self.min, self.max +end + +function BoundingBox:clone() + return BoundingBox:newB( self ) +end + +function BoundingBox:scale( scalar ) + return BoundingBox:newV( self.min, self.max:scale( scalar ) ) +end + +function BoundingBox:getPoints() + return { + self.min:clone(), -- Down back left. + Vector3:new( self.max.x, self.min.y, self.min.z ), -- Down back right. + Vector3:new( self.min.x, self.min.y, self.max.z ), -- Down front left. + Vector3:new( self.max.x, self.min.y, self.max.z ), -- Down front right. + Vector3:new( self.min.x, self.max.y, self.min.z ), -- Up back left. + Vector3:new( self.max.x, self.max.y, self.min.z ), -- Up back right. + Vector3:new( self.min.x, self.max.y, self.max.z ), -- Up front left. + self.max:clone(), -- Up front right. + } +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:newV( self.min, self.min + self.max ) +end + +-- Max to size from position. +function BoundingBox:maxToSize() + 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 -- cgit v1.2.3