Commit 918afff1 authored by Louis  Jimenez-Hernandez's avatar Louis Jimenez-Hernandez
Browse files

Obstacle Collision Revamp side detection

parent c57779bd
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
+37 −6
Original line number Diff line number Diff line
package Shape;

import javafx.geometry.Bounds;
import javafx.geometry.Point3D;
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 javafx.scene.shape.*;

import java.util.ArrayList;
import java.util.HashSet;
@@ -17,7 +16,8 @@ import java.util.Set;
public class Obstacle {

    public Obstacle(int WindowSize, double xTranslation, double yTranslation,
                    double zTranslation, int obSize, String shape, Color color){
                    double zTranslation, int obSize, String shape, Color color
            , boolean immovable){
        this.obSize = obSize/2;
        this.WindowSize = WindowSize;
        this.xTranslation = xTranslation + (double) obSize/2;
@@ -27,6 +27,7 @@ public class Obstacle {
        this.shape.setTranslateX(xTranslation);
        this.shape.setTranslateY(yTranslation);
        this.shape.setTranslateZ(zTranslation);
        this.immovable = immovable;
        shapeBound = this.shape.getBoundsInParent();
    }

@@ -52,9 +53,39 @@ public class Obstacle {
    public void addToGroup(Group group){
        group.getChildren().add(shape);
    }
    public boolean checkCollision(Bounds otherBounds) {
        return shapeBound.intersects(otherBounds);
    public String checkCollision(Bounds otherBounds) {
        if (shapeBound.intersects(otherBounds)) {
            if(immovable){
                return "IMMOVABLE";
            }
            double deltaX = Math.min(shapeBound.getMaxX() - otherBounds.getMinX(), otherBounds.getMaxX() - shapeBound.getMinX());
            double deltaY = Math.min(shapeBound.getMaxY() - otherBounds.getMinY(), otherBounds.getMaxY() - shapeBound.getMinY());
            double deltaZ = Math.min(shapeBound.getMaxZ() - otherBounds.getMinZ(), otherBounds.getMaxZ() - shapeBound.getMinZ());

            // Determine minimum overlap
            if (deltaX < deltaY && deltaX < deltaZ) {
                if (shapeBound.getMaxX() > otherBounds.getMaxX()) {
                    return "RIGHT"; //RIGHT HIT
                } else {
                    return "LEFT";
                }
            } else if (deltaY < deltaZ) {
                if (shapeBound.getMaxY() > otherBounds.getMaxY()) {
                    return "TOP";
                } else {
                    return "BOTTOM";
                }
            } else {
                if (shapeBound.getMaxZ() > otherBounds.getMaxZ()) {
                    return "FRONT";
                } else {
                    return "BACK";
                }
            }
        }
        return "NONE";
    }
    boolean immovable;
    int WindowSize = 0;
    double xTranslation, yTranslation, zTranslation;
    Node shape;
+9 −35
Original line number Diff line number Diff line
@@ -2,11 +2,13 @@ package Shape;

import Shape.Obstacle;
import javafx.geometry.Bounds;
import javafx.geometry.Point3D;
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.MeshView;

import java.util.ArrayList;
import java.util.HashSet;
@@ -14,38 +16,10 @@ 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){
                         String shape, Color color, boolean immovable){
        this.obSize = obSize/2;
        this.windowSize = windowSize;
        this.xTranslation = xTranslation + obSize/2;
@@ -66,7 +40,7 @@ public class ObstacleField {
            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);
                    this.zTranslation + z, this.obSize, shape, color, immovable);
            obstacles.add(ob);
            obstacleBounds.add(ob.getShape().getBoundsInParent());
        }
@@ -86,13 +60,13 @@ public class ObstacleField {
        return obstacleBounds;
    }

    public boolean checkCollision(Bounds otherBounds) {
        for (Bounds bounds : obstacleBounds) {
            if (bounds.intersects(otherBounds)) {
                return true;
    public String checkCollision(Bounds otherBounds) {
        for (Obstacle o : obstacles) {
            if(o.shapeBound.intersects(otherBounds)){
                return o.checkCollision(otherBounds);
            }
        }
        return false;
        return "NONE"; // No collision detected
    }

    public void addToField(Obstacle o){
+32 −11
Original line number Diff line number Diff line
@@ -24,6 +24,8 @@ import javafx.stage.Stage;
import java.io.IOException;
import java.util.Arrays;

import static java.lang.Math.abs;


/**
 * Demonstrates advanced JavaFX graphics and window management by creating a 3D scene with
@@ -52,9 +54,9 @@ public class GraphicsAndWindowsTest extends Application {
        Box ourWindow = sceneWindow.makeWindow();


//        ObstacleField obs = new ObstacleField(sceneWindow.getWindowSize(),sceneWindow.getXOffset(),
//                sceneWindow.getYOffset(),350,100, 20, 50, "Box",
//                Color.ORANGERED);
        ObstacleField obs = new ObstacleField(sceneWindow.getWindowSize(),sceneWindow.getXOffset(),
                sceneWindow.getYOffset(),400,50, 100, 60, "Box",
                Color.ORANGERED, false);

        String[] ob2 = {"--image", "src/teapot.txt", "--speed", "15", "--dir", "4,", "7,", "10"};
        String[] ob3 = {"--image", "src/teapot.txt", "--speed", "5", "--dir", "1,", "1,", "1"};
@@ -81,7 +83,7 @@ public class GraphicsAndWindowsTest extends Application {
        group3D.setTranslateZ(250);
        group3D.getChildren().add(ourWindow);
        group3D.getChildren().add(new AmbientLight(Color.WHITE));
        //obs.addToGroup(group3D);
        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
@@ -229,17 +231,36 @@ public class GraphicsAndWindowsTest extends Application {
                            lastChanged = 3;
                        }

                    /*if(obs.checkCollision(meshBounds[i])){
                        switch (lastChanged){
                            case 1: direction[i][0] *= -1;
                        String collisionSide = obs.checkCollision(meshBounds[i]);
                        if(!(collisionSide.equals("NONE"))){
                            switch (collisionSide){
                                case "LEFT":
                                    direction[i][0] = abs(direction[i][0]);
                                    break;

                                case "RIGHT":
                                    direction[i][0] = -1 * abs(direction[i][0]);
                                    break;

                            case 2: direction[i][1] *= -1;
                                case "TOP":
                                    direction[i][1] =  abs(direction[i][1]);
                                    break;

                            case 3: direction[i][2] *= -1;
                                case "BOTTOM":
                                    direction[i][1] =  -1 * abs(direction[i][1]);
                                    break;

                                case "FRONT":
                                    direction[i][2] =  abs(direction[i][2]);
                                    break;

                                case "BACK":
                                    direction[i][2] =  -1 * abs(direction[i][2]);
                                    break;

                                default: System.out.println();
                            }
                    }*/
                        }
                        objects[i].updateTranslate(x, y, z);
                        System.out.println(Arrays.toString(objects[i].getCentroid()));
                        //objects[i].updateRays();
+693 B (3.11 KiB)

File changed.

No diff preview for this file type.

Loading