Refactored the ray class to have its interesections work more consistently.

This commit is contained in:
Quinn
2023-04-30 19:17:41 -05:00
parent 527615220f
commit 9b0acbe34c
6 changed files with 108 additions and 23 deletions

View File

@@ -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

View File

@@ -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);

View File

@@ -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<Wall> 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};
}
}

View File

@@ -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);
}
}

View File

@@ -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;
}