Setting the location of one object relative to another

I'm trying to make a line join to a certain point of a particular object. I'm mostly there. I am calling setLocalTranslation() from my update method, which looks uses getWorldTranslation() to get the position of the object, then (this is where it goes wrong), I add a vector to it. I need the vector to be added because I want the line to start from a certain point relative to the object. The intended appearance is that the line is attached to the bottom side of the object. However, when the object moves down, the line start-point slowly slips through the object, until it is the top of the object.



I reckon this is because I am adding the vector which is fine when the object is in positive co-ordinate position, but as it moves down, going in to a negative position, the opposite effect occurs.



Does anyone have any ideas how to solve this?

im not sure what ur problem is from ur descriptions. but try use getLocalTranslation().addLocal(…) instead of world translation.



also, if u put the code inside the update mthod, the translation will be modified every frame which will causes the line or object to move.

That's exactly what I'm trying to achieve; to modify the translation at every frame. I don't think I explained myself very well before… I'll have another go.



I have two cylinders, and there is a joint between the two. Think of an arm, and you have the idea. The whole thing (both the cylinders together) can move around, and they can move independently with the joint (elbow). I want to draw a line going from the middle of the first cylinder (let's say the biceps), to the middle of the second cylinder (just before the wrist, say). I'm using this code to do this:


BufferUtils.setInBuffer(joint.getNodes().get(0).getLocalTransition(), line.getVertexBuffer(0), 0);
BufferUtils.setInBuffer(joint.getNodes().get(1).getLocalTransition(), line.getVertexBuffer(0), 1);



This works perfectly fine, except it draws a line from the *centre* of the first cylinder to the *centre of the second. I want the line to start and end on the *outside* of the cylinder... so you could think of this as starting/ending on top of the skin, instead of starting/ending in the middle of the arm. To do this, I figured I'd add a vector to the code above, which represents the radius of the cylinder, thus getting from the middle to the outside, right:


Vector3f offset = new Vector3f(0,4,0);
BufferUtils.setInBuffer(joint.getNodes().get(0).getLocalTransition().add(offset), line.getVertexBuffer(0), 0);
BufferUtils.setInBuffer(joint.getNodes().get(1).getLocalTransition().add(offset), line.getVertexBuffer(0), 1);



This appears to work perfectly at first, but when the arm moves down, the start and end points seem to 'slip' through the arm, until they are on the other side! If i move the arm back up again, they 'slip' back until they are back where they started. Why would it do this? I'm finding this so frustrating, please help!

When looking at my code, I see that I convert the positions to screen coordinates before using them to create the line.



I think it should be like this:


Vector3f screenPoint1 = DisplaySystem.getDisplaySystem().getScreenCoordinates(joint.getNodes().get(0).getLocalTransition().add(offset));
BufferUtils.setInBuffer(screenPoint1, line.getVertexBuffer(0), 0);



Then again, I set the renderer to ortho before drawing that.

I don't really follow your code, but it seems like you would need to transform the offset into the coordinate space of whatever it is 'attached' to. Otherwise you are just offsetting it by a fixed amount in world space with no regard to the rotation of the joints.



Something along the lines of



joint.getNodes().get(0).worldToLocal(offset)



instead of just offset/

Personally, I'd do this with a collection of nodes…




// create joints:
Node shoulder = new Node();

Node elbow = new Node();
// elbow is 3 units down the shoulder (for example)
elbow.setLocalTranslation(3,0,0);

// cylinder which goes from 0,0,0 to 3,00... to show the space from the elbow to the shoulder:
Cylinder bicep = new Cylinder(); // whats the code for this anyways?
shoulder.attachChild(bicep);

// attach the upper arm to the elbow joint:
shoulder.attachChild(elbow);

// cylinder to show the forearm:
Cylinder forearm = new Cylinder();
elbow.attachChild(forearm);

// attach the arm to the root node to display it:
rootNode.attachChild(shoulder);

// create nodes which show where the start and end of the line should be:
Node bicepToWristStart = new Node();
bicepToWristStart.setLocalTranslation(1.5f,0,0);

Node bicepToWristEnd = new Node();
bicepToWristEnd.setLocalTranslation(1.5f,0,0);

// attach these to upper arm and forearm:
shoulder.attachChild(bicepToWristStart);
elbow.attachChild(bicepToWristEnd);

//Now all you need to do is use the following to figure where to put your line:
bicepToWristStart.getWorldLocation();
bicepToWristEnd.getWorldLocation();

// cant remember how to draw lines though.. :)



If you do it that way then all the translations and rotations of the nodes attached to the shoulder or the elbow will be correctly transformed. You have the locations of the points at hand, you just need to update the start and end points of your line every so often..

Thanks for your help, I tried all three solutions, but it turned out jiminy's one was the one that worked, so I've done something like that. Thank youuuu