我的应用程序使用交互式网格库读取obj文件并将其保存在meshview中。我想要的是围绕它的中心旋转物体。
现在我的代码显示了对象,但是当我旋转它时,它会围绕某个点旋转,导致对象进入窗口之外。我宁愿围绕它的中心旋转物体。我知道我必须将对象转换为原点然后再旋转然后再转换,但我不知道该怎么做。
这是我的代码:
public class JavaFx3DObj extends Application
{
private static final String Filename = "model/bunny.obj";
private static final double MODEL_SCALE_FACTOR = 10;
private static final double MODEL_X_OFFSET = 0; // standard
private static final double MODEL_Y_OFFSET = 0; // standard
private static final int VIEWPORT_SIZE = 800;
private double mouseOldX, mouseOldY = 0;
private Rotate rotateX = new Rotate(0, Rotate.X_AXIS);
private Rotate rotateY = new Rotate(0, Rotate.Y_AXIS);
private Rotate rotateZ = new Rotate(0, Rotate.Z_AXIS);
private Group root;
private Group buildScene()
{
File file = new File(Filename);
ObjModelImporter importer = new ObjModelImporter();
importer.read(file);
MeshView[] meshViews = importer.getImport();
importer.close();
// To make the object appear in the middle of the scene
for (MeshView meshView : meshViews)
{
meshView.setTranslateX(VIEWPORT_SIZE / 2 + MODEL_X_OFFSET);
meshView.setTranslateY(VIEWPORT_SIZE / 2 + MODEL_Y_OFFSET);
meshView.setTranslateZ(VIEWPORT_SIZE / 2);
meshView.setScaleX(MODEL_SCALE_FACTOR);
meshView.setScaleY(MODEL_SCALE_FACTOR);
meshView.setScaleZ(MODEL_SCALE_FACTOR);
}
root = new Group(meshViews);
return root;
}
@Override
public void start(Stage stage) throws Exception
{
Group root = buildScene();
PerspectiveCamera camera = new PerspectiveCamera(false);
camera.setTranslateX(100);
camera.setTranslateY(0);
camera.setTranslateZ(100);
camera.getTransforms().addAll (rotateX, rotateY, new Translate(0, 0, 400));
root.setRotationAxis(Rotate.Y_AXIS);
root.setRotate(200);
Scene scene = new Scene(root, 900, 500, true);
scene.setFill(Color.rgb(10, 10, 40));
scene.setCamera(camera);
scene.setOnMousePressed(event ->
{
mouseOldX = event.getSceneX();
mouseOldY = event.getSceneY();
});
scene.setOnMouseDragged(event ->
{
rotateX.setAngle(rotateX.getAngle() - (event.getSceneY() - mouseOldY));
rotateY.setAngle(rotateY.getAngle() + (event.getSceneX() - mouseOldX));
mouseOldX = event.getSceneX();
mouseOldY = event.getSceneY();
});
stage.setTitle("JavaFx3DObj");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args)
{
launch(args);
}
}
答案 0 :(得分:0)
您需要做的是在应用之前设置旋转的轴。 所以你的代码看起来像这样:
public class JavaFx3DObj extends Application {
private static final String Filename = "model/bunny.obj";
private static final double MODEL_SCALE_FACTOR = 10;
private static final double MODEL_X_OFFSET = 0; // standard
private static final double MODEL_Y_OFFSET = 0; // standard
private static final int VIEWPORT_SIZE = 800;
private double mouseOldX, mouseOldY = 0;
private Rotate rotateX = new Rotate(0, Rotate.X_AXIS);
private Rotate rotateY = new Rotate(0, Rotate.Y_AXIS);
private Rotate rotateZ = new Rotate(0, Rotate.Z_AXIS);
private Group root;
private Group buildScene() {
File file = new File(Filename);
ObjModelImporter importer = new ObjModelImporter();
importer.read(file);
MeshView[] meshViews = importer.getImport();
importer.close();
// To make the object appear in the middle of the scene
for (MeshView meshView : meshViews) {
meshView.setTranslateX(VIEWPORT_SIZE / 2 + MODEL_X_OFFSET);
meshView.setTranslateY(VIEWPORT_SIZE / 2 + MODEL_Y_OFFSET);
meshView.setTranslateZ(VIEWPORT_SIZE / 2);
meshView.setScaleX(MODEL_SCALE_FACTOR);
meshView.setScaleY(MODEL_SCALE_FACTOR);
meshView.setScaleZ(MODEL_SCALE_FACTOR);
}
root = new Group(meshViews);
return root;
}
@Override
public void start(Stage stage) throws Exception {
Group root = buildScene();
PerspectiveCamera camera = new PerspectiveCamera(false);
camera.setTranslateX(100);
camera.setTranslateY(0);
camera.setTranslateZ(100);
camera.getTransforms().addAll (rotateX, rotateY, new Translate(0, 0, 400));
root.setRotationAxis(Rotate.Y_AXIS);
root.setRotate(200);
Scene scene = new Scene(root, 900, 500, true);
scene.setFill(Color.rgb(10, 10, 40));
scene.setCamera(camera);
//################ Set pivot points ################\\
rotateX.setPivotX(VIEWPORT_SIZE / 2 + MODEL_X_OFFSET);
rotateX.setPivotY(VIEWPORT_SIZE / 2 + MODEL_Y_OFFSET);
rotateX.setPivotZ(VIEWPORT_SIZE / 2);
rotateY.setPivotX(VIEWPORT_SIZE / 2 + MODEL_X_OFFSET);
rotateY.setPivotY(VIEWPORT_SIZE / 2 + MODEL_Y_OFFSET);
rotateY.setPivotZ(VIEWPORT_SIZE / 2);
rotateZ.setPivotX(VIEWPORT_SIZE / 2 + MODEL_X_OFFSET);
rotateZ.setPivotY(VIEWPORT_SIZE / 2 + MODEL_Y_OFFSET);
rotateZ.setPivotZ(VIEWPORT_SIZE / 2);
//################ End pivot points ################\\
scene.setOnMousePressed(event -> {
mouseOldX = event.getSceneX();
mouseOldY = event.getSceneY();
});
scene.setOnMouseDragged(event -> {
rotateX.setAngle(rotateX.getAngle() - (event.getSceneY() - mouseOldY));
rotateY.setAngle(rotateY.getAngle() + (event.getSceneX() - mouseOldX));
mouseOldX = event.getSceneX();
mouseOldY = event.getSceneY();
});
stage.setTitle("JavaFx3DObj");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
我只能用立方体测试它,因为我没有ObjLoader / ObjFile,但这应该允许你围绕一个点旋转。
小心。如果翻译对象,则必须将轴心点设置为新位置。