[SOLVED] How can I attach a geometry to a bone without offset?

Dear all,

I’m really sorry to have to ask, since I’m new, but I’m having trouble with attaching a geometry to a bone so that it does not have an offset from this bone.

This is my code, to attach a cube (a box geometry) to the bone, which is the head of my robot arm:

cube.setLocalTranslation(head.getWorldTranslation());
cube.removeFromParent();
head.attachChild(cube);

What happens, is that the cube attaches itself to the head, but with a very slight offset. I can then animate the robot arm and the cube goes with the head perfectly. Afterwards, the cube is set down by the robot arm and I attach it to the rootNode:

Vector3f position = cube.getWorldTranslation();
cube.removeFromParent();
rootNode.attachChild(cube);
cube.setLocalTranslation(position);

I can move the arm again, without moving the cube. So far so good… I animate the arm, so that it is back in it’s original position.
But then I want to reset the cube to it’s original location (before the animation) and start the process all over again. Here the problem arises, that the offset of the cube (when attached to the head) suddenly is huge. So the cube is floating in the air. I try to reset the cube with

cube.setLocalTranslation(new Vector3f(-1.5f,0,0));

but it doesn’t change its location at all.

What I don’t understand is why it behaves like that in the second round of animation.

Does anyone know, how to correctly attach geometries to bones, so that there is no offset?

Thanks in advance! :slight_smile:

I mean, you’ve left out a lot of the important code I guess.

The first time, you gave the cube this “offset”.

Then later you give the cube this “offset”… which is the world position that it used to have. So that’s likely going to be pretty big.

If you attach it right back to the head without giving it back the head offset then it’s going to be strange. But we don’t see that code so we are left to guess what you did.

oh sorry. What I didn’t mention was that if I attach the cube to the bone without the cube.setLocalTranslation(head.getWorldTranslation()) it attaches it with this huge offset that also happens in the second round of animation…

Here is the missing code. I didn’t want to post it all, because I’m working with lists since I have several robot arms and it gets kind of confusing. I created a list for all controls named “controls”, one for the models called “arm” and one for the skeletonControls called “skeletonControls”.

So in my simpleInitApp() I create the cube:

        Box b = new Box(0.1f,0.1f,0.1f);
        cube = new Geometry("Box",b);
        Material mat = new Material (assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
        mat.setColor("Color", ColorRGBA.Blue);
        cube.setMaterial(mat);
        cube.setLocalTranslation(new Vector3f(-1.5f,0,0));
        rootNode.attachChild(cube);
        
        int i;
        for (i=0; i<11; i = i+1)
        {
            control.set(i, arm.get(i).getChild("Rmk3.010").getControl(AnimControl.class));
            control.get(i).addListener(this);
            channel.set(i, control.get(i).createChannel());
            channel.get(i).setAnim("down_left");
            channel.get(i).setLoopMode(LoopMode.DontLoop);
            
        }
        
        for (i=0; i<11; i= i+1)
        {
           skeletonControl.set(i, arm.get(i).getChild("Rmk3.010").getControl(SkeletonControl.class));
        }

    }

Then my animations are done here (I only attached the cube to the first arm in the list):

 @Override
    public void onAnimCycleDone(AnimControl control, AnimChannel channel, String animName) {
        int i;
    	
        if (animName.equals("down_left")){
            for (i=0;i<11; i=i+1) {
        	if (control.equals(arm.get(0).getChild("Rmk3.010").getControl(AnimControl.class)));
        	{
                    head = skeletonControl.get(0).getAttachmentsNode("Bone.004");
                    cube.removeFromParent();
                    head.attachChild(cube);                    
                }
            
            }

    	}
    }

This was my original code, before I tried to figure out how to remove the huge offset, that my cube has. I positioned the cube where the head of the robot arm was situated but when I later attach it to the head bone, this is how it looks:

Selection_002

I figured it out, right after my reply. So sorry to have bothered you. By attaching the cube to the head, the offset is now from the head and not from the rootNode. So just before removing the cube from the rootNode, I changed its localTranslation to Vector3f.ZERO and now everything is fine.

1 Like