Commit 01a6c3cf authored by Louis  Jimenez-Hernandez's avatar Louis Jimenez-Hernandez
Browse files

Added Obstacles and Window Class

parent e8016a03
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -9,7 +9,7 @@
    </option>
    <option name="workspaceImportForciblyTurnedOn" value="true" />
  </component>
  <component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="21" 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
+106 −0
Original line number Diff line number Diff line
package Shape;

import javafx.geometry.Bounds;
import javafx.scene.Group;
import javafx.scene.paint.Color;
import javafx.scene.paint.PhongMaterial;
import javafx.scene.shape.Box;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;

public class Obstacle {

    public Obstacle(){
        WindowSize = 500;
        xTranslation = 605;
        yTranslation = 205;
        zTranslation = 5;

        double x, y, z;
        String posKey;
        for(int i = 0; i<100; i++){
            Random rand = new Random();
            do {
                x = rand.nextDouble() * 350 + 100; // Random position for x
                y = rand.nextDouble() * 350 + 100; // Random position for y
                z = rand.nextDouble() * 350 + 100; // Random position for z
                posKey = x + "," + y + "," + z;   // Create a key for the set
            } while (occupiedPositions.contains(posKey)); // Ensure uniqueness

            occupiedPositions.add(posKey); // Add the new position to the set

            Box cube = new Box(obSize,obSize,obSize);
            cube.setMaterial(new PhongMaterial(Color.ORANGERED));
            cube.setTranslateX(xTranslation + (x));
            cube.setTranslateY(yTranslation + (y));
            cube.setTranslateZ(zTranslation + (z));
            obstacles.add(cube);
            obstacleBounds.add(cube.getBoundsInParent());
        }
    }

    public Obstacle(int WindowSize, int xTranslation, int yTranslation, int obSize, int objNum){
        this.obSize = obSize/2;
        this.WindowSize = WindowSize;
        this.xTranslation = xTranslation + obSize/2;
        this.yTranslation = yTranslation + obSize/2;
        zTranslation = obSize/2;
        this.objNum = objNum;

        double x, y, z;
        String posKey;
        for(int i = 0; i<objNum; i++){
            Random rand = new Random();
            do {
                x = rand.nextDouble() * 350 + 100; // Random position for x
                y = rand.nextDouble() * 350 + 100; // Random position for y
                z = rand.nextDouble() * 350 + 100; // Random position for z
                posKey = x + "," + y + "," + z;   // Create a key for the set
            } while (occupiedPositions.contains(posKey)); // Ensure uniqueness

            occupiedPositions.add(posKey); // Add the new position to the set

            Box cube = new Box(obSize,obSize,obSize);
            cube.setMaterial(new PhongMaterial(Color.ORANGERED));
            cube.setTranslateX(xTranslation + (x));
            cube.setTranslateY(yTranslation + (y));
            cube.setTranslateZ(zTranslation + (z));
            obstacles.add(cube);
            obstacleBounds.add(cube.getBoundsInParent());
        }
    }


    public void addToGroup(Group group){
        for(Box b: obstacles){
            group.getChildren().add(b);
        }
    }
    public ArrayList<Box> getObstacles() {
        return obstacles;
    }

    public ArrayList<Bounds> getObstacleBounds() {
        return obstacleBounds;
    }

    public boolean checkCollision(Bounds otherBounds) {
        for (Bounds bounds : obstacleBounds) {
            if (bounds.intersects(otherBounds)) {
                return true;
            }
        }
        return false;
    }

    int WindowSize = 0;
    int xTranslation, yTranslation, zTranslation, objNum;

    int obSize = 10;
    Set<String> occupiedPositions = new HashSet<>();
    ArrayList<Box> obstacles = new ArrayList<Box>();
    ArrayList<Bounds> obstacleBounds = new ArrayList<Bounds>();
}
+57 −0
Original line number Diff line number Diff line
package Shape;


import javafx.scene.shape.Box;
import javafx.scene.shape.DrawMode;
import javafx.scene.transform.Translate;

public class VisibleWindow {

    public VisibleWindow(){
        WindowSize = 300;
        halfWSize = WindowSize/2;
        xOffset = 600;
        xTranslation = halfWSize + xOffset;
        yOffset = 300;
        yTranslation = halfWSize + yOffset;
        zTranslation = halfWSize;
    }
    public VisibleWindow(int WindowSize,  int xOffset, int yOffset){
        this.WindowSize = WindowSize;
        halfWSize = WindowSize/2;
        this.xOffset = xOffset;
        xTranslation = halfWSize + xOffset;
        this.yOffset = yOffset;
        yTranslation = halfWSize + yOffset;
        zTranslation = halfWSize;
    }



    public Box makeWindow(){
        Box window = new Box(WindowSize,WindowSize,WindowSize);
        window.setDrawMode(DrawMode.LINE);
        Translate windowTranslation = new Translate();
        windowTranslation.setX(xTranslation);
        windowTranslation.setY(yTranslation);
        windowTranslation.setZ(zTranslation);
        window.getTransforms().add(windowTranslation);

        return window;
    }


    public int getXOffset() {
        return xOffset;
    }

    public int getYOffset() {
        return yOffset;
    }

    public int getWindowSize(){return WindowSize;
    }
    int WindowSize = 0;
    int halfWSize = 0;
    int xOffset, yOffset, xTranslation, yTranslation, zTranslation;
}
+30 −16
Original line number Diff line number Diff line
package org.example.newmat;
import Graph.readFile;
import Shape.MovingObject;
import Shape.VisibleWindow;
import Shape.Obstacle;

import javafx.animation.AnimationTimer;
import javafx.application.Application;
@@ -42,27 +44,21 @@ public class GraphicsAndWindowsTest extends Application {
     */
    @Override
    public void start(Stage stage) throws IOException {
        Box cube = new Box(300,300,300);
        cube.setDrawMode(DrawMode.LINE);
        Translate translate1 = new Translate();
        // Set the translation values
        //Cube is centered at (600,300,150)
        translate1.setX(750);
        translate1.setY(450);
        translate1.setZ(150);

        cube.getTransforms().add(translate1);
        VisibleWindow sceneWindow = new VisibleWindow(500, 600, 200);
        Box ourWindow = sceneWindow.makeWindow();
        Obstacle obs = new Obstacle(sceneWindow.getWindowSize(),sceneWindow.getXOffset(), sceneWindow.getYOffset(),10,100);

        MovingObject newObject = new MovingObject(getParameters().getRaw().toArray(new String[0]));

        // Create a Translate transform
        // Set the translation values
        newObject.setTranslate(600,300,0);
        newObject.setTranslate(sceneWindow.getXOffset(), sceneWindow.getYOffset(),0);

        Group group3D = new Group(); // Group of our 3D elements
        group3D.getChildren().add(newObject.getMesh());
        group3D.getChildren().add(cube);
        group3D.getChildren().add(ourWindow);
        group3D.getChildren().add(new AmbientLight(Color.WHITE));
        obs.addToGroup(group3D);

        // 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
@@ -169,9 +165,12 @@ public class GraphicsAndWindowsTest extends Application {
                //Number is roughly 1 mill divided by 60
                if (now - lastUpdate >= 16_000_000) { // Roughly 60 frames per second

                    double cubeMin = 300, cubeMax = 600;
                    int lastChanged = 1;
                    double winLeftXBound = sceneWindow.getXOffset();
                    double winTopYBound = sceneWindow.getYOffset();
                    double WindowSize = sceneWindow.getWindowSize();
                    // Reverse direction at bounds
                    if (minX < cubeMin+300 || maxX > cubeMax+300) {
                    if (minX < winLeftXBound || maxX > winLeftXBound + WindowSize) {

                        // load the vertices of the mesh
                        //float[] points = mesh.getPoints().toArray(null);
@@ -187,9 +186,10 @@ public class GraphicsAndWindowsTest extends Application {

                        //update the direction of the mesh and set the new coordinates
                        directionX *= -1;
                        lastChanged = 1;
                        //mesh.getPoints().setAll(points);
                    }
                    if (minY < cubeMin || maxY > cubeMax) {
                    if (minY < winTopYBound || maxY > winTopYBound+WindowSize) {

                        // load the vertices of the mesh
                        //float[] points = mesh.getPoints().toArray(null);
@@ -203,9 +203,10 @@ public class GraphicsAndWindowsTest extends Application {
                        */
                        //update the direction of the mesh and set the new coordinates
                        directionY *= -1;
                        lastChanged = 2;
                        //mesh.getPoints().setAll(points);
                    }
                    if (minZ < cubeMin-300 || maxZ > cubeMax-300) {
                    if (minZ < 0 || maxZ > WindowSize) {

                        // load the vertices of the mesh
                        //float[] points = mesh.getPoints().toArray(null);
@@ -220,9 +221,22 @@ public class GraphicsAndWindowsTest extends Application {

                        //update the direction of the mesh and set the new coordinates
                        directionZ *= -1;
                        lastChanged = 3;
                        //mesh.getPoints().setAll(points);

                    }
                    if(obs.checkCollision(meshBounds)){
                        switch (lastChanged){
                            case 1: directionX *= -1;

                            case 2: directionY *= -1;

                            case 3: directionZ *= -1;

                            default: System.out.println();
                        }
                        directionY *= -1;
                    }

                    double newY = newObject.getTranslate().getY() + directionY;
                    double newX = newObject.getTranslate().getX() + directionX;
+4.09 KiB

File added.

No diff preview for this file type.

Loading