例如——有这样一段代码
//create new cylinder
Cylinder cylinder = new Cylinder(2, 6);
...
//somewhere add transform
cylinder.getTransforms().add(new Translate(0, -5, 0));
...
//and somewhere i call
cylinder.setTranslateX(3);
cylinder.setTranslateY(-2);
因此,圆柱体发生移动,其在场景中的坐标为 {3, -7, 0}
为了获得实际的坐标,我尝试了类似的方法:
double coordY = 0;
for(Transform t : cylinder.getTransforms() ){
coordY += t.getTy();
}
coordY += cylinder.getTranslateY();
它有效,但这显然不是最合理的方式......
更新1
正如正确评论的那样fabian
该方法不考虑是否相对于原点以外的点进行旋转
例如:
Rotate rotate = new Rotate();
rotate.setAngle(60);
rotate.setPivotX(2);
rotate.setPivotY(1);
rotate.setPivotZ(3);
cylinder.getTransforms().add(rotate);
在这种情况下,结果将不正确
如何获取该圆柱体或任何其他对象(如 Shape3d 或 MeshView)的真实坐标{x,y,z}
谢谢!
最佳答案
The rectangular bounds of this Node which include its transforms.
boundsInParent
is calculated by taking the local bounds (defined byboundsInLocal
) and applying the transform created by setting the following additional variables
transforms
ObservableListscaleX
,scaleY
,scaleZ
rotate
layoutX
,layoutY
translateX
,translateY
,translateZ
The resulting bounds will be conceptually in the coordinate space of the
Node
's parent, however the node need not have a parent to calculate these bounds.Note that this method does not take the node's visibility into account; the computation is based on the geometry of this
Node
only.This property will always have a non-null value.
Note that
boundsInParent
is automatically recomputed whenever the geometry of a node changes, or when any of the following the change: transformsObservableList
, any of the translate, layout or scale variables, or the rotate variable. For this reason, it is an error to bind any of these values in a node to an expression that depends upon this variable. For example, the x or y variables of a shape, ortranslateX
,translateY
should never be bound toboundsInParent
for the purpose of positioning the node.
您将获得 Bounds
对象,它具有查询节点位置和尺寸的方法,包括三维对象。如上述文档中所述,bounds-in-parent 考虑了应用的转换(它是 bounds-in-local + 转换)。当然,这些值都在节点父节点的上下文中。如果您需要不同空间中的坐标,那么您需要转换它们 - Node
类为此提供了许多方法。
关于JavaFX 如何获取 3D 场景中对象的坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59436151/