reparentTo with camera

The camera is parented to the panda actor so it will follow it. But it just stays at the panda’s feet. I can’t get it to sit behind the panda. I tried setting its position and that didn’t work. Then I tried changing it in the task and that didn’t work. I’m confused I thought that children nodes were supposed to follow their parents relatively not exactly.

font=loader.loadFont("cmss12")

def addInstructions(pos,msg):
    return OnscreenText(text=msg, style=1, fg=(1,1,1,1), font=font,  
                 pos=(-1.3, pos), align=TextNode.ALeft, scale = .05)   

def AddTitle(text):
    return OnscreenText(text=text, style=1, fg=(1,1,1,1), font = font,
                        pos=(1.3,-0.95), align=TextNode.ARight, scale = .07)
 
class MyApp(DirectObject):
 
    def __init__(self):
        self.keyMap = {"left":0, "right":0, "forward":0, "reverse":0, "turn-left":0, "turn-right":0}
        base.win.setClearColor(Vec4(0,0,0,1))
            
        # Post the instructions        
        self.title = AddTitle("Dominick's Test App")
        self.inst1 = addInstructions(0.95, "[ESC]: Quit")
        self.inst2 = addInstructions(0.90, "[Left Arrow]: Left")
        self.inst3 = addInstructions(0.85, "[Right Arrow]: ight")
        self.inst4 = addInstructions(0.80, "[Up Arrow]: Forward")

        # Load the environment model.
        self.environ = loader.loadModel("samples/Roaming-Ralph/models/world")
        # Reparent the model to render.
        self.environ.reparentTo(render)
        self.environ.setPos(0, 0, 0)

        # Load and transform the panda actor.
        self.panda = Actor("models/panda-model",{"walk": "models/panda-walk4"})
        self.panda.setScale(0.005, 0.005, 0.005)
        self.panda.setPos(0, 0, .2)
        self.panda.reparentTo(render)
        
        self.accept('w', self.setKey, ["forward",1])
        self.accept('w-up', self.setKey, ["forward",0])
        self.accept('a', self.setKey, ["left",1])
        self.accept('a-up', self.setKey, ["left",0])
        self.accept('s', self.setKey, ["reverse",1])
        self.accept('s-up', self.setKey, ["reverse",0])
        self.accept('d', self.setKey, ["right",1])
        self.accept('d-up', self.setKey, ["right",0])
        
        taskMgr.add(self.move, "moveTask")
        
        # Game state variables
        self.isMoving = False
        
        base.camera.reparentTo(self.panda)
        base.camera.setPos(0,-10,10)
        
        base.disableMouse()
       
        
    #Records the state of the arrow keys
    def setKey(self, key, value):
        self.keyMap[key] = value
              
    def move(self,task):
        elapsed = globalClock.getDt()

        if (self.keyMap["forward"]!=0):
            self.panda.setY(self.panda, elapsed*20)
        if (self.keyMap["reverse"]!=0):
            self.panda.setY(self.panda, -(elapsed*20))
        if (self.keyMap["left"]!=0):
            self.panda.setX(self.panda, elapsed*20)
        if (self.keyMap["right"]!=0):
            self.panda.setX(self.panda, -(elapsed*20))
        
        if (self.keyMap["forward"]!=0) or (self.keyMap["reverse"]!=0) or (self.keyMap["left"]!=0) or (self.keyMap["right"]!=0):
            if self.isMoving is False:
                self.panda.loop("walk")
                self.isMoving = True
        else: 
            if self.isMoving:
                self.panda.stop()
                self.isMoving = False
       
        base.camera.setPos(0,-10,10)
        base.camera.lookAt(self.panda)
        
        return Task.cont
       

w=MyApp()
run()

I’m not an expert, nor have I read it closely. But perhaps the mouse needs to be disabled earlier?

I moved to just after I clear the window and it still just shows the panda’s belly.

I just noticed, look at the panda scale. It is at “0.005”, I believe that means that all it’s children inherit that scale. So when setX(10) or whatever, it is really taking “10 * scaleX”. Sorry for not being very clear.

EDIT: It multiplies the scale by the setPos.

First, if you are posting the whole code, please post the includes as well.
You don’t really need lookAt when you parent the cam to player: the rotations are parented as well, so cam will always stay on his back (depending on how it was modelled). Remove that line.
You position the camera in the code twice, also in the task. So on one hand the camera is parented and should follow the panda, on the other hand task repositions it to the same position each frame… Remove that for the positioning to work.
About scaling stuff, if you want x,y,z to scale to same value, you can just write model.setScale(0.005).

Oh and you haven’t set up collisions, so the panda won’t walk on the terrain

AHHHH!! It worked! I can’t believe I spent like 2 hours trying to figure that out…

Well we all felt that at some point.

It twice as bad for me. I’m trying to learn python and Panda3d at the same time. Why does the scaling affect the position coordinates? It seems counter-intuitive. I thought scaling was just the size of the model?

Transforms usually get applied in the order: scale, rotate, translate.
You can imagine a rider parented under a horse model. If you scale down the horse, the position of the rider changes, but he is still in the same position relative to the horse.
If you didn’t scale first, the rider would be floating in the air.

Scaling means multiplying every vertex or sub-object’s position with a constant multiplier.
If there are objects far away from the origin, scaling a parent node will affect the position of the sub-node, not just it’s size.


panda3d.org/manual/index.php/Cheat_Sheets

This is correct

But this?

Just ignore me. :smiley: That was rather late at night for me and I didn’t phrase it well.