Commit 40cdd69d authored by Louis  Jimenez-Hernandez's avatar Louis Jimenez-Hernandez
Browse files

Obstacles more customizable + ObstacleField added

parent 4c3d44f5
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="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
+35 −77
Original line number Diff line number Diff line
@@ -2,9 +2,12 @@ package Shape;

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

import java.util.ArrayList;
import java.util.HashSet;
@@ -13,94 +16,49 @@ 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){
    public Obstacle(int WindowSize, double xTranslation, double yTranslation,
                    double zTranslation, int obSize, String shape, Color color){
        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());
        }
        this.xTranslation = xTranslation + (double) obSize/2;
        this.yTranslation = yTranslation + (double) obSize/2;
        this.zTranslation = zTranslation + (double) obSize/2;
        this.shape = makeNode(shape, color);
        this.shape.setTranslateX(xTranslation);
        this.shape.setTranslateY(yTranslation);
        this.shape.setTranslateZ(zTranslation);
        shapeBound = this.shape.getBoundsInParent();
    }


    public void addToGroup(Group group){
        for(Box b: obstacles){
            group.getChildren().add(b);
        }
    public Node makeNode(String shape, Color color){
        if(shape.equals("Box")){
            Box b = new Box(obSize,obSize,obSize);
            b.setMaterial(new PhongMaterial(color));
            return b;
        } else if (shape.equals("Sphere")){
            Sphere s = new Sphere(obSize);
            s.setMaterial(new PhongMaterial(color));
            return s;
        } else {
            Cylinder c = new Cylinder(obSize,obSize);
            c.setMaterial(new PhongMaterial(color));
            return c;
        }
    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;
    public Node getShape(){
        return shape;
    }
    public void addToGroup(Group group){
        group.getChildren().add(shape);
    }
        return false;
    public boolean checkCollision(Bounds otherBounds) {
        return shapeBound.intersects(otherBounds);
    }

    int WindowSize = 0;
    int xTranslation, yTranslation, zTranslation, objNum;
    double xTranslation, yTranslation, zTranslation;
    Node shape;
    Bounds shapeBound;

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

import Shape.Obstacle;
import javafx.geometry.Bounds;
import javafx.scene.Group;
import javafx.scene.Node;
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 ObstacleField {
//    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 ObstacleField(int windowSize, int xTranslation, int yTranslation,
                         int range, int min, int obSize, int objNum,
                         String shape, Color color){
        this.obSize = obSize/2;
        this.windowSize = windowSize;
        this.xTranslation = xTranslation + obSize/2;
        this.yTranslation = yTranslation + obSize/2;
        zTranslation = obSize/2;

        double x, y, z;
        String posKey;
        for(int i = 0; i<objNum; i++){
            Random rand = new Random();
            do {
                x = rand.nextDouble() * range + min; // Random position for x
                y = rand.nextDouble() * range + min; // Random position for y
                z = rand.nextDouble() * range + min; // 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

            Obstacle ob = new Obstacle(this.windowSize, this.xTranslation + x, this.yTranslation +y,
                    this.zTranslation + z, this.obSize, shape, color);
            obstacles.add(ob);
            obstacleBounds.add(ob.getShape().getBoundsInParent());
        }
    }


    public void addToGroup(Group group){
        for(Obstacle o: obstacles){
            group.getChildren().add(o.getShape());
        }
    }
    public ArrayList<Obstacle> 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;
    }

    public void addToField(Obstacle o){
        obstacles.add(o);
        obstacleBounds.add(o.getShape().getBoundsInParent());
    }

    private int windowSize, obSize;
    private int xTranslation,yTranslation,zTranslation;
    Set<String> occupiedPositions = new HashSet<>();
    ArrayList<Obstacle> obstacles = new ArrayList<Obstacle>();
    ArrayList<Bounds> obstacleBounds = new ArrayList<Bounds>();
}
+4 −2
Original line number Diff line number Diff line
@@ -3,6 +3,7 @@ import Graph.readFile;
import Shape.MovingObject;
import Shape.VisibleWindow;
import Shape.Obstacle;
import Shape.ObstacleField;

import javafx.animation.AnimationTimer;
import javafx.application.Application;
@@ -50,8 +51,9 @@ public class GraphicsAndWindowsTest extends Application {
        VisibleWindow sceneWindow = new VisibleWindow(500, -250, -250);
        Box ourWindow = sceneWindow.makeWindow();

        //Obstacle obs = new Obstacle(sceneWindow.getWindowSize(),sceneWindow.getXOffset(), sceneWindow.getYOffset(),10,100);

//        ObstacleField obs = new ObstacleField(sceneWindow.getWindowSize(),sceneWindow.getXOffset(),
//                sceneWindow.getYOffset(),350,100, 20, 50, "Box",
//                Color.ORANGERED);
        String[] ob2 = {"--image", "src/teapot.txt", "--speed", "20", "--dir", "4,", "10,", "6"};
        String[] ob3 = {"--image", "src/armadillo.txt", "--speed", "5", "--dir", "1,", "1,", "1"};
        String[] ob4 = {"--image", "src/teapot.txt", "--speed", "100", "--dir", "3,", "5,", "7"};
−1.66 KiB (2.43 KiB)

File changed.

No diff preview for this file type.

Loading