How to match Material to a Variable


I was just wondering if there is anyway to match a material to a variable. For example. If the Material equals “mat_1” than var equals “hand” than I setLocalTranslation to a object to that specific material that is on a separate model. I think if there is a way to scan the model for a special Material than I could spawn a weapon in the players hand. Sorry if you do not understand.

Probably material is not what you want.

You probably want the bone name… because then you will also have a place to attach the weapon.

90% sure this is covered in the tutorials since I think the Sinbad model has a sword that can be put into his hand or something.

Fairly new to jmonkey. Can you point me in the right direction

I agree with all the other responders: to attach a weapon to the avatar’s hand, you should create an attachment node for the hand bone and attach the weapon model to the attachment node. This is the usual way, the easy way, the quick way, the way JME’s designers intend you to do this.

On the other hand, I can sympathize with inventing new ways of doing things, also known as “doing things the hard way”. I’ve learned a lot of interesting facts by doing things this way. So …

Assuming there’s a Material specific to the avatar’s hand, then there must be a Geometry with that material applied. You could find that Geometry (which is also a Spatial) by an exhaustive search of the model’s spatials using Spatial.depthFirstTraversal(). For each Spatial that’s an instance of Geometry, you’d compare geometry.getMaterial() with handMaterial.

When a match is found, you’d copy the (world) coordinate transform from geometry to the sword model. But unless the sword and avatar models were designed to fit this way, you’d probably have to adjust (rotate, translate, and/or scale) the Transform to make the sword fit the avatar’s hand. After that, if the hand ever moved, you’d need to update the Transform each time.