Everything can now be drawn
This commit is contained in:
@@ -4,6 +4,7 @@ import static java.lang.Math.PI;
|
|||||||
import static processing.core.PApplet.degrees;
|
import static processing.core.PApplet.degrees;
|
||||||
import static processing.core.PApplet.radians;
|
import static processing.core.PApplet.radians;
|
||||||
|
|
||||||
|
import Graph.*;
|
||||||
import Vector.Vector;
|
import Vector.Vector;
|
||||||
import processing.core.PApplet;
|
import processing.core.PApplet;
|
||||||
|
|
||||||
@@ -37,17 +38,17 @@ public class Car{
|
|||||||
}
|
}
|
||||||
|
|
||||||
//draw the car and its views
|
//draw the car and its views
|
||||||
public void drawCar(ArrayList<Wall> walls){
|
public void drawCar(PointGraph g){
|
||||||
proc.stroke(255);
|
proc.stroke(255);
|
||||||
proc.ellipse(pose.x, pose.y, carWidth, carLength);
|
proc.ellipse(pose.x, pose.y, carWidth, carLength);
|
||||||
this.updateScan(walls);
|
this.updateScan(g);
|
||||||
// this.slam.drawLines();
|
// this.slam.drawLines();
|
||||||
}
|
}
|
||||||
|
|
||||||
//With all 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<Wall> walls){
|
void updateScan(PointGraph map){
|
||||||
for(View view : views){
|
for(View view : views){
|
||||||
view.look(walls);
|
view.look(map);
|
||||||
}
|
}
|
||||||
|
|
||||||
for(View view : views){
|
for(View view : views){
|
||||||
|
|||||||
@@ -5,6 +5,8 @@ import processing.core.PApplet;
|
|||||||
|
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
|
|
||||||
|
import static java.lang.Math.PI;
|
||||||
|
|
||||||
public class LineEdge extends Edge implements LineInterface{
|
public class LineEdge extends Edge implements LineInterface{
|
||||||
protected PointVertex vStart;
|
protected PointVertex vStart;
|
||||||
protected PointVertex vEnd;
|
protected PointVertex vEnd;
|
||||||
@@ -41,5 +43,12 @@ public class LineEdge extends Edge implements LineInterface{
|
|||||||
|
|
||||||
public void draw(PApplet proc){
|
public void draw(PApplet proc){
|
||||||
line.draw(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);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ import java.util.ArrayList;
|
|||||||
|
|
||||||
public class PointGraph extends Graph {
|
public class PointGraph extends Graph {
|
||||||
PointVertex selectedVertex;
|
PointVertex selectedVertex;
|
||||||
PointGraph(){
|
public PointGraph(){
|
||||||
super();
|
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
|
* @param v set this vertex as the selected vertex in the graph
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ public class PointVertex extends Vertex {
|
|||||||
* @param xPos the x position of the vertex
|
* @param xPos the x position of the vertex
|
||||||
* @param yPos the y posiiton of the vertex
|
* @param yPos the y posiiton of the vertex
|
||||||
*/
|
*/
|
||||||
PointVertex(float xPos, float yPos){
|
public PointVertex(float xPos, float yPos){
|
||||||
super();
|
super();
|
||||||
this.position = new Vector(xPos, yPos);
|
this.position = new Vector(xPos, yPos);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,14 +1,14 @@
|
|||||||
import Vector.Vector;
|
import Graph.*;
|
||||||
import processing.core.PApplet;
|
import processing.core.PApplet;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
|
|
||||||
public class Processing extends PApplet {
|
public class Processing extends PApplet {
|
||||||
|
|
||||||
Car car;
|
Car car;
|
||||||
ArrayList<Wall> objects = new ArrayList<>();
|
|
||||||
public static PApplet processing;
|
public static PApplet processing;
|
||||||
|
|
||||||
|
PointGraph map = new PointGraph();
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
PApplet.main("Processing");
|
PApplet.main("Processing");
|
||||||
}
|
}
|
||||||
@@ -18,23 +18,18 @@ public class Processing extends PApplet {
|
|||||||
car = new Car(processing, 100,100,50,40);
|
car = new Car(processing, 100,100,50,40);
|
||||||
size(1000, 1000);
|
size(1000, 1000);
|
||||||
car.addView(360,180);
|
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)));
|
for(int i = 0; i < 10; i++){
|
||||||
objects.add(wall);
|
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(){
|
public void draw(){
|
||||||
background(0);
|
background(0);
|
||||||
// for(Wall object : objects){
|
map.draw(processing);
|
||||||
// object.drawWall();
|
car.drawCar(map);
|
||||||
// }
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
strokeWeight(2);
|
strokeWeight(2);
|
||||||
stroke(255);
|
stroke(255);
|
||||||
//car.drive(new int[] {0, 0});
|
//car.drive(new int[] {0, 0});
|
||||||
|
|||||||
14
src/Ray.java
14
src/Ray.java
@@ -1,3 +1,4 @@
|
|||||||
|
import Graph.*;
|
||||||
import Vector.*;
|
import Vector.*;
|
||||||
|
|
||||||
import processing.core.PApplet;
|
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.
|
//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;
|
float shortestWallDistance = maxRayDistance;
|
||||||
int[] newColor = new int[]{255, 255, 255};
|
ArrayList<LineEdge> walls = map.getAllEdges();
|
||||||
for(Wall wall : walls){
|
for(LineEdge wall : walls){
|
||||||
|
|
||||||
// get the necessary vectors for two parameterized lines
|
// get the necessary vectors for two parameterized lines
|
||||||
// parameterized lines are of the form L = d*t + p
|
// 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();
|
float distance = d1.mul(t).add(p1).sub(this.position).mag();
|
||||||
if(distance < shortestWallDistance){
|
if(distance < shortestWallDistance){
|
||||||
shortestWallDistance = distance;
|
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 we collided with a wall, set the ray's length to the distance from it to the collision
|
||||||
if(shortestWallDistance != maxRayDistance){
|
if(shortestWallDistance != maxRayDistance){
|
||||||
this.direction = this.direction.normalize().mul(shortestWallDistance);
|
this.direction = this.direction.normalize().mul(shortestWallDistance);
|
||||||
this.color = newColor;
|
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
this.direction = this.direction.normalize().mul(maxRayDistance);
|
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 setRayLength(int rayLength){this.direction = this.direction.normalize().mul(rayLength);}
|
||||||
|
|
||||||
public void setAngle(float angle){
|
public void setAngle(float angle){
|
||||||
float distance = this.direction.mag();
|
float currentAngle = direction.angle();
|
||||||
this.direction = new Vector(cos(angle), sin(angle)).mul(distance);
|
this.direction = direction.rotate2D(angle - currentAngle);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,8 @@
|
|||||||
package Vector;
|
package Vector;
|
||||||
|
|
||||||
import static java.lang.Math.*;
|
import static java.lang.Math.*;
|
||||||
|
import static processing.core.PApplet.cos;
|
||||||
|
import static processing.core.PApplet.sin;
|
||||||
|
|
||||||
public class Vector {
|
public class Vector {
|
||||||
public float x = 0;
|
public float x = 0;
|
||||||
@@ -82,4 +84,10 @@ public class Vector {
|
|||||||
public float angle(){
|
public float angle(){
|
||||||
return (float) atan2(y, x);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import Graph.PointGraph;
|
||||||
import Vector.Vector;
|
import Vector.Vector;
|
||||||
import processing.core.*;
|
import processing.core.*;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@@ -30,9 +31,9 @@ public class View {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//sees if the ray will collide with the walls in the wall list
|
//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) {
|
for (Ray ray : rays) {
|
||||||
ray.castRay(walls);
|
ray.castRay(map);
|
||||||
if(ray.hasCollided()){
|
if(ray.hasCollided()){
|
||||||
ray.drawRay(proc);
|
ray.drawRay(proc);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user