I am trying to automate some node's movements. I would like to move a node on top of another node but I am not able to achieve it using a generic method.
I.E. I wrote something like this:
public Point3D getPosition(Node referenceNode, Node nodeToPlace) {
Bounds refBounds = referenceNode.getBoundsInParent();
double refX = refBounds.getMinX() + (refBounds.getWidth() / 2);
double refY = refBounds.getMaxY() + nodeToPlace.getBoundsInParent().getHeight();
double refZ = refBounds.getMinZ() + (refBounds.getDepth() / 2);
double nodeToPlaceX = nodeToPlace.getBoundsInParent().getMinX() +
(nodeToPlace.getBoundsInParent().getWidth()/2);
double nodeToPlaceY = nodeToPlace.getBoundsInParent().getMinY() +
(nodeToPlace.getBoundsInParent().getHeigth()/2);
double nodeToPlaceZ = nodeToPlace.getBoundsInParent().getMinZ() +
(nodeToPlace.getBoundsInParent().getDepth()/2);
double translationX = refX - nodeToPlaceX;
double translationY = refY - nodeToPlaceY;
double translationZ = refZ - nodeToPlaceZ;
nodeToPlace.getTransforms().add(new Translate(translationX,
translationY, translationZ));
}
What am I doing wrong? I suppose that I am not considering something of important but I can't figure it. I hope that someone can explain me the right way... Thanks in advance.
Based on the first example seen here, the example below uses a
Timeline
to animate the motion ofb1
, shadedAQUA
, towardb2
, tintedCORAL
.Your code looks to be correct, except that you need to account for the transformations of the nodes' parents as well, in the general case that these two nodes don't have the same parent. (I assume
getBoundsInParent
returns the coordinates in the parent's frame of reference, not the world.)