Loading src/main/java/Shape/Obstacle.java +55 −9 Original line number Diff line number Diff line Loading @@ -7,11 +7,37 @@ import javafx.scene.paint.Color; import javafx.scene.paint.PhongMaterial; import javafx.scene.shape.*; /** * The Obstacle class represents a 3D object that can be used in a JavaFX scene. * This class allows for the creation of obstacles with different shapes and colors, * and can handle collision detection against other objects within the scene. */ public class Obstacle { public int obSize = 10; public int WindowSize = 0; public double xTranslation; public double yTranslation; public double zTranslation; public Node shape; public Bounds shapeBound; public boolean removable; /** * Constructs an Obstacle object with specified properties. * * @param WindowSize The size of the window in which the obstacle exists. * @param xTranslation The x translation of the obstacle from the origin. * @param yTranslation The y translation of the obstacle from the origin. * @param zTranslation The z translation of the obstacle from the origin. * @param obSize The size of the obstacle. * @param shape The geometric shape of the obstacle (Box, Sphere, or Cylinder). * @param color The color of the obstacle. * @param removable Specifies whether the obstacle is removable or not. */ public Obstacle(int WindowSize, double xTranslation, double yTranslation, double zTranslation, int obSize, String shape, Color color , boolean immovable){ , boolean removable){ this.obSize = obSize/2; this.WindowSize = WindowSize; this.xTranslation = xTranslation + (double) obSize/2; Loading @@ -21,10 +47,17 @@ public class Obstacle { this.shape.setTranslateX(xTranslation); this.shape.setTranslateY(yTranslation); this.shape.setTranslateZ(zTranslation); this.removable = immovable; this.removable = removable; shapeBound = this.shape.getBoundsInParent(); } /** * Creates a node based on the specified shape and color. * * @param shape The shape type ("Box", "Sphere", or "Cylinder"). * @param color The color of the shape. * @return Node representing the shape with the specified properties. */ public Node makeNode(String shape, Color color){ if(shape.equals("Box")){ Box b = new Box(obSize,obSize,obSize); Loading @@ -41,17 +74,36 @@ public class Obstacle { } } /** * Returns the shape of the obstacle as a Node. * * @return Node representing the obstacle's shape. */ public Node getShape(){ return shape; } /** * Adds the shape of this obstacle to a specified group. * * @param group The group to which the shape will be added. */ public void addToGroup(Group group){ group.getChildren().add(shape); } /** * Checks for collision between this obstacle and another bounding box. * * @param otherBounds The bounds of the other object to check for collision. * @return A string indicating the side of collision ("LEFT", "RIGHT", "TOP", "BOTTOM", "FRONT", "BACK", "REMOVABLE", "NONE"). */ public String checkCollision(Bounds otherBounds) { if (shapeBound.intersects(otherBounds)) { if(removable){ return "REMOVABLE"; } //Figures out distance between bounds for collision detection 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()); Loading @@ -59,7 +111,7 @@ public class Obstacle { // Determine minimum overlap if (deltaX < deltaY && deltaX < deltaZ) { if (shapeBound.getMaxX() > otherBounds.getMaxX()) { return "RIGHT"; //RIGHT HIT return "RIGHT"; } else { return "LEFT"; } Loading @@ -79,11 +131,5 @@ public class Obstacle { } return "NONE"; } boolean removable; int WindowSize = 0; double xTranslation, yTranslation, zTranslation; Node shape; Bounds shapeBound; int obSize = 10; } src/main/java/Shape/ObstacleField.java +57 −14 Original line number Diff line number Diff line 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.*; /** * The ObstacleField class manages a collection of Obstacle objects in a 3D space. * It allows for the dynamic creation of obstacles, managing their positions, and checking for collisions. */ public class ObstacleField { public int windowSize, obSize; public int xTranslation, yTranslation, zTranslation; Set<String> occupiedPositions = new HashSet<>(); ArrayList<Obstacle> obstacles = new ArrayList<>(); ArrayList<Bounds> obstacleBounds = new ArrayList<>(); /** * Constructs an ObstacleField with a specified number of obstacles. * * @param windowSize The size of the window in which the obstacles are placed. * @param xTranslation The x offset for the initial translation of obstacles. * @param yTranslation The y offset for the initial translation of obstacles. * @param range The range within which random positions will be generated. * @param min The minimum value for the position offsets. * @param obSize The size of each obstacle. * @param objNum The number of obstacles to generate. * @param shape The shape of the obstacles (e.g., "Box", "Sphere"). * @param color The color of the obstacles. * @param Removable Indicates whether the obstacles are immovable. */ public ObstacleField(int windowSize, int xTranslation, int yTranslation, int range, int min, int obSize, int objNum, String shape, Color color, boolean immovable){ String shape, Color color, boolean Removable){ this.obSize = obSize/2; this.windowSize = windowSize; this.xTranslation = xTranslation + obSize/2; Loading @@ -37,26 +54,47 @@ 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, immovable); this.zTranslation + z, this.obSize, shape, color, Removable); obstacles.add(ob); obstacleBounds.add(ob.getShape().getBoundsInParent()); } } /** * Adds all obstacle shapes to a specified group. * * @param group The group to which the obstacle shapes are added. */ public void addToGroup(Group group){ for(Obstacle o: obstacles){ group.getChildren().add(o.getShape()); } } /** * Returns the list of obstacles. * * @return ArrayList of Obstacle objects. */ public ArrayList<Obstacle> getObstacles() { return obstacles; } /** * Returns the bounds of all obstacles in this field. * * @return ArrayList of Bounds objects. */ public ArrayList<Bounds> getObstacleBounds() { return obstacleBounds; } /** * Checks for collision between the bounds of any obstacle in the field and another set of bounds. * * @param otherBounds The bounds of another object to check for collisions. * @return List containing collision information and the obstacle involved, or "NONE" and null if no collision. */ public List<Object> checkCollision(Bounds otherBounds) { for (Obstacle o : obstacles) { if(o.shapeBound.intersects(otherBounds)){ Loading @@ -66,18 +104,23 @@ public class ObstacleField { return Arrays.asList("NONE", null); // No collision detected } /** * Adds an obstacle to the field. * * @param o The obstacle to be added. */ public void addToField(Obstacle o){ obstacles.add(o); obstacleBounds.add(o.getShape().getBoundsInParent()); } /** * Removes an obstacle from the field. * * @param o The obstacle to be removed. */ public void removeField(Obstacle o){ obstacles.remove(o); } 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>(); } src/main/java/org/example/newmat/GraphicsAndWindowsTest.java +9 −3 Original line number Diff line number Diff line Loading @@ -65,8 +65,9 @@ public class GraphicsAndWindowsTest extends Application { Box ourWindow = sceneWindow.makeWindow(); //Do to sceneWindow.getX and YOffsets() the range assume you start from the origin of the visible window ObstacleField obs = new ObstacleField(sceneWindow.getWindowSize(),sceneWindow.getXOffset(), sceneWindow.getYOffset(),300,0, 50, 100, "Box", sceneWindow.getYOffset(),450,0, 50, 100, "Box", Color.ORANGERED, true); String[] ob2 = {"--image", "src/teapot.txt", "--speed", "32", "--dir", "4,", "7,", "10"}; Loading Loading @@ -156,6 +157,9 @@ public class GraphicsAndWindowsTest extends Application { Label fpsLabel = new Label("FPS: 0"); fpsLabel.setTextFill(Color.WHITE); /* * Text label to display Score */ Label scoreLabel = new Label("Score: 0"); fpsLabel.setTextFill(Color.WHITE); //Lays out the children in Z order with the last member being front most Loading Loading @@ -344,11 +348,13 @@ public class GraphicsAndWindowsTest extends Application { direction[i][2] *= -1; lastChanged = 3; } //Essentially a Pair of (String, corresponding obstacle) List<Object> collisionSide = obs.checkCollision(meshBounds[i]); if(!(collisionSide.get(0).equals("NONE"))){ Bounds obBounds = ((Obstacle) collisionSide.get(1)).getShape().getBoundsInParent(); //Handles Collision cases switch ((String)collisionSide.get(0)){ //For game, increments score and "eats" the obstacle on collision case "REMOVABLE": Obstacle o = ((Obstacle) collisionSide.get(1)); group3D.getChildren().remove(o.getShape()); Loading @@ -365,7 +371,7 @@ public class GraphicsAndWindowsTest extends Application { break; case "TOP": y = direction[i][1] - (obBounds.getMaxY() + (meshBounds[i].getMinY() + y)); //y = direction[i][1] - (obBounds.getMaxY() + (meshBounds[i].getMinY() + y)); direction[i][1] = abs(direction[i][1]); break; Loading target/classes/Shape/Obstacle.class −12 B (3.11 KiB) File changed.No diff preview for this file type. View original file View changed file target/classes/Shape/ObstacleField.class (4.04 KiB) File changed.No diff preview for this file type. View original file View changed file Loading
src/main/java/Shape/Obstacle.java +55 −9 Original line number Diff line number Diff line Loading @@ -7,11 +7,37 @@ import javafx.scene.paint.Color; import javafx.scene.paint.PhongMaterial; import javafx.scene.shape.*; /** * The Obstacle class represents a 3D object that can be used in a JavaFX scene. * This class allows for the creation of obstacles with different shapes and colors, * and can handle collision detection against other objects within the scene. */ public class Obstacle { public int obSize = 10; public int WindowSize = 0; public double xTranslation; public double yTranslation; public double zTranslation; public Node shape; public Bounds shapeBound; public boolean removable; /** * Constructs an Obstacle object with specified properties. * * @param WindowSize The size of the window in which the obstacle exists. * @param xTranslation The x translation of the obstacle from the origin. * @param yTranslation The y translation of the obstacle from the origin. * @param zTranslation The z translation of the obstacle from the origin. * @param obSize The size of the obstacle. * @param shape The geometric shape of the obstacle (Box, Sphere, or Cylinder). * @param color The color of the obstacle. * @param removable Specifies whether the obstacle is removable or not. */ public Obstacle(int WindowSize, double xTranslation, double yTranslation, double zTranslation, int obSize, String shape, Color color , boolean immovable){ , boolean removable){ this.obSize = obSize/2; this.WindowSize = WindowSize; this.xTranslation = xTranslation + (double) obSize/2; Loading @@ -21,10 +47,17 @@ public class Obstacle { this.shape.setTranslateX(xTranslation); this.shape.setTranslateY(yTranslation); this.shape.setTranslateZ(zTranslation); this.removable = immovable; this.removable = removable; shapeBound = this.shape.getBoundsInParent(); } /** * Creates a node based on the specified shape and color. * * @param shape The shape type ("Box", "Sphere", or "Cylinder"). * @param color The color of the shape. * @return Node representing the shape with the specified properties. */ public Node makeNode(String shape, Color color){ if(shape.equals("Box")){ Box b = new Box(obSize,obSize,obSize); Loading @@ -41,17 +74,36 @@ public class Obstacle { } } /** * Returns the shape of the obstacle as a Node. * * @return Node representing the obstacle's shape. */ public Node getShape(){ return shape; } /** * Adds the shape of this obstacle to a specified group. * * @param group The group to which the shape will be added. */ public void addToGroup(Group group){ group.getChildren().add(shape); } /** * Checks for collision between this obstacle and another bounding box. * * @param otherBounds The bounds of the other object to check for collision. * @return A string indicating the side of collision ("LEFT", "RIGHT", "TOP", "BOTTOM", "FRONT", "BACK", "REMOVABLE", "NONE"). */ public String checkCollision(Bounds otherBounds) { if (shapeBound.intersects(otherBounds)) { if(removable){ return "REMOVABLE"; } //Figures out distance between bounds for collision detection 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()); Loading @@ -59,7 +111,7 @@ public class Obstacle { // Determine minimum overlap if (deltaX < deltaY && deltaX < deltaZ) { if (shapeBound.getMaxX() > otherBounds.getMaxX()) { return "RIGHT"; //RIGHT HIT return "RIGHT"; } else { return "LEFT"; } Loading @@ -79,11 +131,5 @@ public class Obstacle { } return "NONE"; } boolean removable; int WindowSize = 0; double xTranslation, yTranslation, zTranslation; Node shape; Bounds shapeBound; int obSize = 10; }
src/main/java/Shape/ObstacleField.java +57 −14 Original line number Diff line number Diff line 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.*; /** * The ObstacleField class manages a collection of Obstacle objects in a 3D space. * It allows for the dynamic creation of obstacles, managing their positions, and checking for collisions. */ public class ObstacleField { public int windowSize, obSize; public int xTranslation, yTranslation, zTranslation; Set<String> occupiedPositions = new HashSet<>(); ArrayList<Obstacle> obstacles = new ArrayList<>(); ArrayList<Bounds> obstacleBounds = new ArrayList<>(); /** * Constructs an ObstacleField with a specified number of obstacles. * * @param windowSize The size of the window in which the obstacles are placed. * @param xTranslation The x offset for the initial translation of obstacles. * @param yTranslation The y offset for the initial translation of obstacles. * @param range The range within which random positions will be generated. * @param min The minimum value for the position offsets. * @param obSize The size of each obstacle. * @param objNum The number of obstacles to generate. * @param shape The shape of the obstacles (e.g., "Box", "Sphere"). * @param color The color of the obstacles. * @param Removable Indicates whether the obstacles are immovable. */ public ObstacleField(int windowSize, int xTranslation, int yTranslation, int range, int min, int obSize, int objNum, String shape, Color color, boolean immovable){ String shape, Color color, boolean Removable){ this.obSize = obSize/2; this.windowSize = windowSize; this.xTranslation = xTranslation + obSize/2; Loading @@ -37,26 +54,47 @@ 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, immovable); this.zTranslation + z, this.obSize, shape, color, Removable); obstacles.add(ob); obstacleBounds.add(ob.getShape().getBoundsInParent()); } } /** * Adds all obstacle shapes to a specified group. * * @param group The group to which the obstacle shapes are added. */ public void addToGroup(Group group){ for(Obstacle o: obstacles){ group.getChildren().add(o.getShape()); } } /** * Returns the list of obstacles. * * @return ArrayList of Obstacle objects. */ public ArrayList<Obstacle> getObstacles() { return obstacles; } /** * Returns the bounds of all obstacles in this field. * * @return ArrayList of Bounds objects. */ public ArrayList<Bounds> getObstacleBounds() { return obstacleBounds; } /** * Checks for collision between the bounds of any obstacle in the field and another set of bounds. * * @param otherBounds The bounds of another object to check for collisions. * @return List containing collision information and the obstacle involved, or "NONE" and null if no collision. */ public List<Object> checkCollision(Bounds otherBounds) { for (Obstacle o : obstacles) { if(o.shapeBound.intersects(otherBounds)){ Loading @@ -66,18 +104,23 @@ public class ObstacleField { return Arrays.asList("NONE", null); // No collision detected } /** * Adds an obstacle to the field. * * @param o The obstacle to be added. */ public void addToField(Obstacle o){ obstacles.add(o); obstacleBounds.add(o.getShape().getBoundsInParent()); } /** * Removes an obstacle from the field. * * @param o The obstacle to be removed. */ public void removeField(Obstacle o){ obstacles.remove(o); } 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>(); }
src/main/java/org/example/newmat/GraphicsAndWindowsTest.java +9 −3 Original line number Diff line number Diff line Loading @@ -65,8 +65,9 @@ public class GraphicsAndWindowsTest extends Application { Box ourWindow = sceneWindow.makeWindow(); //Do to sceneWindow.getX and YOffsets() the range assume you start from the origin of the visible window ObstacleField obs = new ObstacleField(sceneWindow.getWindowSize(),sceneWindow.getXOffset(), sceneWindow.getYOffset(),300,0, 50, 100, "Box", sceneWindow.getYOffset(),450,0, 50, 100, "Box", Color.ORANGERED, true); String[] ob2 = {"--image", "src/teapot.txt", "--speed", "32", "--dir", "4,", "7,", "10"}; Loading Loading @@ -156,6 +157,9 @@ public class GraphicsAndWindowsTest extends Application { Label fpsLabel = new Label("FPS: 0"); fpsLabel.setTextFill(Color.WHITE); /* * Text label to display Score */ Label scoreLabel = new Label("Score: 0"); fpsLabel.setTextFill(Color.WHITE); //Lays out the children in Z order with the last member being front most Loading Loading @@ -344,11 +348,13 @@ public class GraphicsAndWindowsTest extends Application { direction[i][2] *= -1; lastChanged = 3; } //Essentially a Pair of (String, corresponding obstacle) List<Object> collisionSide = obs.checkCollision(meshBounds[i]); if(!(collisionSide.get(0).equals("NONE"))){ Bounds obBounds = ((Obstacle) collisionSide.get(1)).getShape().getBoundsInParent(); //Handles Collision cases switch ((String)collisionSide.get(0)){ //For game, increments score and "eats" the obstacle on collision case "REMOVABLE": Obstacle o = ((Obstacle) collisionSide.get(1)); group3D.getChildren().remove(o.getShape()); Loading @@ -365,7 +371,7 @@ public class GraphicsAndWindowsTest extends Application { break; case "TOP": y = direction[i][1] - (obBounds.getMaxY() + (meshBounds[i].getMinY() + y)); //y = direction[i][1] - (obBounds.getMaxY() + (meshBounds[i].getMinY() + y)); direction[i][1] = abs(direction[i][1]); break; Loading
target/classes/Shape/Obstacle.class −12 B (3.11 KiB) File changed.No diff preview for this file type. View original file View changed file
target/classes/Shape/ObstacleField.class (4.04 KiB) File changed.No diff preview for this file type. View original file View changed file