Compare commits
2 commits
cd3b21d129
...
d36ec336ab
Author | SHA1 | Date | |
---|---|---|---|
|
d36ec336ab | ||
|
ee2064867e |
90
main.lua
90
main.lua
|
@ -1,6 +1,13 @@
|
||||||
-- Bootstrap
|
-- Bootstrap
|
||||||
appName = "lovr-playspace"
|
appName = "lovr-playspace"
|
||||||
|
|
||||||
|
-- Function to get the current time
|
||||||
|
function getCurrentTime()
|
||||||
|
local time = os.time()
|
||||||
|
local date = os.date("*t", time)
|
||||||
|
return string.format("%02d:%02d", date.hour, date.min)
|
||||||
|
end
|
||||||
|
|
||||||
-- App
|
-- App
|
||||||
hands = {"hand/right","hand/left"}
|
hands = {"hand/right","hand/left"}
|
||||||
limbs = {
|
limbs = {
|
||||||
|
@ -32,6 +39,16 @@ function getDistanceBetweenPoints3D(x1, y1, z1, x2, y2, z2)
|
||||||
return math.sqrt((x2 - x1)^2 + (z2 - z1)^2)
|
return math.sqrt((x2 - x1)^2 + (z2 - z1)^2)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function getFloorMatrix()
|
||||||
|
local fx, fy, fz, fangle, fax, fay, faz = lovr.headset.getPose("floor")
|
||||||
|
return lovr.math.newMat4(lovr.math.vec3(fx, fy, fz), lovr.math.vec3(1, 1, 1), lovr.math.quat(fangle, fax, fay, faz))
|
||||||
|
end
|
||||||
|
|
||||||
|
function getHeadMatrix()
|
||||||
|
local fx, fy, fz, fangle, fax, fay, faz = lovr.headset.getPose("head")
|
||||||
|
return lovr.math.newMat4(lovr.math.vec3(fx, fy, fz), lovr.math.vec3(1, 1, 1), lovr.math.quat(fangle, fax, fay, faz))
|
||||||
|
end
|
||||||
|
|
||||||
function getLineDistance(x, _, z, point1, point2) -- Notice the underscore for y
|
function getLineDistance(x, _, z, point1, point2) -- Notice the underscore for y
|
||||||
-- Vector from point1 to point2
|
-- Vector from point1 to point2
|
||||||
local dx = point2[1] - point1[1]
|
local dx = point2[1] - point1[1]
|
||||||
|
@ -67,8 +84,6 @@ function getLineDistance(x, _, z, point1, point2) -- Notice the underscore for
|
||||||
return math.sqrt(dx_ * dx_ + dz_ * dz_)
|
return math.sqrt(dx_ * dx_ + dz_ * dz_)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function getClosestDistanceToPerimeter(x, y, z, points)
|
function getClosestDistanceToPerimeter(x, y, z, points)
|
||||||
local lowestDist = 9999
|
local lowestDist = 9999
|
||||||
local length = #points
|
local length = #points
|
||||||
|
@ -89,12 +104,6 @@ function getButton(method,button,devices)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function isTracked(device)
|
|
||||||
local x,y,z = lovr.headset.getPosition(device)
|
|
||||||
if x == 0.0 and y == 0.0 and z == 0.0 then return false end
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
|
|
||||||
function drawSinglePointGrid(pass, point1, point2, cornerColor, miscColor)
|
function drawSinglePointGrid(pass, point1, point2, cornerColor, miscColor)
|
||||||
local _, hy, _ = lovr.headset.getPosition("head")
|
local _, hy, _ = lovr.headset.getPosition("head")
|
||||||
local lx1 = point1[1]
|
local lx1 = point1[1]
|
||||||
|
@ -145,8 +154,15 @@ function drawPointGrid(pass,points,cornerColor,miscColor)
|
||||||
drawSinglePointGrid(pass,points[length],points[1],cornerColor,miscColor)
|
drawSinglePointGrid(pass,points[length],points[1],cornerColor,miscColor)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function printTable(table)
|
||||||
|
for _,point in ipairs(table) do
|
||||||
|
print(point[1], " ", point[2])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
function lovr.load()
|
function lovr.load()
|
||||||
lovr.graphics.setBackgroundColor(0.0, 0.0, 0.0, 0.0)
|
lovr.graphics.setBackgroundColor(0.0, 0.0, 0.0, 0.0)
|
||||||
|
print("Is tracked floor:", lovr.headset.isTracked('floor'))
|
||||||
|
|
||||||
-- Default settings
|
-- Default settings
|
||||||
local defaults = {
|
local defaults = {
|
||||||
|
@ -207,7 +223,8 @@ function lovr.load()
|
||||||
color_close_grid = loadSetting("color_close_grid.json", defaults.color_close_grid, json.decode),
|
color_close_grid = loadSetting("color_close_grid.json", defaults.color_close_grid, json.decode),
|
||||||
color_far_corners = loadSetting("color_far_corners.json", defaults.color_far_corners, json.decode),
|
color_far_corners = loadSetting("color_far_corners.json", defaults.color_far_corners, json.decode),
|
||||||
color_far_grid = loadSetting("color_far_grid.json", defaults.color_far_grid, json.decode),
|
color_far_grid = loadSetting("color_far_grid.json", defaults.color_far_grid, json.decode),
|
||||||
points = {}
|
points = {},
|
||||||
|
transformed = false
|
||||||
}
|
}
|
||||||
|
|
||||||
-- Handle points.json
|
-- Handle points.json
|
||||||
|
@ -229,6 +246,8 @@ function lovr.load()
|
||||||
local pointsContent = lovr.filesystem.read(pointsPath)
|
local pointsContent = lovr.filesystem.read(pointsPath)
|
||||||
if pointsContent then
|
if pointsContent then
|
||||||
settings.points = json.decode(pointsContent)
|
settings.points = json.decode(pointsContent)
|
||||||
|
print("FloorSpace:")
|
||||||
|
printTable(settings.points)
|
||||||
end
|
end
|
||||||
|
|
||||||
mode = modeDraw
|
mode = modeDraw
|
||||||
|
@ -253,10 +272,14 @@ end
|
||||||
|
|
||||||
function modeConfigure(pass)
|
function modeConfigure(pass)
|
||||||
local hx, hy, hz = lovr.headset.getPosition("head")
|
local hx, hy, hz = lovr.headset.getPosition("head")
|
||||||
|
floorMatrixInv = getFloorMatrix():invert()
|
||||||
|
|
||||||
for _, hand in ipairs(hands) do
|
for _, hand in ipairs(hands) do
|
||||||
if isTracked(hand) then
|
if lovr.headset.isTracked(hand) then
|
||||||
local cx, cy, cz = lovr.headset.getPosition(hand)
|
local cx, cy, cz = lovr.headset.getPosition(hand)
|
||||||
|
local floorSpaceControlerVector = lovr.math.vec3(cx, cy, cz):transform(floorMatrixInv)
|
||||||
|
local fcx, fcy, fcz = floorSpaceControlerVector:unpack()
|
||||||
|
|
||||||
|
|
||||||
-- Compute the direction from the controller to the headset
|
-- Compute the direction from the controller to the headset
|
||||||
local dirX = hx - cx
|
local dirX = hx - cx
|
||||||
|
@ -274,23 +297,29 @@ function modeConfigure(pass)
|
||||||
pass:text(
|
pass:text(
|
||||||
"- Press '" .. settings.action_button .. "' to add a point -\n" ..
|
"- Press '" .. settings.action_button .. "' to add a point -\n" ..
|
||||||
"- Hold '" .. settings.action_button .. "' to save -\n\n" ..
|
"- Hold '" .. settings.action_button .. "' to save -\n\n" ..
|
||||||
string.format("%.2f", cx) .. "," .. string.format("%.2f", cy) .. "," .. string.format("%.2f", cz),
|
string.format("%.2f", fcx) .. "," .. string.format("%.2f", fcy) .. "," .. string.format("%.2f", fcz),
|
||||||
cx, cy - 0.3, cz, 0.066, angle, 0, 1, 0
|
cx, cy - 0.3, cz, 0.066, angle, 0, 1, 0
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local inputDev = getButton(lovr.headset.wasReleased,settings.action_button,hands)
|
local inputDev = getButton(lovr.headset.wasReleased,settings.action_button,hands)
|
||||||
if inputDev ~= nil and isTracked(inputDev) then
|
if inputDev ~= nil and lovr.headset.isTracked(inputDev) then
|
||||||
local hx,_,hz = lovr.headset.getPosition(inputDev)
|
local cx, _, cz = lovr.headset.getPosition(inputDev)
|
||||||
table.insert(settings.points,{hx,hz})
|
table.insert(settings.points,{cx,cz})
|
||||||
end
|
end
|
||||||
|
|
||||||
inputDev = getButton(lovr.headset.isDown,settings.action_button,hands)
|
inputDev = getButton(lovr.headset.isDown,settings.action_button,hands)
|
||||||
if inputDev ~= nil then
|
if inputDev ~= nil then
|
||||||
saveProg = saveProg - (deltaTime / 3)
|
saveProg = saveProg - (deltaTime / 3)
|
||||||
if saveProg <= 0 then
|
if saveProg <= 0 then
|
||||||
lovr.filesystem.write("points.json", json.encode(settings.points))
|
local floorSpacePoints = {}
|
||||||
|
for _,point in ipairs(settings.points) do
|
||||||
|
local floorSpacePoint = lovr.math.vec3(point[1], 0, point[2]):transform(floorMatrixInv)
|
||||||
|
local x, _, z = floorSpacePoint:unpack()
|
||||||
|
table.insert(floorSpacePoints,{x,z})
|
||||||
|
end
|
||||||
|
lovr.filesystem.write("points.json", json.encode(floorSpacePoints))
|
||||||
deinitConfigure()
|
deinitConfigure()
|
||||||
modeDraw(pass)
|
modeDraw(pass)
|
||||||
return
|
return
|
||||||
|
@ -310,13 +339,28 @@ end
|
||||||
function modeDraw(pass)
|
function modeDraw(pass)
|
||||||
local hx, hy, hz = lovr.headset.getPosition("head")
|
local hx, hy, hz = lovr.headset.getPosition("head")
|
||||||
|
|
||||||
|
if not settings.transformed and lovr.headset.isTracked("floor") then
|
||||||
|
local floorMatrix = getFloorMatrix()
|
||||||
|
print("floorMatrix: ", floorMatrix)
|
||||||
|
transformedPoints = {}
|
||||||
|
for _,point in ipairs(settings.points) do
|
||||||
|
local point = lovr.math.vec3(point[1], 0, point[2]):transform(floorMatrix)
|
||||||
|
local x, _, z = point:unpack()
|
||||||
|
table.insert(transformedPoints, {x, z})
|
||||||
|
end
|
||||||
|
settings.points = transformedPoints
|
||||||
|
print("HeadSpace:")
|
||||||
|
printTable(settings.points)
|
||||||
|
settings.transformed = true
|
||||||
|
end
|
||||||
|
|
||||||
-- Calculate the distance from the head to the perimeter
|
-- Calculate the distance from the head to the perimeter
|
||||||
local perimeterDistHead = getClosestDistanceToPerimeter(hx, hy, hz, settings.points)
|
local perimeterDistHead = getClosestDistanceToPerimeter(hx, hy, hz, settings.points)
|
||||||
|
|
||||||
-- Check distance from each hand to the perimeter
|
-- Check distance from each hand to the perimeter
|
||||||
local handDistances = {perimeterDistHead}
|
local handDistances = {perimeterDistHead}
|
||||||
for _, hand in ipairs(hands) do
|
for _, hand in ipairs(hands) do
|
||||||
if isTracked(hand) then
|
if lovr.headset.isTracked(hand) then
|
||||||
local handX, handY, handZ = lovr.headset.getPosition(hand)
|
local handX, handY, handZ = lovr.headset.getPosition(hand)
|
||||||
local dist = getClosestDistanceToPerimeter(handX, handY, handZ, settings.points)
|
local dist = getClosestDistanceToPerimeter(handX, handY, handZ, settings.points)
|
||||||
table.insert(handDistances, dist)
|
table.insert(handDistances, dist)
|
||||||
|
@ -346,5 +390,17 @@ function modeDraw(pass)
|
||||||
end
|
end
|
||||||
|
|
||||||
function lovr.draw(pass)
|
function lovr.draw(pass)
|
||||||
mode(pass)
|
mode(pass)
|
||||||
|
|
||||||
|
local hx, hy, hz = lovr.headset.getPosition("head")
|
||||||
|
local hangle, hax, hay, haz = lovr.headset.getOrientation("head")
|
||||||
|
local currentTime = getCurrentTime()
|
||||||
|
pass:setColor(1, 1, 1, 0.25)
|
||||||
|
local transform = lovr.math.newMat4()
|
||||||
|
transform:translate(hx, hy + 1.5, hz)
|
||||||
|
transform:rotate(math.pi / 2, 1, 0, 0)
|
||||||
|
transform:rotate(math.pi - math.atan2(hax, haz)*2, 0, 0, 1)
|
||||||
|
transform:translate(0, -0.1, 0)
|
||||||
|
transform:scale(0.1, 0.1, 0.1)
|
||||||
|
pass:text(currentTime, transform)
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue