Place an object in front of the camera

it’s a simple question but I can’t figure out how to do it, from the position of my camera and its rotation (getPos and getHPR) I would like to place an object in front of the camera (at a distance of 10 meters for example).

The simplest option.

from direct.showbase.ShowBase import ShowBase

class MyApp(ShowBase):

    def __init__(self):
        ShowBase.__init__(self)
        
        self.disableMouse()

        camera.setPos(45, 0, 200)
        camera.setHpr(0, 45, 50)
        
        object = self.loader.loadModel("panda")
        object.reparentTo(render)
        object.setPos(camera, (0, 50, 0))

app = MyApp()
app.run()
2 Likes

yes but my question is how to find aumatically object.setPos(camera, (0, 50, 0)) ?
I would like to determine this position automatically

The call “object.setPos(camera, (0, 50, 0))” just means “place the object at a position of x = 0, y = 50, z = 0, relative to ‘camera’”. The coordinates are whatever you want them to be, and the relative position will be automatically determined by the call to “setPos”.

Unless you have some more-specific requirement, or I’m misunderstanding?

from direct.showbase.ShowBase import ShowBase
from panda3d.core import NodePath

class MyApp(ShowBase):

    def __init__(self):
        ShowBase.__init__(self)
        
        self.disableMouse()

        camera.setPos(45, 0, 200)
        camera.setHpr(0, 45, 50)
        
        point = NodePath("point")
        point.reparentTo(render)
        point.setPos(camera, (0, 50, 0))
        pos = point.getPos()
        print(pos)
        
        object = self.loader.loadModel("panda")
        object.reparentTo(render)
        object.setPos(pos)

app = MyApp()
app.run()

Well you can use an intermediate node for the calculation.

A minor point: I think that you would want “pos = point.getPos(render)” there–otherwise you’re just going to get the position of the object relative to its parent, I believe, i.e. (0, 50, 50).

And indeed, you could do that–I’m just struggling to see why you might want to. That said, my not seeing a reason doesn’t mean that one doesn’t exist–but it does mean that if there is such a reason, I’m curious to know what it is!

Watch carefully, sleight of hand and nothing else.

pos = point.getPos()
print(pos)

out:

LPoint3f(45, 35.3553, 235.355)

Ah, you’re right–sorry, I’m a little out of it today! I missed that, in this case, the object wasn’t parented below the camera. My mistake!

I understood, sorry I misread the code of serega-kkz
it does what I want

1 Like