Refactored the ray class to have its interesections work more consistently.
This commit is contained in:
@@ -5,7 +5,9 @@ import java.util.List;
|
|||||||
import static processing.core.PApplet.*;
|
import static processing.core.PApplet.*;
|
||||||
|
|
||||||
public class Line{
|
public class Line{
|
||||||
|
// vector which represents the direction and length of the line from its starting position
|
||||||
protected Vector direction = new Vector(0,0);
|
protected Vector direction = new Vector(0,0);
|
||||||
|
// store the starting position of the line
|
||||||
protected Vector position = new Vector(0,0);
|
protected Vector position = new Vector(0,0);
|
||||||
|
|
||||||
Line(Vector startPosition, Vector endPosition){
|
Line(Vector startPosition, Vector endPosition){
|
||||||
@@ -80,6 +82,10 @@ public class Line{
|
|||||||
|
|
||||||
public float getAngle(){return atan2(this.direction.y, this.direction.x);}
|
public float getAngle(){return atan2(this.direction.y, this.direction.x);}
|
||||||
|
|
||||||
|
public Vector endPoint(){
|
||||||
|
return this.position.add(this.direction);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param point
|
* @param point
|
||||||
* @return the smallest distance from the point to this line
|
* @return the smallest distance from the point to this line
|
||||||
|
|||||||
@@ -16,9 +16,9 @@ public class Processing extends PApplet {
|
|||||||
processing = this;
|
processing = this;
|
||||||
car = new Car(processing, 100,100,50,40);
|
car = new Car(processing, 100,100,50,40);
|
||||||
size(1000, 1000);
|
size(1000, 1000);
|
||||||
car.addView(180,180);
|
car.addView(180,90);
|
||||||
for(int i = 0; i < 20; i++){
|
for(int i = 0; i < 15; i++){
|
||||||
Wall wall = new Wall(processing, new Vector((int)random(40, 1840), (int)random(40, 960)), (int)random(360), (int)random(100, 1000));
|
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);
|
objects.add(wall);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -35,16 +35,16 @@ public class Processing extends PApplet {
|
|||||||
|
|
||||||
public void keyPressed(){
|
public void keyPressed(){
|
||||||
if(key == 'd'){
|
if(key == 'd'){
|
||||||
car.setPose(car.getPose().add(1, 0));
|
car.setPose(car.getPose().add(10, 0));
|
||||||
}
|
}
|
||||||
if(key == 'w'){
|
if(key == 'w'){
|
||||||
car.setPose(car.getPose().add(0, -1));
|
car.setPose(car.getPose().add(0, -10));
|
||||||
}
|
}
|
||||||
if(key == 'a'){
|
if(key == 'a'){
|
||||||
car.setPose(car.getPose().add(-1, 0));
|
car.setPose(car.getPose().add(-10, 0));
|
||||||
}
|
}
|
||||||
if(key == 's'){
|
if(key == 's'){
|
||||||
car.setPose(car.getPose().add(0, 1));
|
car.setPose(car.getPose().add(0, 10));
|
||||||
}
|
}
|
||||||
if(key == 'q'){
|
if(key == 'q'){
|
||||||
car.setAngle(car.getAngle()+1);
|
car.setAngle(car.getAngle()+1);
|
||||||
|
|||||||
37
src/Ray.java
37
src/Ray.java
@@ -7,6 +7,7 @@ import static processing.core.PApplet.*;
|
|||||||
|
|
||||||
public class Ray extends Line{
|
public class Ray extends Line{
|
||||||
float maxRayDistance = 1000;
|
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)
|
//takes the starting position of the ray, the length of the ray, and it's casting angle (radians)
|
||||||
Ray(Vector startPosition, float angle){
|
Ray(Vector startPosition, float angle){
|
||||||
super(startPosition, startPosition.add(new Vector(cos(angle), sin(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){
|
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.
|
//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(ArrayList<Wall> walls){
|
||||||
float shortestWallDistance = maxRayDistance;
|
float shortestWallDistance = maxRayDistance;
|
||||||
|
int[] newColor = new int[]{255, 255, 255};
|
||||||
for(Wall wall : walls){
|
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
|
// get the necessary vectors for two parameterized lines
|
||||||
float x = (wallLine.y - rayLine.y) / (rayLine.x - wallLine.x);
|
// parameterized lines are of the form L = d*t + p
|
||||||
float y = rayLine.x * x + rayLine.y;
|
Vector d1 = this.direction.normalize().mul(maxRayDistance);
|
||||||
Vector intersect = new Vector(x, y);
|
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
|
// calculate the parameters for the intersection t and u
|
||||||
float distAlongLine = intersect.sub(wall.getPosition()).mag();
|
float t = -(d2.x*(p2.y-p1.y) + d2.y*(p1.x-p2.x))/(d1.x*d2.y - d2.x*d1.y);
|
||||||
float angleFromLine = wall.direction.angleDiff(intersect.sub(wall.position));
|
float u = -(d1.x*(p2.y-p1.y) + d1.y*(p1.x-p2.x))/(d1.x*d2.y-d2.x*d1.y);
|
||||||
if(distAlongLine > wall.getLength() && abs(angleFromLine) < 5*PI/180){
|
|
||||||
|
// 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;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// if the distance from the ray to the intersection is shorter than the shortestWallDistance, this is our new closest wall
|
// 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){
|
if(distance < shortestWallDistance){
|
||||||
shortestWallDistance = distance;
|
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 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};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
import static java.lang.Math.acos;
|
import static java.lang.Math.*;
|
||||||
import static java.lang.Math.sqrt;
|
|
||||||
|
|
||||||
public class Vector {
|
public class Vector {
|
||||||
public float x = 0;
|
public float x = 0;
|
||||||
@@ -67,8 +66,18 @@ public class Vector {
|
|||||||
return new Vector(x / mag, y / mag, z / mag);
|
return new Vector(x / mag, y / mag, z / mag);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param other
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
float angleDiff(Vector other){
|
float angleDiff(Vector other){
|
||||||
float dot = this.dot(other);
|
float dot = this.dot(other); // dot product
|
||||||
return (float)acos(dot / (this.mag() * other.mag()));
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,10 +17,22 @@ public class Wall extends Line{
|
|||||||
b = (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(){
|
void drawWall(){
|
||||||
proc.stroke(r,g,b);
|
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);
|
proc.line(position.x, position.y, position.x + direction.x, position.y + direction.y);
|
||||||
}
|
}
|
||||||
|
|
||||||
Vector getPos(){
|
Vector getPos(){
|
||||||
return position;
|
return position;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import processing.core.PApplet;
|
|||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
import static java.lang.Math.abs;
|
import static java.lang.Math.abs;
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
@@ -31,6 +32,34 @@ class LineTest{
|
|||||||
// make sure the line is starting in the correct place
|
// make sure the line is starting in the correct place
|
||||||
assertFloatEquals(v1.x, line.getPosition().x);
|
assertFloatEquals(v1.x, line.getPosition().x);
|
||||||
assertFloatEquals(v1.y, line.getPosition().y);
|
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
|
@Test
|
||||||
@@ -54,4 +83,20 @@ class LineTest{
|
|||||||
assertFloatEquals(v1.y, line.getPosition().y);
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user