Everything can now be drawn

This commit is contained in:
Quinn
2023-05-02 10:28:06 -05:00
parent 3321e77061
commit 1a4d6e6909
9 changed files with 59 additions and 72 deletions

View File

@@ -4,6 +4,7 @@ import static java.lang.Math.PI;
import static processing.core.PApplet.degrees;
import static processing.core.PApplet.radians;
import Graph.*;
import Vector.Vector;
import processing.core.PApplet;
@@ -37,17 +38,17 @@ public class Car{
}
//draw the car and its views
public void drawCar(ArrayList<Wall> walls){
public void drawCar(PointGraph g){
proc.stroke(255);
proc.ellipse(pose.x, pose.y, carWidth, carLength);
this.updateScan(walls);
this.updateScan(g);
// this.slam.drawLines();
}
//With all the views that the car has, get their point list
void updateScan(ArrayList<Wall> walls){
void updateScan(PointGraph map){
for(View view : views){
view.look(walls);
view.look(map);
}
for(View view : views){

View File

@@ -5,6 +5,8 @@ import processing.core.PApplet;
import java.awt.*;
import static java.lang.Math.PI;
public class LineEdge extends Edge implements LineInterface{
protected PointVertex vStart;
protected PointVertex vEnd;
@@ -41,5 +43,12 @@ public class LineEdge extends Edge implements LineInterface{
public void draw(PApplet proc){
line.draw(proc);
Vector leftFlange = line.getDirection().rotate2D((float)(-3*PI/4)).normalize().mul(20);
Vector rightFlange = line.getDirection().rotate2D((float) (3*PI/4)).normalize().mul(20);
Line l1 = new Line(line.endPoint(), line.endPoint().add(leftFlange));
Line l2 = new Line(line.endPoint(), line.endPoint().add(rightFlange));
l1.draw(proc);
l2.draw(proc);
}
}

View File

@@ -11,7 +11,7 @@ import java.util.ArrayList;
public class PointGraph extends Graph {
PointVertex selectedVertex;
PointGraph(){
public PointGraph(){
super();
}
@@ -26,6 +26,21 @@ public class PointGraph extends Graph {
}
}
public void addEdge(PointVertex vStart, PointVertex vEnd){
addVertex(vStart);
// don't add the edge if it is already added
for(Edge e : adjList.get(vStart)){
if(e.getEndVertex() == (Vertex) vEnd){
return;
}
}
addVertex(vEnd);
LineEdge edge = new LineEdge(vStart, vEnd);
adjList.get((Vertex) vStart).add(new LineEdge(vStart, vEnd));
}
/**
* @param v set this vertex as the selected vertex in the graph
*/

View File

@@ -10,7 +10,7 @@ public class PointVertex extends Vertex {
* @param xPos the x position of the vertex
* @param yPos the y posiiton of the vertex
*/
PointVertex(float xPos, float yPos){
public PointVertex(float xPos, float yPos){
super();
this.position = new Vector(xPos, yPos);
}

View File

@@ -1,14 +1,14 @@
import Vector.Vector;
import Graph.*;
import processing.core.PApplet;
import java.util.ArrayList;
public class Processing extends PApplet {
Car car;
ArrayList<Wall> objects = new ArrayList<>();
public static PApplet processing;
PointGraph map = new PointGraph();
public static void main(String[] args) {
PApplet.main("Processing");
}
@@ -18,23 +18,18 @@ public class Processing extends PApplet {
car = new Car(processing, 100,100,50,40);
size(1000, 1000);
car.addView(360,180);
for(int i = 0; i < 15; i++){
Wall wall = new Wall(processing, new Vector((int)random(50, 950), (int)random(50, 950)), new Vector((int)random(50, 950), (int)random(50, 950)));
objects.add(wall);
for(int i = 0; i < 10; i++){
PointVertex vStart = new PointVertex(random(50, 950), random(50, 950));
PointVertex vEnd = new PointVertex(random(50, 950), random(50, 950));
map.addEdge(vStart, vEnd);
}
}
public void draw(){
background(0);
// for(Wall object : objects){
// object.drawWall();
// }
car.drawCar(objects);
View view = car.views.get(0);
view.look(objects);
ArrayList<Vector> points = view.getPoints();
for(Vector point: points){
circle(point.x, point.y, 5);
}
map.draw(processing);
car.drawCar(map);
strokeWeight(2);
stroke(255);
//car.drive(new int[] {0, 0});

View File

@@ -1,3 +1,4 @@
import Graph.*;
import Vector.*;
import processing.core.PApplet;
@@ -26,10 +27,10 @@ public class Ray extends Line {
//checks to see at what coordinate the ray will collide with an object and sets the ray length to meet that point.
public void castRay(ArrayList<Wall> walls){
public void castRay(PointGraph map){
float shortestWallDistance = maxRayDistance;
int[] newColor = new int[]{255, 255, 255};
for(Wall wall : walls){
ArrayList<LineEdge> walls = map.getAllEdges();
for(LineEdge wall : walls){
// get the necessary vectors for two parameterized lines
// parameterized lines are of the form L = d*t + p
@@ -51,7 +52,6 @@ public class Ray extends Line {
float distance = d1.mul(t).add(p1).sub(this.position).mag();
if(distance < shortestWallDistance){
shortestWallDistance = distance;
newColor = new int[]{wall.r, wall.g, wall.b};
}
}
@@ -59,11 +59,9 @@ public class Ray extends Line {
// if we collided with a wall, set the ray's length to the distance from it to the collision
if(shortestWallDistance != maxRayDistance){
this.direction = this.direction.normalize().mul(shortestWallDistance);
this.color = newColor;
}
else{
this.direction = this.direction.normalize().mul(maxRayDistance);
this.color = new int[]{255, 255, 255};
}
}
@@ -92,8 +90,8 @@ public class Ray extends Line {
public void setRayLength(int rayLength){this.direction = this.direction.normalize().mul(rayLength);}
public void setAngle(float angle){
float distance = this.direction.mag();
this.direction = new Vector(cos(angle), sin(angle)).mul(distance);
float currentAngle = direction.angle();
this.direction = direction.rotate2D(angle - currentAngle);
}
}

View File

@@ -1,6 +1,8 @@
package Vector;
import static java.lang.Math.*;
import static processing.core.PApplet.cos;
import static processing.core.PApplet.sin;
public class Vector {
public float x = 0;
@@ -82,4 +84,10 @@ public class Vector {
public float angle(){
return (float) atan2(y, x);
}
public Vector rotate2D(float angle){
float distance = mag();
float currentAngle = this.angle();
return new Vector(cos(currentAngle + angle), sin(currentAngle + angle)).mul(distance);
}
}

View File

@@ -1,3 +1,4 @@
import Graph.PointGraph;
import Vector.Vector;
import processing.core.*;
import java.util.ArrayList;
@@ -30,9 +31,9 @@ public class View {
}
//sees if the ray will collide with the walls in the wall list
public void look(ArrayList<Wall> walls) {
public void look(PointGraph map) {
for (Ray ray : rays) {
ray.castRay(walls);
ray.castRay(map);
if(ray.hasCollided()){
ray.drawRay(proc);
}

View File

@@ -1,40 +0,0 @@
import Vector.*;
import processing.core.*;
import static processing.core.PApplet.*;
public class Wall extends Line {
int r;
int g;
int b;
private final PApplet proc;
Wall(PApplet proc, Vector pos, float angle, int wallLength){
super(pos, (new Vector(cos(angle), sin(angle)).mul(wallLength)).add(pos));
this.proc = proc;
r = (int)proc.random(50, 255);
g = (int)proc.random(50, 255);
b = (int)proc.random(50, 255);
}
Wall(PApplet proc, Vector startPos, Vector endPos){
super(startPos, endPos);
this.proc = proc;
r = (int)proc.random(50, 255);
g = (int)proc.random(50, 255);
b = (int)proc.random(50, 255);
}
void drawWall(){
proc.stroke(r,g,b);
proc.strokeWeight(10);
proc.circle(position.x, position.y, 10);
proc.strokeWeight(2);
proc.line(position.x, position.y, position.x + direction.x, position.y + direction.y);
}
Vector getPos(){
return position;
}
}