forked from hkc/cc-stuff
1
0
Fork 0

wallhack: flipping entity coordinates, so that they are rendered in front

This commit is contained in:
Vftdan 2023-10-13 14:22:39 +03:00
parent 2856bf796b
commit 7d8cc2834e
1 changed files with 51 additions and 3 deletions

View File

@ -1,4 +1,42 @@
return function()
local playerLookVector = nil
local function updateLookVector()
-- pitch:
-- 0 => positive Z
-- 90 => negative X
local yawr = math.rad(player.yaw)
local pitchr = math.rad(player.pitch)
local cosPitch = math.cos(pitchr)
local x = cosPitch * -math.sin(yawr)
local z = cosPitch * math.cos(yawr)
local y = math.sin(pitchr)
playerLookVector = {x, y, z, x = x, y = y, z = z}
return playerLookVector
end
local function dotProduct3(u, v)
return u.x * v.x + u.y * v.y + u.z * v.z
end
local function vec3Scale(k, v)
return {k * v.x, k * v.y, k * v.z}
end
local function vec3Add(u, v)
return {u.x + v.x, u.y + v.y, u.z + v.z}
end
local function toFrontCoords(entity)
local dotResult = dotProduct3(entity, playerLookVector)
if dotResult >= 0.0 then
return {x = entity.x, y = entity.y, z = entity.z, flipped = false}
end
local result = vec3Add(entity, vec3Scale(-2 * dotResult, playerLookVector))
result.flipped = true
return result
end
local cache = {}
while _G._running do
for id, entry in pairs(cache) do
@ -18,14 +56,24 @@ return function()
cache[id].frame = canvas3d.addFrame({ 0, 0, 0 })
cache[id].text = cache[id].frame.addText({ 0, 0 }, "")
end
local entityFront = toFrontCoords(entity)
if entityFront.flipped then
cache[id].cube.setColor(0x004040FF)
else
cache[id].cube.setColor(0xFFC0C0FF)
end
cache[id].cube.setAlpha(0x20)
cache[id].cube.setDepthTested(false)
cache[id].frame.setDepthTested(false)
cache[id].cube.setPosition(entity.x - 0.25, entity.y - 0.25, entity.z - 0.25)
cache[id].frame.setPosition(entity.x, entity.y, entity.z)
cache[id].cube.setPosition(entityFront.x - 0.25, entityFront.y - 0.25, entityFront.z - 0.25)
cache[id].frame.setPosition(entityFront.x, entityFront.y, entityFront.z)
cache[id].text.setAlpha(0xFF)
cache[id].text.setText(entity.name .. "\n" .. textutils.serialize(entity))
cache[id].text.setColor(0xFF0000FF)
if entityFront.flipped then
cache[id].text.setColor(0x00FFFFFF)
else
cache[id].text.setColor(0xFF0000FF)
end
end
end
os.sleep(0.05)