GetRayBoxCells fix.

This commit is contained in:
jussi
2024-08-05 23:35:57 +03:00
parent c6eb85b367
commit b011b2ca4e
16 changed files with 126 additions and 60 deletions

View File

@@ -116,6 +116,8 @@ https://github.com/raysan5/raylib
https://github.com/lua/lua or https://github.com/LuaJIT/LuaJIT https://github.com/lua/lua or https://github.com/LuaJIT/LuaJIT
Note! Lua header files are from Lua 5.4.0, if you use different version be sure to replace them.
### Linux ### Linux
Compile Raylib and lua by following their instructions. They will compile to libraylib.a and liblua.a by default. Compile Raylib and lua by following their instructions. They will compile to libraylib.a and liblua.a by default.

View File

@@ -67,6 +67,7 @@ DETAILED CHANGES:
- ADDED: Some raygui controls return 1 instead of 0 when pressed or scrolled. - ADDED: Some raygui controls return 1 instead of 0 when pressed or scrolled.
- ADDED: DrawGridEx. - ADDED: DrawGridEx.
- ADDED: GetRayBoxCells. - ADDED: GetRayBoxCells.
- FIXED: GenImageColor color was also argument 1.
------------------------------------------------------------------------ ------------------------------------------------------------------------
Release: ReiLua version 0.7.0 Using Raylib 5.0 and Forked Raygui 4.0 Release: ReiLua version 0.7.0 Using Raylib 5.0 and Forked Raygui 4.0

View File

@@ -19,15 +19,17 @@ Backlog {
* Improve Dungeon crawler example by generating custom mesh instead of drawing 3D quads. * Improve Dungeon crawler example by generating custom mesh instead of drawing 3D quads.
* Platformer example physics update for true framerate independence. * Platformer example physics update for true framerate independence.
* Android support * Android support
* For raylib 5.5
* DrawBillboardPro BREAKING CHANGE.
} }
Bugs { Bugs {
* glfwSet*Callback functions segfault on Windows. Should keep Lua events off for now. * glfwSet*Callback functions segfault on Windows. Should keep Lua events off for now.
} }
Notes {
* raylib 5.5
* DrawBillboardPro BREAKING CHANGE.
}
Needs Testing { Needs Testing {
* rlSetUniform * rlSetUniform
* rlSetShader * rlSetShader

View File

@@ -10,7 +10,8 @@ Cam3D = require( "camera3d" )
local monitor = 0 local monitor = 0
local camera = {} local camera = {}
local box = BoundinBox:new( { -8, 0, -8 }, { 16, 16, 16 } ) -- local box = BoundinBox:new( { -8, 0, -8 }, { 16, 16, 16 } )
local box = BoundinBox:new( { -7, 1, -8 }, { 4, 1, 7 } )
local cellSize = Vector3:new( 1, 1, 1 ) local cellSize = Vector3:new( 1, 1, 1 )
local drawCellSize = cellSize:clone() local drawCellSize = cellSize:clone()
local ray = nil local ray = nil
@@ -60,10 +61,9 @@ function RL.update( delta )
-- Raycast. -- Raycast.
-- if not guiMouseHover then
if not guiMouseHover and RL.IsMouseButtonPressed( RL.MOUSE_BUTTON_LEFT ) then if not guiMouseHover and RL.IsMouseButtonPressed( RL.MOUSE_BUTTON_LEFT ) then
ray = RL.GetMouseRay( RL.GetMousePosition(), camera.camera ) ray = RL.GetMouseRay( RL.GetMousePosition(), camera.camera )
rayCol = box:getRayCollision( ray ) rayCol = box:getRayCollisionMAS( ray )
cells, exitPoint = RL.GetRayBoxCells( ray, box:maxToPos(), cellSize ) cells, exitPoint = RL.GetRayBoxCells( ray, box:maxToPos(), cellSize )
drawCellSize:setV( cellSize ) drawCellSize:setV( cellSize )
@@ -99,8 +99,8 @@ function RL.draw()
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 if exitPoint and exitPoint.hit then
RL.DrawSphere( exitPoint.point, 0.1, RL.RED ) RL.DrawSphere( exitPoint.point, 0.1, RL.YELLOW )
end end
if cells then if cells then
for _, cell in ipairs( cells ) do for _, cell in ipairs( cells ) do

View File

@@ -3,7 +3,7 @@ if table.unpack == nil then
table.unpack = unpack table.unpack = unpack
end end
local Vector3 = require( "vector3" ) local Vector3 = Vector3 or require( "vector3" )
local BoundingBox = {} local BoundingBox = {}
local metatable = { local metatable = {
@@ -135,31 +135,25 @@ function BoundingBox:getPoints()
} }
end end
-- Assumes max is used as size.
function BoundingBox:checkCollisionBox( b ) function BoundingBox:checkCollisionBox( b )
return RL.CheckCollisionBoxes( self:maxToPos(), b:maxToPos() ) return RL.CheckCollisionBoxes( self, b )
end end
-- Assumes max is used as size.
function BoundingBox:checkCollisionSphere( center, radius ) function BoundingBox:checkCollisionSphere( center, radius )
return RL.CheckCollisionBoxSphere( self:maxToPos(), center, radius ) return RL.CheckCollisionBoxSphere( self, center, radius )
end end
-- Assumes max is used as size.
function BoundingBox:checkCollisionPoint( point ) function BoundingBox:checkCollisionPoint( point )
local max = self.min + self.max
return self.min.x <= point.x return self.min.x <= point.x
and self.min.y <= point.y and self.min.y <= point.y
and self.min.z <= point.z and self.min.z <= point.z
and point.x <= max.x and point.x <= self.max.x
and point.y <= max.y and point.y <= self.max.y
and point.z <= max.z and point.z <= self.max.z
end end
-- Assumes max is used as size.
function BoundingBox:getRayCollision( ray ) function BoundingBox:getRayCollision( ray )
return RL.GetRayCollisionBox( ray, self:maxToPos() ) return RL.GetRayCollisionBox( ray, self )
end end
-- Max to position from size. -- Max to position from size.
@@ -172,6 +166,36 @@ function BoundingBox:maxToSize()
return BoundingBox:newV( self.min, self.max - self.min ) return BoundingBox:newV( self.min, self.max - self.min )
end end
-- MAS stands for max as size. These functions handles max as size instead of position.
function BoundingBox:checkCollisionBoxMASBox( b )
return RL.CheckCollisionBoxes( self:tempMaxToPos(), b )
end
function BoundingBox:checkCollisionBoxMAS( b )
return RL.CheckCollisionBoxes( self:tempMaxToPos(), b:tempMaxToPos() )
end
function BoundingBox:checkCollisionSphereMAS( center, radius )
return RL.CheckCollisionBoxSphere( self:tempMaxToPos(), center, radius )
end
function BoundingBox:checkCollisionPointMAS( 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
function BoundingBox:getRayCollisionMAS( ray )
return RL.GetRayCollisionBox( ray, self:tempMaxToPos() )
-- return RL.GetRayCollisionBox( ray, self:maxToPos() )
end
-- Temp pre generated objects to avoid "slow" table generation. -- Temp pre generated objects to avoid "slow" table generation.
local TEMP_COUNT = 100 local TEMP_COUNT = 100
@@ -230,6 +254,16 @@ function BoundingBox:tempB( b )
return object return object
end end
-- Max to position from size.
function BoundingBox:tempMaxToPos()
return BoundingBox:tempV( self.min, Vector3:tempT( RL.Vector3Add( self.min, self.max ) ) )
end
-- Max to size from position.
function BoundingBox:tempMaxToSize()
return BoundingBox:tempV( self.min, Vector3:tempT( RL.Vector3Subtract( self.max, self.min ) ) )
end
function BoundingBox:getTempId() function BoundingBox:getTempId()
return curTemp return curTemp
end end

View File

@@ -3,8 +3,6 @@ if table.unpack == nil then
table.unpack = unpack table.unpack = unpack
end end
local Vector3 = require( "vector3" )
local Color = {} local Color = {}
local metatable = { local metatable = {
__index = Color, __index = Color,
@@ -92,7 +90,7 @@ function Color:setC( c )
end end
function Color:serialize() function Color:serialize()
return "Color:new("..self.r..","..self.g..","..self.b..","..self.a..")" return "Color:new("..RL.Round( self.r )..","..RL.Round( self.g )..","..RL.Round( self.b )..","..RL.Round( self.a )..")"
end end
function Color:arr() function Color:arr()
@@ -156,12 +154,12 @@ function Color:alphaBlend( dst, src, tint )
end end
function Color:lerp( color, amount ) function Color:lerp( color, amount )
return Color:new( return Color:temp(
RL.Lerp( self.r, color.r, amount ), RL.Lerp( self.r, color.r, amount ),
RL.Lerp( self.g, color.g, amount ), RL.Lerp( self.g, color.g, amount ),
RL.Lerp( self.b, color.b, amount ), RL.Lerp( self.b, color.b, amount ),
RL.Lerp( self.a, color.a, amount ) RL.Lerp( self.a, color.a, amount )
) ):round()
end end
function Color:round() function Color:round()
@@ -173,6 +171,10 @@ function Color:round()
) )
end end
function Color:invert()
return Color:new( 255 - self.r, 255 - self.g, 255 - self.b, self.a )
end
-- Temp pre generated objects to avoid "slow" table generation. -- Temp pre generated objects to avoid "slow" table generation.
local TEMP_COUNT = 100 local TEMP_COUNT = 100

View File

@@ -1729,7 +1729,7 @@ function Raygui:update()
for i = #self.controls, 1, -1 do for i = #self.controls, 1, -1 do
local control = self.controls[i] local control = self.controls[i]
if control.visible and control.update ~= nil and self:inView( control ) then if control.visible and not control.noUpdate and control.update ~= nil and self:inView( control ) then
if control:update() then if control:update() then
self.focused = i self.focused = i

View File

@@ -36,6 +36,12 @@ local metatable = {
__eq = function( v1, v2 ) __eq = function( v1, v2 )
return RL.Vector2Equals( v1, v2 ) return RL.Vector2Equals( v1, v2 )
end, end,
__lt = function( v1, v2 )
return v1.x < v2.x and v1.y < v2.y
end,
__le = function( v1, v2 )
return v1.x <= v2.x and v1.y <= v2.y
end,
__concat = function( a, b ) __concat = function( a, b )
return tostring( a )..tostring( b ) return tostring( a )..tostring( b )
end, end,
@@ -205,6 +211,10 @@ function Vector2:equals( v2 )
return RL.Vector2Equals( self, v2 ) return RL.Vector2Equals( self, v2 )
end end
function Vector2:sign()
return Vector2:new( RL.Sign( self.x ), RL.Sign( self.y ) )
end
function Vector2:addEq( v2 ) function Vector2:addEq( v2 )
self.x = self.x + v2.x self.x = self.x + v2.x
self.y = self.y + v2.y self.y = self.y + v2.y

View File

@@ -3,8 +3,6 @@ if table.unpack == nil then
table.unpack = unpack table.unpack = unpack
end end
local Vector2 = Vector2 or require( "vector2" )
local Vector3 = {} local Vector3 = {}
local metatable = { local metatable = {
__index = Vector3, __index = Vector3,
@@ -38,6 +36,12 @@ local metatable = {
__eq = function( v1, v2 ) __eq = function( v1, v2 )
return RL.Vector3Equals( v1, v2 ) return RL.Vector3Equals( v1, v2 )
end, end,
__lt = function( v1, v2 )
return v1.x < v2.x and v1.y < v2.y and v1.z < v2.z
end,
__le = function( v1, v2 )
return v1.x <= v2.x and v1.y <= v2.y and v1.z <= v2.z
end,
__concat = function( a, b ) __concat = function( a, b )
return tostring( a )..tostring( b ) return tostring( a )..tostring( b )
end, end,
@@ -232,6 +236,10 @@ function Vector3:equals( v2 )
return RL.Vector3Equals( self, v2 ) return RL.Vector3Equals( self, v2 )
end end
function Vector3:sign()
return Vector3:new( RL.Sign( self.x ), RL.Sign( self.y ), RL.Sign( self.z ) )
end
function Vector3:addEq( v2 ) function Vector3:addEq( v2 )
self.x = self.x + v2.x self.x = self.x + v2.x
self.y = self.y + v2.y self.y = self.y + v2.y

View File

@@ -13,13 +13,12 @@ out vec4 finalColor;
// NOTE: Add here your custom variables // NOTE: Add here your custom variables
void main() void main() {
{
// Texel color fetching from texture sampler // Texel color fetching from texture sampler
vec4 texelColor = texture(texture0, fragTexCoord); vec4 texelColor = texture( texture0, fragTexCoord );
// NOTE: Implement here your fragment shader code // NOTE: Implement here your fragment shader code
finalColor = texelColor*colDiffuse; finalColor = texelColor * colDiffuse;
} }

View File

@@ -15,12 +15,11 @@ out vec4 fragColor;
// NOTE: Add here your custom variables // NOTE: Add here your custom variables
void main() void main() {
{
// Send vertex attributes to fragment shader // Send vertex attributes to fragment shader
fragTexCoord = vertexTexCoord; fragTexCoord = vertexTexCoord;
fragColor = vertexColor; fragColor = vertexColor;
// Calculate final vertex position // Calculate final vertex position
gl_Position = mvp*vec4(vertexPosition, 1.0); gl_Position = mvp * vec4( vertexPosition, 1.0 );
} }

View File

@@ -37,9 +37,9 @@ typedef struct {
GLFWcursorenterfun raylibCursorEnterCallback; GLFWcursorenterfun raylibCursorEnterCallback;
GLFWjoystickfun raylibJoystickCallback; GLFWjoystickfun raylibJoystickCallback;
/* NOTE! Experimental. Needs glfw PR https://github.com/glfw/glfw/pull/1445 */ /* NOTE! Experimental. Needs glfw PR https://github.com/glfw/glfw/pull/1445 */
GLFWpentabletdatafun glfwtabletDataCallback; GLFWpentabletdatafun glfwTabletDataCallback;
GLFWpentabletcursorfun glfwtabletCursorCallback; GLFWpentabletcursorfun glfwTabletCursorCallback;
GLFWpentabletproximityfun glfwtabletProximityCallback; GLFWpentabletproximityfun glfwTabletProximityCallback;
#elif PLATFORM_DESKTOP_SDL #elif PLATFORM_DESKTOP_SDL
int SDL_eventQueueLen; int SDL_eventQueueLen;
SDL_Event* SDL_eventQueue; SDL_Event* SDL_eventQueue;

View File

@@ -755,6 +755,7 @@ static void defineGlobals() {
assignGlobalColor( RAYWHITE, "RAYWHITE" ); // My own White (raylib logo) assignGlobalColor( RAYWHITE, "RAYWHITE" ); // My own White (raylib logo)
/* Math */ /* Math */
assignGlobalFloat( PI, "PI" ); // Pi assignGlobalFloat( PI, "PI" ); // Pi
assignGlobalFloat( EPSILON, "EPSILON" ); // Epsilon
assignGlobalFloat( DEG2RAD, "DEG2RAD" ); // Degrees to radians assignGlobalFloat( DEG2RAD, "DEG2RAD" ); // Degrees to radians
assignGlobalFloat( RAD2DEG, "RAD2DEG" ); // Radians to degrees assignGlobalFloat( RAD2DEG, "RAD2DEG" ); // Radians to degrees
/* Gui control state */ /* Gui control state */

View File

@@ -2516,7 +2516,7 @@ int lmodelsGetRayCollisionTriangle( lua_State* L ) {
/* /*
> rayCollision = RL.GetRayCollisionQuad( Ray ray, Vector3 p1, Vector3 p2, Vector3 p3, Vector3 p4 ) > 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 - Success return RayCollision
*/ */
@@ -2599,11 +2599,6 @@ int lmodelsGetRayBoxCells( lua_State* L ) {
0.0f <= ray.direction.z ? 1.0f : -1.0f 0.0f <= ray.direction.z ? 1.0f : -1.0f
}; };
/* We transform everything to absolute space to make this simpler. */ /* 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 = { Vector3 absDir = {
fabsf( ray.direction.x ), fabsf( ray.direction.x ),
fabsf( ray.direction.y ), 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.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 ) 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 }; Vector3 absCell = { 0, 0, 0 };
int cellId = 2; /* We already added first so we will start at 2. */ 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 ); float rayMoveScale = fmin( fmin( cellDis.x, cellDis.y ), cellDis.z );
absPos = Vector3Add( absPos, Vector3Scale( absDir, rayMoveScale ) ); 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 ) } ); 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 {
Vector3 exitPoint = Vector3Add( localRayPos, box.min );
uluaPushRayCollision( L, (RayCollision){ uluaPushRayCollision( L, (RayCollision){
.hit = true, .hit = true,
.distance = Vector3Distance( ray.position, exitPoint ), .distance = Vector3Distance( ray.position, exitPoint ),

View File

@@ -99,6 +99,7 @@ int lcoreGetKeyScancode( lua_State* L ) {
Called when the window is resized. Type GLFW_WINDOW_SIZE_EVENT Called when the window is resized. Type GLFW_WINDOW_SIZE_EVENT
*/ */
static void windowSizeEvent( GLFWwindow* window, int width, int height ) { static void windowSizeEvent( GLFWwindow* window, int width, int height ) {
// GLFWwindowsizefun windowSizeEvent( GLFWwindow* window, int width, int height ) {
/* Pass through to raylib callback. */ /* Pass through to raylib callback. */
state->raylibWindowSizeCallback( window, width, height ); state->raylibWindowSizeCallback( window, width, height );
@@ -617,7 +618,7 @@ static void penTabletProximityEvent( int proxState ) {
static void platformRegisterEvents() { static void platformRegisterEvents() {
/* Window events. */ /* Window events. */
state->raylibWindowSizeCallback = glfwSetWindowSizeCallback( GetWindowHandle(), windowSizeEvent ); state->raylibWindowSizeCallback = glfwSetWindowSizeCallback( GetWindowHandle(), (GLFWwindowsizefun)windowSizeEvent );
#if !defined( PLATFORM_WEB ) #if !defined( PLATFORM_WEB )
state->raylibWindowMaximizeCallback = glfwSetWindowMaximizeCallback( GetWindowHandle(), windowMaximizeEvent ); state->raylibWindowMaximizeCallback = glfwSetWindowMaximizeCallback( GetWindowHandle(), windowMaximizeEvent );
#endif #endif
@@ -634,9 +635,9 @@ static void platformRegisterEvents() {
state->raylibCursorEnterCallback = glfwSetCursorEnterCallback( GetWindowHandle(), cursorEnterInputEvent ); state->raylibCursorEnterCallback = glfwSetCursorEnterCallback( GetWindowHandle(), cursorEnterInputEvent );
state->raylibJoystickCallback = glfwSetJoystickCallback( joystickEvent ); state->raylibJoystickCallback = glfwSetJoystickCallback( joystickEvent );
/* NOTE! Experimental. Needs glfw PR https://github.com/glfw/glfw/pull/1445 */ /* NOTE! Experimental. Needs glfw PR https://github.com/glfw/glfw/pull/1445 */
// state->glfwtabletDataCallback = glfwSetPenTabletDataCallback( penTabletDataEvent ); // state->glfwTabletDataCallback = glfwSetPenTabletDataCallback( penTabletDataEvent );
// state->glfwtabletCursorCallback = glfwSetPenTabletCursorCallback( penTabletCursorEvent ); // state->glfwTabletCursorCallback = glfwSetPenTabletCursorCallback( penTabletCursorEvent );
// state->glfwtabletProximityCallback = glfwSetPenTabletProximityCallback( penTabletProximityEvent ); // state->glfwTabletProximityCallback = glfwSetPenTabletProximityCallback( penTabletProximityEvent );
} }
void luaPlatformRegister() { void luaPlatformRegister() {

View File

@@ -255,7 +255,7 @@ Generate image: plain color
*/ */
int ltexturesGenImageColor( lua_State* L ) { int ltexturesGenImageColor( lua_State* L ) {
Vector2 size = uluaGetVector2( L, 1 ); Vector2 size = uluaGetVector2( L, 1 );
Color color = uluaGetColor( L, 1 ); Color color = uluaGetColor( L, 2 );
uluaPushImage( L, GenImageColor( size.x, size.y, color ) ); uluaPushImage( L, GenImageColor( size.x, size.y, color ) );