Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Whether the camera's lens can lock onto an object in Craft.

Whether the camera's lens can lock onto an object, even if the camera's position is shifted, but the object remains in the lens?

I found that the Camera class has an attribute:target that doesn't work, or it's not what I expected.

The effect is similar to the video, where the camera locks onto the castle in the distance, so even if you move the camera, it should always be pointed towards the castle.

It is best if there are simple implemented property settings.

https://youtu.be/SnMWBxHmh1k

Tagged:

Comments

  • Posts: 1,795
    I’m not sure, but I think what you want has been implemented exactly in the ObjectViewer in the built-in Cameras project.

    You set the target right in the initialization statement, it couldn’t be too much simpler.

    Also, almost all @dave1707’s examples use the ObjectViewer for their cameras, and if you look at them you’ll see it basically takes two lines to set up.

    ***That said***, while I think the OrbitViewer is fantastic, and really simple, and does what it does really well, I personally found it very confusing whenever I tried to customize it to do anything else.

    That’s why, for myself, I developed the “rig system” for camera handling, which you can see in the attached project. Its main focus is the two-joystick-controller UI, but it also includes rig-system versions of the Orbit Viewer and the First-Person View, which to me are implemented in a way that’s much easier to customize.
  • dave1707dave1707 Mod
    Posts: 9,977

    @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
    
  • Posts: 297

    @UberGoober @dave1707 Thank you for providing the routine code! it seems to have achieved the function I need, I intend to study these code.

Sign In or Register to comment.