From 1dcd5b3614e30651d97f4f81b98b1c381c6527b0 Mon Sep 17 00:00:00 2001 From: Quinn Date: Wed, 5 Apr 2023 23:28:21 -0500 Subject: [PATCH] We now have full functionality like we did in Ray-Tracing-2. It's time to begin adding features. --- src/Car.java | 26 ++++++++++++++----------- src/Processing.java | 47 +++++++++++++++++++++++++++++++++++++++++++-- src/SLAM.java | 19 ++++++++++++------ src/View.java | 18 ++++++++++------- src/Wall.java | 19 +++++++++++------- 5 files changed, 96 insertions(+), 33 deletions(-) diff --git a/src/Car.java b/src/Car.java index 8e3c209..6a53891 100644 --- a/src/Car.java +++ b/src/Car.java @@ -6,20 +6,25 @@ import static processing.core.PApplet.radians; import processing.core.PVector; import processing.core.PApplet; -public class Car extends PApplet{ +public class Car{ PVector pose = new PVector(0,0); // the car's x, y position float angle = 0; // the current angle that the car is at. int carLength = 50; int carWidth = 40; - SLAM slam = new SLAM(); + SLAM slam; + private static PApplet proc; - ArrayList views = new ArrayList(); - ArrayList points = new ArrayList(); + ArrayList views = new ArrayList<>(); // default constructor - Car(){} + Car(PApplet processing){ + this.proc = processing; + slam = new SLAM(proc); + } - Car(int xPos, int yPos, int carLength, int carWidth){ + Car(PApplet processing, int xPos, int yPos, int carLength, int carWidth){ + this.proc = processing; + slam = new SLAM(proc); this.pose = new PVector(xPos, yPos); this.carLength = carLength; this.carWidth = carWidth; @@ -27,17 +32,17 @@ public class Car extends PApplet{ //adds a new view with the specified FOV and ray number public void addView(float FOV, int numberOfRays){ - views.add(new View(pose, numberOfRays, radians(FOV))); + views.add(new View(proc, pose, numberOfRays, radians(FOV))); } //draw the car and its views public void drawCar(ArrayList walls){ - stroke(255); - ellipse(pose.x, pose.y, carWidth, carLength); + proc.stroke(255); + proc.ellipse(pose.x, pose.y, carWidth, carLength); this.updateScan(walls); } - //With all of the views that the car has, get their point list + //With all the views that the car has, get their point list void updateScan(ArrayList walls){ for(View view : views){ view.look(walls); @@ -68,7 +73,6 @@ public class Car extends PApplet{ for(View view : views){ view.setAngle(angle); } - return; } public void setPose(PVector newPose){ diff --git a/src/Processing.java b/src/Processing.java index 59d6e66..3084f03 100644 --- a/src/Processing.java +++ b/src/Processing.java @@ -1,17 +1,60 @@ import processing.core.PApplet; +import processing.core.PVector; + +import java.util.ArrayList; public class Processing extends PApplet { + + Car car; + ArrayList objects = new ArrayList(); + + public static PApplet processing; + public static void main(String[] args) { PApplet.main("Processing"); } public void settings(){ - size(200, 200); + processing = this; + car = new Car(processing, 100,100,50,40); + size(1000, 1000); + car.addView(60,6); + for(int i = 0; i < 20; i++){ + Wall wall = new Wall(processing, new PVector((int)random(40, 1840), (int)random(40, 960)), (int)random(360), (int)random(100, 1000)); + objects.add(wall); + } } public void draw(){ background(0); - ellipse(mouseX, mouseY, 20, 20); + for(Wall object : objects){ + object.drawWall(); + } + car.drawCar(objects); + //car.drive(new int[] {0, 0}); + } + + public void keyPressed(){ + if(key == 'd'){ + car.setPose(car.getPose().add(1, 0)); + } + if(key == 'w'){ + car.setPose(car.getPose().add(0, -1)); + } + if(key == 'a'){ + car.setPose(car.getPose().add(-1, 0)); + } + if(key == 's'){ + car.setPose(car.getPose().add(0, 1)); + } + if(key == 'q'){ + car.setAngle(car.getAngle()+1); + } + if(key == 'e'){ + car.setAngle(car.getAngle()-1); + } + } + } \ No newline at end of file diff --git a/src/SLAM.java b/src/SLAM.java index 4d47bc7..7185753 100644 --- a/src/SLAM.java +++ b/src/SLAM.java @@ -4,14 +4,16 @@ import java.util.ArrayList; import static processing.core.PApplet.pow; -public class SLAM extends PApplet{ +public class SLAM{ ArrayList points = new ArrayList(); + private static PApplet proc; - SLAM(){ + SLAM(PApplet processing){ + proc = processing; } public void addPoints(ArrayList newPoints){ - Line line = new Line(newPoints); + Line line = new Line(proc, newPoints); } } @@ -19,19 +21,24 @@ public class SLAM extends PApplet{ class Line{ PVector direction = new PVector(0,0); PVector position = new PVector(0,0); + private static PApplet proc; - Line(){} - Line(PVector direction, PVector position){ + Line(PApplet processing){ + proc = processing; + } + Line(PApplet processing, PVector direction, PVector position){ this.direction = direction; this.position = position; + proc = processing; } /** * @brief attempt to find the line of best fit for the given points * @param points the points to get the line of best for */ - Line(ArrayList points){ + Line(PApplet processing, ArrayList points){ bestFit(points); + proc = processing; } // least squares line of best fit algorithm diff --git a/src/View.java b/src/View.java index feb6c4d..3ed973a 100644 --- a/src/View.java +++ b/src/View.java @@ -4,14 +4,16 @@ import java.util.Objects; import static processing.core.PApplet.*; -public class View extends PApplet{ +public class View{ PVector pose; float angle = 0; float FOV; - ArrayList rays = new ArrayList(); + ArrayList rays = new ArrayList<>(); + private static PApplet proc; //the x,y position of the view, what angle it's looking at and its FOV - View(PVector newPose, int numberOfRays, float FOV){ + View(PApplet processing, PVector newPose, int numberOfRays, float FOV){ + proc = processing; this.pose = newPose; this.FOV = FOV; this.setRayNum(numberOfRays, FOV, this.angle); @@ -23,7 +25,7 @@ public class View extends PApplet{ rays.clear(); float angle = (float) (0.01-angleOffset); //the 0.01 fixes some bugs for(int i = 0; i < numberOfRays; i++){ - Ray ray = new Ray(pose, 100000, angle); + Ray ray = new Ray(proc, pose, 100000, angle); angle = angle + rayStep; rays.add(ray); } @@ -77,14 +79,16 @@ public class View extends PApplet{ } } -class Ray extends PApplet{ +class Ray{ PVector pose; int rayLength; int defaultRayLength; float angle; // IN RADIANS + private static PApplet proc; //takes the starting position of the ray, the length of the ray, and it's casting angle (radians) - Ray(PVector position, int defaultRayLength, float angle){ + Ray(PApplet processing, PVector position, int defaultRayLength, float angle){ + proc = processing; this.pose = position; this.defaultRayLength = defaultRayLength; this.rayLength = defaultRayLength; @@ -92,7 +96,7 @@ class Ray extends PApplet{ } public void drawRay(){ - line(pose.x, pose.y, (pose.x + cos(angle)*rayLength), (pose.y + sin(angle)*rayLength)); + proc.line(pose.x, pose.y, (pose.x + cos(angle)*rayLength), (pose.y + sin(angle)*rayLength)); } //checks to see at what coordinate the ray will collide with an object and sets the ray length to meet that point. diff --git a/src/Wall.java b/src/Wall.java index cfc47f7..ee2a0bd 100644 --- a/src/Wall.java +++ b/src/Wall.java @@ -2,23 +2,28 @@ import processing.core.*; import static processing.core.PApplet.*; -public class Wall extends PApplet{ +public class Wall{ PVector pos; float angle; int wallLength; - int r = (int)random(50, 255); - int g = (int)random(50, 255); - int b = (int)random(50, 255); + private static PApplet proc; + int r; + int g; + int b; - Wall(PVector pos, float angle, int wallLength){ + Wall(PApplet processing, PVector pos, float angle, int wallLength){ + proc = processing; this.pos = pos; this.angle = angle; this.wallLength = wallLength; + r = (int)proc.random(50, 255); + g = (int)proc.random(50, 255); + b = (int)proc.random(50, 255); } void drawWall(){ - stroke(r,g,b); - line(pos.x, pos.y, (pos.x + cos(radians(angle))*wallLength), (pos.y + sin(radians(angle))*wallLength)); + proc.stroke(r,g,b); + proc.line(pos.x, pos.y, (pos.x + cos(radians(angle))*wallLength), (pos.y + sin(radians(angle))*wallLength)); //ellipse((xPos + cos(radians(angle))*wallLength), (yPos + sin(radians(angle))*wallLength), 20, 20); }