put the camera to fit the screen with an object

hi
i have an object with its shperebounds positioned in Poin3(100, 0, 253).

i want to put the camera to let this object fit the screen.

i tried to do some calcolous but i don’t find a solution.

following this maths:
math.it/formulario/images/tr … oretto.gif
B: is my camera
beta: angle that is fov/2
AC: radius of BoundingSphere.

now with some trigonometry i get this formula:
AB = CA / sin(beta) * cos(beta)

this is my code:

 node.setPos(100, 0, 253)

        # position node for fitting the sceen. this is done by some calculaton with lens fov.
        fov = self.camLens.getFov().getX()
        print 'fov: ', fov 

        radius = node.getBounds().getRadius()
        print 'quad height: ', radius

        print 'screen dimension',self.win.getXSize(), self.win.getYSize()

        b = radius
        beta = fov/2
        x = (b/sin(beta)) * cos(beta)
                
        def foo(task):
            camera.setPos(node.getX()+radius, node.getY()-x, node.getZ()+radius)
            print camera.getPos()
            return Task.cont
        taskMgr.add(foo, "SpinCameraTask")

id does not work. some advice?

See this post on a similar question:

[url][SOLVED] Make NodePath visible to camera FOV]

David

this is exactly what i was looking for! thanks!

this is my code:

   def foo(task):
            self.centerTrackball(self.current_node)
            return Task.cont
        taskMgr.add(foo, "SpinCameraTask")
        self.useTrackball()
        
    def centerTrackball(self, where): 
        bounds = where.getBounds() 
        center = bounds.getCenter() 
        radius = bounds.getRadius() 
        
        lens = self.camLens 
        fov = lens.getFov() 
        distance = radius / tan(radians(min(fov[0], fov[1]) / 2.0)) 
        
        self.camera.setPos(bounds.getCenter()) 
        self.camera.setY(base.camera, -distance)

in this way i have the camera centered in the exact point i’m looking for BUT i’ve lost the trackball functionality like pan and zoom.

i tried ShowBase.tracknball.setPos and ShowBase.tracknball.setCenter but the code has no effect…

Note the difference between base.trackball.setPos() and base.trackballNode.setPos(). The former will not do anything at all; the latter will move the camera and the trackball position.

David

yeah ok but why any usage of camera.setPos in the init has no effect??

        def foo(task):
            distance = self.centerTrackball(self.current_node)
            base.trackball.node().setOrigin(self.current_node.getBounds().getCenter())
            base.trackball.node().setY(distance)
            return Task.cont
        taskMgr.add(foo, "foo")
        self.useTrackball()

this code put the trackball in right position but, obviously, with the leftclick i can’t change the zoom…

I’m not sure what you’re asking.

When the trackball is enabled, moving the camera directly has no effect, because every frame the trackball moves the camera right back to where the trackball thinks it belongs.

That’s why you can’t use camera.setPos(), but you can use trackball.node().setPos(), because this second call is telling the trackball where the camera belongs. (And trackball.setPos() only moves the trackball itself, which does nothing.)

If you want to change the scale of the zoom controls, you can use trackball.node().setForwardScale().

David

mhmm ok. there is some tutorial about this a part panda3d.org/manual/index.php … era_Driver that is quite generic?

i also need to trigger - i the space bar is pressed - the rotation of an object according to mouse movement

Right, that page is very generic; it’s meant for a brief introduction to the concept.

You can use accept(‘space’, handlerFunc) and let the handlerFunc toggle the mouse interface on and off.

David