From 9b0acbe34cab26615abf7aa6cff67030b75497f9 Mon Sep 17 00:00:00 2001 From: Quinn Date: Sun, 30 Apr 2023 19:17:41 -0500 Subject: [PATCH] Refactored the ray class to have its interesections work more consistently. --- src/Line.java | 6 ++++++ src/Processing.java | 14 +++++++------- src/Ray.java | 37 +++++++++++++++++++++++++------------ src/Vector.java | 17 +++++++++++++---- src/Wall.java | 12 ++++++++++++ tests/LineTest.java | 45 +++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 108 insertions(+), 23 deletions(-) diff --git a/src/Line.java b/src/Line.java index 4ee9dbe..3068e95 100644 --- a/src/Line.java +++ b/src/Line.java @@ -5,7 +5,9 @@ import java.util.List; import static processing.core.PApplet.*; public class Line{ + // vector which represents the direction and length of the line from its starting position protected Vector direction = new Vector(0,0); + // store the starting position of the line protected Vector position = new Vector(0,0); Line(Vector startPosition, Vector endPosition){ @@ -80,6 +82,10 @@ public class Line{ public float getAngle(){return atan2(this.direction.y, this.direction.x);} + public Vector endPoint(){ + return this.position.add(this.direction); + } + /** * @param point * @return the smallest distance from the point to this line diff --git a/src/Processing.java b/src/Processing.java index d742132..19a4be4 100644 --- a/src/Processing.java +++ b/src/Processing.java @@ -16,9 +16,9 @@ public class Processing extends PApplet { processing = this; car = new Car(processing, 100,100,50,40); size(1000, 1000); - car.addView(180,180); - for(int i = 0; i < 20; i++){ - Wall wall = new Wall(processing, new Vector((int)random(40, 1840), (int)random(40, 960)), (int)random(360), (int)random(100, 1000)); + car.addView(180,90); + 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); } } @@ -35,16 +35,16 @@ public class Processing extends PApplet { public void keyPressed(){ if(key == 'd'){ - car.setPose(car.getPose().add(1, 0)); + car.setPose(car.getPose().add(10, 0)); } if(key == 'w'){ - car.setPose(car.getPose().add(0, -1)); + car.setPose(car.getPose().add(0, -10)); } if(key == 'a'){ - car.setPose(car.getPose().add(-1, 0)); + car.setPose(car.getPose().add(-10, 0)); } if(key == 's'){ - car.setPose(car.getPose().add(0, 1)); + car.setPose(car.getPose().add(0, 10)); } if(key == 'q'){ car.setAngle(car.getAngle()+1); diff --git a/src/Ray.java b/src/Ray.java index 0864446..3eb0b35 100644 --- a/src/Ray.java +++ b/src/Ray.java @@ -7,6 +7,7 @@ import static processing.core.PApplet.*; public class Ray extends Line{ float maxRayDistance = 1000; + int[] color = new int[]{255, 255, 255}; //takes the starting position of the ray, the length of the ray, and it's casting angle (radians) Ray(Vector startPosition, float angle){ super(startPosition, startPosition.add(new Vector(cos(angle), sin(angle)))); @@ -14,32 +15,42 @@ public class Ray extends Line{ } public void drawRay(PApplet proc){ - proc.line(position.x, position.y, position.add(direction).x, position.add(direction).y); + proc.stroke(color[0], color[1], color[2]); + proc.line(position.x, position.y, position.x + direction.x, position.y + direction.y); +// proc.noFill(); +// proc.circle(position.x, position.y, 2*direction.mag()); +// proc.fill(255); } + + //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 walls){ float shortestWallDistance = maxRayDistance; + int[] newColor = new int[]{255, 255, 255}; for(Wall wall : walls){ - Vector rayLine = this.getSlopeIntForm(); // stored as x:m, y:b - Vector wallLine = wall.getSlopeIntForm(); // m, b - // find the point where they intersect - float x = (wallLine.y - rayLine.y) / (rayLine.x - wallLine.x); - float y = rayLine.x * x + rayLine.y; - Vector intersect = new Vector(x, y); + // get the necessary vectors for two parameterized lines + // parameterized lines are of the form L = d*t + p + Vector d1 = this.direction.normalize().mul(maxRayDistance); + Vector d2 = wall.direction; + Vector p1 = this.position; + Vector p2 = wall.position; - // if the intersect doesn't actually lie on the wallLine, go to the next wall - float distAlongLine = intersect.sub(wall.getPosition()).mag(); - float angleFromLine = wall.direction.angleDiff(intersect.sub(wall.position)); - if(distAlongLine > wall.getLength() && abs(angleFromLine) < 5*PI/180){ + // calculate the parameters for the intersection t and u + float t = -(d2.x*(p2.y-p1.y) + d2.y*(p1.x-p2.x))/(d1.x*d2.y - d2.x*d1.y); + float u = -(d1.x*(p2.y-p1.y) + d1.y*(p1.x-p2.x))/(d1.x*d2.y-d2.x*d1.y); + + // the lines will only be intersecting when both t and u are between 0 and 1. + if(!(0 <= t && t <= 1 && 0 <= u && u <= 1)){ continue; } // if the distance from the ray to the intersection is shorter than the shortestWallDistance, this is our new closest wall - float distance = this.position.sub(intersect).mag(); + 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}; } } @@ -47,9 +58,11 @@ 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}; } } diff --git a/src/Vector.java b/src/Vector.java index b1cc038..0d859d7 100644 --- a/src/Vector.java +++ b/src/Vector.java @@ -1,5 +1,4 @@ -import static java.lang.Math.acos; -import static java.lang.Math.sqrt; +import static java.lang.Math.*; public class Vector { public float x = 0; @@ -67,8 +66,18 @@ public class Vector { return new Vector(x / mag, y / mag, z / mag); } + /** + * @param other + * @return + */ float angleDiff(Vector other){ - float dot = this.dot(other); - return (float)acos(dot / (this.mag() * other.mag())); + float dot = this.dot(other); // dot product + float det = this.x*other.y - this.y*other.x; // determinant + float angle = (float) atan2(det, dot); // atan2(y, x) or atan2(sin, cos) + return angle; + } + + float angle(){ + return (float) atan2(y, x); } } diff --git a/src/Wall.java b/src/Wall.java index 93c2f88..065e970 100644 --- a/src/Wall.java +++ b/src/Wall.java @@ -17,10 +17,22 @@ public class Wall extends Line{ 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; } diff --git a/tests/LineTest.java b/tests/LineTest.java index 089ed66..4101fb2 100644 --- a/tests/LineTest.java +++ b/tests/LineTest.java @@ -3,6 +3,7 @@ import processing.core.PApplet; import java.util.ArrayList; import java.util.List; +import java.util.Random; import static java.lang.Math.abs; import static org.junit.jupiter.api.Assertions.*; @@ -31,6 +32,34 @@ class LineTest{ // make sure the line is starting in the correct place assertFloatEquals(v1.x, line.getPosition().x); assertFloatEquals(v1.y, line.getPosition().y); + + // make sure the line ends in the correct place + assertFloatEquals(v2.x, line.endPoint().x); + assertFloatEquals(v2.y, line.endPoint().y); + + Random rand = new Random(); + // repeat this test with 100 random lines + for(int i = 0; i < 100; i++){ + v1 = new Vector(rand.nextInt(1001), rand.nextInt(1001)); + v2 = new Vector(rand.nextInt(1001), rand.nextInt(1001)); + line = new Line(v1, v2); + + // make sure the line is pointing in the right direction + lineDirection = v2.sub(v1); + float angleDiff = lineDirection.angleDiff(line.getDirection()); + assertFloatEquals(0, angleDiff, 0.001f); + + // make sure the line is the correct length + assertFloatEquals(lineDirection.mag(), line.getLength()); + + // make sure the line is starting in the correct place + assertFloatEquals(v1.x, line.getPosition().x); + assertFloatEquals(v1.y, line.getPosition().y); + + // make sure the line ends in the correct place + assertFloatEquals(v2.x, line.endPoint().x); + assertFloatEquals(v2.y, line.endPoint().y); + } } @Test @@ -54,4 +83,20 @@ class LineTest{ assertFloatEquals(v1.y, line.getPosition().y); } + @Test + public void testAngle(){ + Line ray = new Line(new Vector(680, 560), new Vector(680+116, 560+1)); + Line wall = new Line(new Vector(675, 587), new Vector(675-114, 587-29)); + + + float angle1 = wall.position.sub(ray.position).angle(); + float angle2 = wall.endPoint().sub(ray.position).angle(); + float rayAngle = ray.direction.angle(); + + System.out.println("Angle 1: " + String.valueOf(angle1) + ", Angle 2: " + String.valueOf(angle2) + ", Ray Angle: " + String.valueOf(rayAngle)); + assertFloatEquals(1.753907144057f, angle1, 0.001f); + assertFloatEquals(3.1583977941048f, angle2, 0.001f); + assertFloatEquals(0.0086204761121f, rayAngle, 0.001f); + } + } \ No newline at end of file