Page 1 of 1

Parenting help

PostPosted: Tue Oct 09, 2012 11:50 pm
by frankg2
I'm not sure how to parent my spotlight to an object, like my vehicle.

Here's my spotlight code:
Code: Select all
slight = Spotlight('slight')
slight.setColor(VBase4(0.2, 0.2, 0.8, 1))
spotLightNP = render.attachNewNode(slight)
lens = PerspectiveLens()
slight.setLens(lens)
slnp = render.attachNewNode(slight)
slnp.setPos(0, 0, 0)
slnp.lookAt(spotLightNP)
render.setLight(slnp)


The rest of the code is the same as Mark's in class.

Re: Parenting help

PostPosted: Wed Oct 10, 2012 1:00 am
by BenChang
You can use reparentTo() to attach the spotlight to the vehicle. take a look at the manual page on scenegraph manipulation.

See also this forum thread on the difference between reparentTo and attachNewNode. The most concise way of doing it is probably something like this:

Code: Select all
headlight = car.attachNewNode(Spotlight('headlight'))
headlight.node().setColor(Vec4(0.2,0.2,0.8,1))
render.setLight(headlight)


Notice a couple of shortcut tricks in here. first, I'm not storing a separate variable for the actual spotlight node, only the nodepath (headlight). I'm accessing the spotlight attributes using the node() method on the nodepath, which for any nodepath will get you to the node with specific data and methods in it.

Read the manual page about the scenegraph for a more indepth explanation

I'm using attachNewNode() on the car itself, instead of render. car here is a nodepath; remember that render is ALSO just a nodepath, just at the root of the scenegraph. so anything you can do with 'render' you can also do with any other nodepath.

Also note the shorthand "Vec4" instead "VBase4". VBase4 is the internal C++ name of a 4-element vector in Panda, but when using the Python binding you can just call it a "Vec4" instead which is a much more understandable name than VBase4. There is also Vec3 and Vec2.

You don't always have to create a lens for a spotlight - it gives you a default one to start with. You also don't need to use lookAt() in this case - your code below just adds the light to two nodepaths and then kind of points the light at itself, I think.


If you're trying to make the light point at a specific point in space you can say

Code: Select all
spotlightNP.lookAt(0,0,0)

or
Code: Select all
building=loader.loadModel('building.egg')
spotlightNP.lookAt(building)

or
Code: Select all
spotlightTarget=render.attachNewNode("spotlightTarget")
spotlightNP.lookAt(spotlightTarget)