Javafx使用鼠标拖动来连续旋转对象以加速

时间:2018-06-28 19:09:44

标签: animation javafx 3d rotation javafx-8

我有一个项目正在尝试开发控件。我需要能够通过单击鼠标并拖动并释放来连续旋转对象。释放后,我希望该对象继续从其初始点旋转到释放鼠标的位置。(前提是该对象尚未到达此位置。)我以前看到过以这种方式工作的应用程序,但我似乎无法在网上找到任何代码以显示其实现方式。在将鼠标控件添加到我的项目中之前,我正在使用以下代码对其进行测试。它已经包含简单的拖放机制。但是,我希望您对如何添加连续旋转有所投入。我希望鼠标能够控制对象沿任何轴的旋转速度。当我设置好它时,它可以沿任何轴旋转。但是我想拖动而不是旋转对象,而是使对象以与鼠标拖动的距离成比例的速度旋转(递归旋转)。随着距离增加,这将增加速度。如果我向同一方向拖动相同距离两次,速度应增加一倍,然后对象旋转的速度快两倍。

package javafxapplication3;



import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.geometry.Point3D;
import javafx.scene.DepthTest;
import javafx.scene.Group;
import javafx.scene.PerspectiveCamera;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.paint.PhongMaterial;
import javafx.scene.shape.Box;
import javafx.scene.shape.Sphere;
import javafx.scene.transform.Rotate;
import javafx.scene.transform.Transform;
import javafx.scene.transform.Translate;
import javafx.stage.Stage;

public class trafo extends Application {
    final Group root = new Group();
    final XformWorld world = new XformWorld();
    final PerspectiveCamera camera = new PerspectiveCamera(true);
    final XformCamera cameraXform = new XformCamera();
    private static final double CAMERA_INITIAL_DISTANCE = -1000;
    private static final double CAMERA_NEAR_CLIP = 0.1;
    private static final double CAMERA_FAR_CLIP = 10000.0;
    double mousePosX, mousePosY, mouseOldX, mouseOldY, mouseDeltaX, mouseDeltaY;
    double mouseFactorX, mouseFactorY;

    @Override
    public void start(Stage primaryStage) {
        root.getChildren().add(world);
        root.setDepthTest(DepthTest.ENABLE);
        buildCamera();
        buildBodySystem();
        Scene scene = new Scene(root, 800, 600, true);
        scene.setFill(Color.GREY);
        handleMouse(scene);
        primaryStage.setTitle("TrafoTest");
        primaryStage.setScene(scene);
        primaryStage.show();
        scene.setCamera(camera);
        mouseFactorX = 180.0 / scene.getWidth();
        mouseFactorY = 180.0 / scene.getHeight();
    }

    private void buildCamera() {
        root.getChildren().add(cameraXform);
        cameraXform.getChildren().add(camera);
        camera.setNearClip(CAMERA_NEAR_CLIP);
        camera.setFarClip(CAMERA_FAR_CLIP);
        camera.setTranslateZ(CAMERA_INITIAL_DISTANCE);
    }

    private void buildBodySystem() {
        PhongMaterial whiteMaterial = new PhongMaterial();
        whiteMaterial.setDiffuseColor(Color.WHITE);
        whiteMaterial.setSpecularColor(Color.LIGHTBLUE);
        Box box = new Box(400, 200, 100);
        box.setMaterial(whiteMaterial);
        PhongMaterial redMaterial = new PhongMaterial();
        redMaterial.setDiffuseColor(Color.DARKRED);
        redMaterial.setSpecularColor(Color.RED);
        Sphere sphere = new Sphere(5);
        sphere.setMaterial(redMaterial);
        sphere.setTranslateX(200.0);
        sphere.setTranslateY(-100.0);
        sphere.setTranslateZ(-50.0);
        world.getChildren().addAll(box);
        world.getChildren().addAll(sphere);
    }

    private void handleMouse(Scene scene) {
        scene.setOnMousePressed((MouseEvent me) -> {
            mousePosX = me.getSceneX();
            mousePosY = me.getSceneY();
            mouseOldX = me.getSceneX();
            mouseOldY = me.getSceneY();
        });
        scene.setOnMouseDragged((MouseEvent me) -> {
            mouseOldX = mousePosX;
            mouseOldY = mousePosY;
            mousePosX = me.getSceneX();
            mousePosY = me.getSceneY();
            mouseDeltaX = (mousePosX - mouseOldX);
            mouseDeltaY = (mousePosY - mouseOldY);
            if (me.isPrimaryButtonDown()) {
                cameraXform.ry(mouseDeltaX * 180.0 / scene.getWidth());
                cameraXform.rx(-mouseDeltaY * 180.0 / scene.getHeight());
            } else if (me.isSecondaryButtonDown()) {
                camera.setTranslateZ(camera.getTranslateZ() + mouseDeltaY);
            }
        });
    }

    public static void main(String[] args) {
        launch(args);
    }

}

class XformWorld extends Group {
    final Translate t = new Translate(0.0, 0.0, 0.0);
    final Rotate rx = new Rotate(0, 0, 0, 0, Rotate.X_AXIS);
    final Rotate ry = new Rotate(0, 0, 0, 0, Rotate.Y_AXIS);
    final Rotate rz = new Rotate(0, 0, 0, 0, Rotate.Z_AXIS);

    public XformWorld() {
        super();
        this.getTransforms().addAll(t, rx, ry, rz);
    }
}

class XformCamera extends Group {
    Point3D px = new Point3D(1.0, 0.0, 0.0);
    Point3D py = new Point3D(0.0, 1.0, 0.0);
    Rotate r;
    Transform t = new Rotate();

    public XformCamera() {
        super();
    }

    public void rx(double angle) {
        r = new Rotate(angle, px);
        this.t = t.createConcatenation(r);
        this.getTransforms().clear();
        this.getTransforms().addAll(t);
    }

    public void ry(double angle) {
        r = new Rotate(angle, py);
        this.t = t.createConcatenation(r);
        this.getTransforms().clear();
        this.getTransforms().addAll(t);
    }

}

1 个答案:

答案 0 :(得分:-1)

因此,按照我的观察方式,您要计算从要旋转的对象中间拖动的弧,并在该对象未正确旋转时旋转它,它将以恒定速度旋转直到达到所需的旋转。我从来没有做过这样的事情,所以我不知道我的答案是否会真正起作用,但是我至少可以尝试一下,因为我认为我知道解决方案。现在,下面的代码可能有效,也可能无效,请告诉我是否有任何不正确的地方,我将尝试对其进行修复。

public class RotationNode extends Pane {

    double startMouseAngle, goalAngle = 0.0, angleBeforeRotation;
    boolean currentlyRotating = false;

    public RotationNode(double angle) {
        //Parameter angle is to give this Pane a start angle if you want to
        super();

        //This can all change to whatever you want it to be
        setTranslateX(400.0);
        setTranslateY(200.0);
        setPrefSize(100.0, 100.0);
        setRotationAxis(new Point3D(0.0, 0.0, 360.0));

        //This is just to show something is actually rotating
        Rectangle r = new Rectangle(0, 0, 100, 100);
        r.setFill(Color.BLUE);
        getChildren().add(r);

        setRotate(angle);
        angleBeforeRotation = 0.0;

        addEventHandler(MouseEvent.MOUSE_PRESSED, ov -> {
            if(ov.getButton() == MouseButton.PRIMARY) {
                double x = ov.getX();
                double y = ov.getY();
                startMouseAngle = Math.toDegrees(Math.atan2(x, y));
                angleBeforeRotation = currentlyRotating ? goalAngle : getRotate();
                ov.consume();
            }
        });

        addEventHandler(MouseEvent.MOUSE_DRAGGED, ov -> {
            if(ov.getButton() == MouseButton.PRIMARY) {
                double x = ov.getX();
                double y = ov.getY();
                double a = Math.toDegrees(Math.atan2(x, y));
                double deltaAngle = startMouseAngle - a;
                goalAngle = angleBeforeRotation + deltaAngle;
                if(!currentlyRotating) {
                    new changeRotation();
                }
                ov.consume();
            }
        });
    }

    private class changeRotation {

        private final long TIME_STEP = 25;

        private final double ANGLE_PER_SECOND = 90;
        public changeRotation() {
            final ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();

            Runnable changeWidthCmd = () -> {
                double rotationDelta = ANGLE_PER_SECOND / (1000.0 / TIME_STEP);
                rotationDelta *= getRotate() > goalAngle ? -1 : 1;
                setRotate(getRotate() + rotationDelta);
                if(getRotate() - goalAngle < ANGLE_PER_SECOND / (1000.0 / TIME_STEP) && getRotate() - goalAngle > -ANGLE_PER_SECOND / (1000.0 / TIME_STEP)) {
                    setRotate(goalAngle);
                    currentlyRotating = false;
                    service.shutdown();
                }
            };

            currentlyRotating = true;
            service.scheduleAtFixedRate(changeWidthCmd, 0, TIME_STEP, TimeUnit.MILLISECONDS);
        }
    }
}

希望这对您有帮助

Lenardjee

编辑:
更新了代码,现在可以正常工作了。基于鼠标拖动的旋转有效,但中心点很奇怪,效果不佳。我将尝试改善这一点并更新此答案

重要编辑:
我忘了提一下,这个答案包含使用外部库的代码。要添加它,请将其添加到项目pom文件:

<dependencies>
    <dependency>
        <groupId>io.reactivex.rxjava2</groupId>
        <artifactId>rxjava</artifactId>
        <version>2.1.14</version>
    </dependency>
</dependencies>

或从
https://jar-download.com/?detail_search=g%3A%22io.reactivex.rxjava2%22&g=io.reactivex.rxjava2&p=1
下载该库,然后将该jar添加到项目依赖项中。