Commit 1d809691 authored by Sergio Emanuel Rodriguez Rivera's avatar Sergio Emanuel Rodriguez Rivera
Browse files

mouse moving objects

parent 3ac2e45b
Loading
Loading
Loading
Loading
+1 −2
Original line number Diff line number Diff line
@@ -7,9 +7,8 @@
        <option value="$PROJECT_DIR$/pom.xml" />
      </list>
    </option>
    <option name="workspaceImportForciblyTurnedOn" value="true" />
  </component>
  <component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="22" project-jdk-type="JavaSDK">
  <component name="ProjectRootManager" version="2" languageLevel="JDK_X" default="true" project-jdk-name="22" project-jdk-type="JavaSDK">
    <output url="file://$PROJECT_DIR$/out" />
  </component>
</project>
 No newline at end of file
+6 −0
Original line number Diff line number Diff line
@@ -100,6 +100,12 @@ public class MovingObject {
        return velocityVec;
    }

    public void setVelocityVec(double x,double y, double z) {
        velocityVec[0] = x;
        velocityVec[1] = y;
        velocityVec[2] = z;
    }

    public void updateVelocity() {
        velocityVec[1] += gravity;
    }
+140 −33
Original line number Diff line number Diff line
@@ -14,6 +14,7 @@ import javafx.geometry.Point3D;
import javafx.geometry.Pos;
import javafx.scene.*;
import javafx.scene.control.Label;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
@@ -40,11 +41,13 @@ import static java.lang.Math.abs;
 * it includes a real-time FPS counter overlaid on the scene to monitor performance.
 */
public class GraphicsAndWindowsTest extends Application {
    private double anchorX, anchorY;
    private double anchorAngleX = 0;
    private double anchorAngleY = 0;
    private final DoubleProperty angleX = new SimpleDoubleProperty(0);
    private final DoubleProperty angleY = new SimpleDoubleProperty(0);
    public double anchorX, anchorY, releaseX, releaseY;

    private Line slingLine;
    //private double anchorAngleX = 0;
    //private double anchorAngleY = 0;
    //private final DoubleProperty angleX = new SimpleDoubleProperty(0);
    //private final DoubleProperty angleY = new SimpleDoubleProperty(0);
    //public static String[] parameters;
    public static MovingObject[] objects;
    /**
@@ -63,7 +66,7 @@ public class GraphicsAndWindowsTest extends Application {


        ObstacleField obs = new ObstacleField(sceneWindow.getWindowSize(),sceneWindow.getXOffset(),
                sceneWindow.getYOffset(),300,-250, 1000, 30, "Box",
                sceneWindow.getYOffset(),300,-250, 1000, 0, "Box",
                Color.ORANGERED, false);

        String[] ob2 = {"--image", "src/teapot.txt", "--speed", "32", "--dir", "4,", "7,", "10"};
@@ -93,6 +96,8 @@ public class GraphicsAndWindowsTest extends Application {
        group3D.getChildren().add(ourWindow);
        group3D.getChildren().add(new AmbientLight(Color.WHITE));

        slingLine = new Line(0,0,0,0);


        // NOTE: Due to the perspectiveCamera it looks like it is rotating even though it isn't
        // If you comment out this line and the scene.setCamera(camera) line
@@ -141,7 +146,8 @@ public class GraphicsAndWindowsTest extends Application {
        subScene3D.setFill(Color.SILVER);
        subScene3D.setCamera(camera);

        initMouseControl(group3D, subScene3D);
        //initMouseControl(group3D, subScene3D);



        /*
@@ -163,6 +169,71 @@ public class GraphicsAndWindowsTest extends Application {

        Scene scene = new Scene(Layers, 1500, 800);

        //setupMouseControl(subScene3D,objects); DELETE THIS LATER IF NOT USED

        double elasticityFactor = 0.5;


        ourWindow.setOnMousePressed(mouseEvent -> {
            anchorX = mouseEvent.getX();
            anchorY = mouseEvent.getY();
            //System.out.println("Mouse pressed at: " + anchorX + ", " + anchorY);

            slingLine.setStroke(Color.RED);
            slingLine.setStrokeWidth(2);
            slingLine.setStartX(anchorX);
            slingLine.setStartY(anchorY);
            slingLine.setEndX(anchorX);
            slingLine.setEndY(anchorY);
            group3D.getChildren().add(slingLine);
        });

        ourWindow.setOnMouseDragged(mouseEvent -> {
            double dragX = mouseEvent.getX();
            double dragY = mouseEvent.getY();
            //System.out.println("Mouse dragged to: " + dragX + ", " + dragY);

            // Update the end of the sling line to the current drag position
            slingLine.setEndX(dragX);
            slingLine.setEndY(dragY);
        });

        ourWindow.setOnMouseReleased(mouseEvent -> {
            releaseX = mouseEvent.getX();
            releaseY = mouseEvent.getY();
            //System.out.println("Mouse released at: " + releaseX + ", " + releaseY);

            for (MovingObject o : objects ) {
                double x = releaseX -anchorX;
                double y = releaseY - anchorY;
                o.setVelocityVec(x,y,o.getVelocityVec()[2]);
            }

            // Remove the sling visualization line
            group3D.getChildren().remove(slingLine);

        });

        stage.addEventHandler(KeyEvent.KEY_PRESSED, e->{
            if( e.getCode() == KeyCode.W){
                //System.out.println("SPACE");
                for(MovingObject o: objects){
                    double Z = o.getVelocityVec()[2]+5;
                    o.setVelocityVec(o.getVelocityVec()[0], o.getVelocityVec()[1], Z);
                }
            }
            if( e.getCode() == KeyCode.S){
                //System.out.println("SPACE");
                for(MovingObject o: objects){
                    double Z = o.getVelocityVec()[2]-5;
                    o.setVelocityVec(o.getVelocityVec()[0], o.getVelocityVec()[1], Z);
                }
            }

        });



        stage.setTitle("FPS test");
        stage.setScene(scene);
        stage.show();
@@ -305,7 +376,7 @@ public class GraphicsAndWindowsTest extends Application {
                            }
                        }
                        objects[i].updateTranslate(x, y, z);
                        System.out.println(Arrays.toString(objects[i].getCentroid()));
                        //System.out.println(Arrays.toString(objects[i].getCentroid()));
                        //objects[i].updateRays();
                    }
                    lastUpdate = now;
@@ -319,7 +390,6 @@ public class GraphicsAndWindowsTest extends Application {
                    fpsLabel.setText(String.format("FPS: %d", frameRate));
                    frameRate = 0; // Reset frame count
                    fpsTimer = now; // Reset the FPS timer
                    score++;
                    scoreLabel.setText(String.format("Score: %d", score));
                }

@@ -327,30 +397,67 @@ public class GraphicsAndWindowsTest extends Application {
        };
        timer.start();
    }
    private void initMouseControl(Group group, SubScene scene){
        Rotate xRotate;
        Rotate yRotate;
        group.getTransforms().addAll(
                xRotate = new Rotate(0, Rotate.X_AXIS),
                yRotate = new Rotate(0, Rotate.Y_AXIS)
        );
        xRotate.angleProperty().bind(angleX);
        yRotate.angleProperty().bind(angleY);

        scene.setOnMouseClicked(mouseEvent -> {
            anchorX = mouseEvent.getSceneX();
            anchorY = mouseEvent.getSceneY();
            anchorAngleX = angleX.get();
            anchorAngleY = angleY.get();

        });

        scene.setOnMouseDragged(mouseEvent -> {
            angleX.set(anchorAngleX - (anchorY - mouseEvent.getSceneY()));
            angleY.set(anchorAngleY - (anchorX - mouseEvent.getSceneX()));

        });
    }
//    private void initMouseControl(Group group, SubScene scene){
//        Rotate xRotate;
//        Rotate yRotate;
//        group.getTransforms().addAll(
//                xRotate = new Rotate(0, Rotate.X_AXIS),
//                yRotate = new Rotate(0, Rotate.Y_AXIS)
//        );
//        xRotate.angleProperty().bind(angleX);
//        yRotate.angleProperty().bind(angleY);
//
//        scene.setOnMouseClicked(mouseEvent -> {
//            anchorX = mouseEvent.getSceneX();
//            anchorY = mouseEvent.getSceneY();
//            anchorAngleX = angleX.get();
//            anchorAngleY = angleY.get();
//
//        });
//
//        scene.setOnMouseDragged(mouseEvent -> {
//            angleX.set(anchorAngleX - (anchorY - mouseEvent.getSceneY()));
//            angleY.set(anchorAngleY - (anchorX - mouseEvent.getSceneX()));
//
//        });
//    }

//    private void setupMouseControl(SubScene scene, MovingObject[] objects) {
//        anchorX = 0;
//        anchorY = 0;
//        double elasticityFactor = 0.5;
//
//        scene.setOnMousePressed(mouseEvent -> {
//            anchorX = mouseEvent.getX();
//            anchorY = mouseEvent.getY();
//            System.out.println("Mouse pressed at: " + anchorX + ", " + anchorY);
//
//            slingLine = new Line(anchorX, anchorY, anchorX, anchorY);
//            slingLine.setStroke(Color.RED);
//            slingLine.setStrokeWidth(2);
//            ((Group) scene.getRoot()).getChildren().add(slingLine);
//        });
//
//        scene.setOnMouseDragged(mouseEvent -> {
//            double dragX = mouseEvent.getSceneX();
//            double dragY = mouseEvent.getSceneY();
//            System.out.println("Mouse dragged to: " + dragX + ", " + dragY);
//
//            // Update the end of the sling line to the current drag position
//            slingLine.setEndX(dragX);
//            slingLine.setEndY(dragY);
//        });
//
//        scene.setOnMouseReleased(mouseEvent -> {
//            double releaseX = mouseEvent.getSceneX();
//            double releaseY = mouseEvent.getSceneY();
//            System.out.println("Mouse released at: " + releaseX + ", " + releaseY);
//
//            // Remove the sling visualization line
//            ((Group) scene.getRoot()).getChildren().remove(slingLine);
//
//        });
//    }
    /**
     * The entry point of the application. This method is called when the application starts.
     *