[SOLVED] Attaching spatial to bone through controls

You are using them wrong! It’s nothing bad, don’t worry. For me it seems like you haven’t fully understand how nodes and their relative behaviour works when you attach them to other nodes. Because of that I can highly recommend the following tutorial: Keynote

Anyway, to make it a little bit easier for you, I will show you one possible way to do the offset for the gun.

/**
 *
 * @author Domenic
 */
public class WeaponAttachmentControl extends AbstractControl {

	private final Node attachmentNode;
	private Node offsetNode;
	private Node rootNode;
	
	public WeaponAttachmentControl(SkeletonControl skeletonControl, String bone, Node realRootNode, Vector3f offsetTranslation, Quaternion offsetRotation) {
		if (skeletonControl != null && bone != null) {
			attachmentNode = skeletonControl.getAttachmentsNode(bone);
		} else {
			attachmentNode = null;
		}
		
		// the rootNode will just copy the tranform from the real attachment node
                // you can call it how you want
		this.rootNode = new Node();
		realRootNode.attachChild(rootNode);
		
		// offsetNode contains the offset
		offsetNode = new Node("OffsetNode");
		offsetNode.setLocalTranslation(offsetTranslation); // apply your offset
		offsetNode.setLocalRotation(offsetRotation);       // same here
		rootNode.attachChild(offsetNode); 
		
		// Remember: The local translation / rotation is relativ to the parent
		// so you don't have to do math operations in update !!!!
	}

	@Override
	public void setSpatial(Spatial spatial) {
		super.setSpatial(spatial); 
		
		if (spatial == null) {
			return;
		}
		// the gun will keep its transformation 
		offsetNode.attachChild(spatial);
		
	}
	
	

	@Override
	protected void controlUpdate(float tpf) {
		if (spatial == null || attachmentNode == null) {
			return;
		}
		// in update we have to refresh the transformation 
		// it's like copy & paste ;-)
		rootNode.setLocalTranslation(attachmentNode.getWorldTranslation());
		rootNode.setLocalRotation(attachmentNode.getWorldRotation());
		
	}

	@Override
	protected void controlRender(RenderManager rm, ViewPort vp) {
	}

}
1 Like

Ok, ok, NOW I’ve figured out this offset issue: all I had to do was parent the offset node to the attachment node in the control class and replace spatial.setLocalTranslation(attachmentNode.getWorldTranslation()) with spatial.setLocalTranslation(offsetNode.getWorldTranslation()).

Thank you all for putting an effort to explain this to me even though a lot of times it went over my head.:chimpanzee_grin: