1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
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 box = BoundinBox:new( { -7, 1, -8 }, { 4, 1, 7 } )
local cellSize = Vector3:new( 1, 1, 1 )
local drawCellSize = cellSize:clone()
local ray = nil
local rayCol = nil
local cells = {}
local exitPoint = {}
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 and RL.IsMouseButtonPressed( RL.MOUSE_BUTTON_LEFT ) then
ray = RL.GetMouseRay( RL.GetMousePosition(), camera.camera )
rayCol = box:getRayCollisionMAS( ray )
cells, exitPoint = 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 exitPoint and exitPoint.hit then
RL.DrawSphere( exitPoint.point, 0.1, RL.YELLOW )
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
|