Parenting help

Parenting help

Postby frankg2 » Tue Oct 09, 2012 11:50 pm

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()
slnp = render.attachNewNode(slight)
slnp.setPos(0, 0, 0)

The rest of the code is the same as Mark's in class.
Posts: 1
Joined: Wed Aug 29, 2012 7:16 pm

Re: Parenting help

Postby BenChang » Wed Oct 10, 2012 1:00 am

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'))

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

Code: Select all

Code: Select all
User avatar
Posts: 176
Joined: Mon Jan 17, 2011 4:48 pm

Return to Game Development

Who is online

Users browsing this forum: No registered users and 2 guests