Problems orienting and positioning the camera

Hi,

This is a rather stupid problem and I’m sure it has a really simple solution but I’ve been struggling with this almost the whole day without getting it right. What I’m trying to do is positioning the camera so that a square object is visible right from the startup. I have tried many differen values, great and small, for the camera.setPos() function but none have been correct. I have tried values ranging from 1 up to 100 on each of the parameters. I have also tried the camera.lookAt() function. I have made sure that the modeling package I use has set the center of the square to the center of the model. Sometimes I am able to get the square to show if I use the mouse to control the camera but the square always seems to be in a totally different place than I expected. It’s always somehow behind the camera, no matter what values I use.

Here’s the code I’m using:

import direct.directbase.DirectStart
from pandac.PandaModules import *
from direct.task import Task

tile = loader.loadModel("tile.egg")
tile.reparentTo(render)

camera.setPos(0, 0, 5)
tile.setPos(camera, 10,10,-5)
camera.lookAt(tile)

run()

In this code I’m assuming that z is the up direction and that I am setting the tile 5 units lower than the camera. I’m a little confused as to which parameter is the “up” direction. Is it the y or the z?

Hi Crimson,
I’m struggling to figure out the camera too. So I’ll try to give you the benefit of my VERY limited knowledge.

When you use camera.setPos(X, Y, Z) the X value moves the camera to the left and right. The Y value acts like a zoom function, negative numbers move the camera back and positive numbers move the camera in. The Z value moves the camera up and down. Pathetic attempt at a diagram:


                                             Z
                                             l
                                             l
                                             l
                                             l_ _ _ _ _ X
                                            /
                                          /
                                        /
                                       Y

When you use camera.setHpr(heading, pitch, roll) the first number represents the heading or direction the camera is facing. The second number is the pitch or pan, a negative number makes the camera look down at the ground, a positive number makes it look up into the sky. The last number is the roll, which is kind of hard to explain, but it rolls the camera left and right (makes the world look slanted) so I always leave it set at 0.

Try playing with the Y and Pitch numbers until you can see your model, sort of like this:

import direct.directbase.DirectStart
from pandac.PandaModules import *
from direct.task import Task

tile = loader.loadModel("tile.egg")
tile.reparentTo(render)
tile.setPos(0,0,0) # To make sure it's at the center of the world

camera.setPos(0, -15, 5) # move the camera back
camera.setHpr(0, -15, 0) # make the camera look down

run()

Hope that helps.

Cheers

Thank you for your reply Tiptoe.

I tried experimenting with the values and used setHpr() but for some strange reason nothing I do seems to affect the view in any way. The model I use (now I’m experimenting with the panda model) seems to be always in the same position right behind the camera. Weird. And I’m quite sure that I remembered to save the script between runs :smiley: I’ll try looking at some of the tutorials in case I have missed something.

The manual doesn’t do a very good job of pointing out that you must call:


base.disableMouse()

if you plan to be moving the camera with direct setPos() and setHpr() calls. Otherwise, the camera will automatically reset itself every frame to wherever you moved it with the mouse (for instance, the initial position), regardless of where your code tries to move it to.

David

Oh, that’s it! No wonder it didn’t work. I’m going to try that out. Thank you.