ScreenToWorld and worldToScreen return nan in setup()

The original code is @John’s, I just added the reporting.

-- Voxel Example

function setup()
    -- Create a new craft scene
    scene = craft:scene()
    craft.scene.main = scene
    scene.camera:add(OrbitViewer, vec3(5,5,0), 20, 1, 100)
    
    -- Create a new entity for the frog
    frog = scene:entity()
    frog.scale = vec3(1,1,1) * 0.05
    
    -- Create an entity to display the frog's voxel model and parent to the frog entity
    local model = scene:entity()
    model.parent = frog
    
    -- Add a volume component (displays voxel models)
    local v = model:add(craft.volume)
    -- Load an existing model from the Froggy project (any model created with Codea will work, including the Voxel Editor project)
    v:load(asset.documents.Craft.Froggy.Player_cvox)
    
    -- Offset the entity's local position based on the size of the voxel model so that it is centered
    model.position = -vec3(v:size()) * 0.5
    
    frog.z = 5

    reportPositions("from setup:")
end

function reportPositions(title)
    print(title)
    local screenPosition = scene.camera:get(craft.camera):worldToScreen(vec3(0,0,0))
    local worldPosition = scene.camera:get(craft.camera):screenToWorld(vec3(0,0,0))
    print("screenPosition",screenPosition)
    print("worldPosition",worldPosition)
end

function update(dt)
    -- Update the scene (physics, transforms etc)
    scene:update(dt)
    
    -- Randomly rotate the frog over time
    local angle = ElapsedTime * 45
    frog.rotation = quat.eulerAngles(angle, angle, angle)
end

-- Called automatically by codea 
function draw()
    update(DeltaTime)
    
    -- Draw the scene
    scene:draw()	
    if not drawReported then
        reportPositions("from draw:")
        drawReported = true
    end
end

function touched(touch)
    -- Convert from screen to world coordinates using the main camera component (z is left at 5 to keep it consistent)
    local p = vec3(touch.x, touch.y, 5)
    frog.position = scene.camera:get(craft.camera):screenToWorld(p)
    reportPositions("from touch:")
end

I think this is because it hasn’t updated the world yet and the screen’s internal data is no initialised. You could probably get it to work by calling scene:update() before using the worldToScreen(), etc… functions

From playing with it, seems it requires both scene:update(DeltaTime) and scene:draw() to be called in setup before the coordinate-translating functions work.