@binaryblues Here’s some code I modified from something I posted who knows when. A RED sphere is placed somewhere in an 800x800x800 area along with 600 GREEN spheres. The camera is also placed randomly in there pointing who knows where. Tap the screen once and the camera will pan to put the RED sphere in the screen center. Once the RED sphere is in view, move the cx,cy,cz sliders to move the camera around the area. No matter where you move the camera, it will always pan to keep the RED sphere in the center of the screen. Is that something you’re looking for. This could be modified so the camera will follow the RED sphere if it moved instead of the camera.
viewer.mode=STANDARD
function setup()
parameter.integer("cx",-2,2,0,function() sp=1 end)
parameter.integer("cy",-2,2,0,function() sp=1 end)
parameter.integer("cz",-2,2,0,function() sp=1 end)
assert(craft, "Please include Craft as a dependency")
scene = craft.scene()
skyMaterial = scene.sky.material
skyMaterial.horizon = color(0, 203, 255, 255)
-- create 600 green spheres
for z=1,600 do
createSphere(math.random(-400,400),math.random(-400,400),
math.random(-400,400),color(0,255,0))
end
sp=.01 -- camera follow speed
-- create red sphere at some random position
sxPos=math.random(-400,400)
syPos=math.random(-400,400)
szPos=math.random(-400,400)
createSphere(sxPos,syPos,szPos,color(255,0,0))
-- place camera at some random position
cxPos=math.random(-400,400)
cyPos=math.random(-400,400)
czPos=math.random(-400,400)
direction=vec3(0,0,0)
fill(255)
end
function update(dt)
scene:update(dt)
scene.camera.position=vec3(cxPos,cyPos,czPos)
if rot then
rotateCamera(sp)
cxPos=cxPos+cx
cyPos=cyPos+cy
czPos=czPos+cz
end
end
function draw()
background(0)
update(DeltaTime)
scene:draw()
text("Sphere position x="..sxPos.." y="..syPos.." z="..szPos,WIDTH/2,HEIGHT-50)
text("Camera position x="..cxPos.." y="..cyPos.." z="..czPos,WIDTH/2,HEIGHT-80)
text("Tap screen to point camera at the red sphere",WIDTH/2,HEIGHT-150)
text("then move the sliders to move the camera",WIDTH/2,HEIGHT-180)
end
function createSphere(x,y,z,c)
sphere=scene:entity()
sphere.model = craft.model.icosphere(2,3)
sphere.position=vec3(x,y,z)
sphere.material = craft.material(asset.builtin.Materials.Specular)
sphere.material.diffuse=c
end
function touched(t)
if t.state==BEGAN then
rot=true
end
end
function rotateCamera(v)
local diff=-(scene.camera.position-sphere.position):normalize()
direction=quat.lookRotation(diff,vec3(0,1,0))
scene.camera.rotation=scene.camera.rotation:slerp(direction,v)
end