Refactoring the ray class to extend a line class

This commit is contained in:
Quinn
2023-04-06 18:05:56 -05:00
parent aa012e660b
commit cd4ec2c819
3 changed files with 88 additions and 109 deletions

View File

@@ -5,8 +5,8 @@ import java.util.List;
import static processing.core.PApplet.*; import static processing.core.PApplet.*;
public class Line{ public class Line{
private Vector direction = new Vector(0,0); protected Vector direction = new Vector(0,0);
private Vector position = new Vector(0,0); protected Vector position = new Vector(0,0);
Line(Vector startPosition, Vector endPosition){ Line(Vector startPosition, Vector endPosition){
direction = endPosition.sub(startPosition); direction = endPosition.sub(startPosition);

56
src/Ray.java Normal file
View File

@@ -0,0 +1,56 @@
import processing.core.PApplet;
import processing.core.PVector;
import java.util.ArrayList;
import static processing.core.PApplet.*;
public class Ray extends Line{
//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))));
}
public void drawRay(PApplet proc){
proc.line(position.x, position.y, position.add(direction).x, position.add(direction).y);
}
//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 maxRange = 1000;
for(Wall wall : walls){
// TODO: find the intersection between the wall and ray.
}
}
public Vector getPos(){ return this.position;}
public float getRayLength(){return this.direction.mag();}
public float getAngle(){return atan2(this.direction.y, this.direction.x);}
public boolean hasCollided(){
return this.direction.mag() == 0;
}
//returns the absolute position of the point
public Vector getPoint(){
if(this.direction.mag() == 0){
return new Vector();
}
return this.position.add(this.direction);
}
public void setPos(Vector newPosition){
this.position = newPosition;
}
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);
}
}

View File

@@ -42,7 +42,9 @@ public class View{
//changes the position of the view //changes the position of the view
public void setPos(Vector newPose) { public void setPos(Vector newPose) {
pose = newPose; pose = newPose;
for(Ray ray : rays){ray.setPos(pose);} for (Ray ray : rays) {
ray.setPos(pose);
}
} }
//changes the angle of the view //changes the angle of the view
@@ -57,13 +59,21 @@ public class View{
this.setRayNum(this.rays.size(), this.FOV, this.angle); this.setRayNum(this.rays.size(), this.FOV, this.angle);
} }
public Vector getPos(){return pose;} public Vector getPos() {
return pose;
}
public float getAngle(){return this.angle;} public float getAngle() {
return this.angle;
}
public float getFOV(){return this.FOV;} public float getFOV() {
return this.FOV;
}
public int getRayNum(){return this.rays.size();} public int getRayNum() {
return this.rays.size();
}
//gets the point that each ray has collided with //gets the point that each ray has collided with
public ArrayList<Vector> getPoints() { public ArrayList<Vector> getPoints() {
@@ -79,90 +89,3 @@ public class View{
} }
} }
class Ray{
Vector pose;
int rayLength;
int defaultRayLength;
float angle; // IN RADIANS
private static PApplet proc;
//takes the starting position of the ray, the length of the ray, and it's casting angle (radians)
Ray(PApplet processing, Vector position, int defaultRayLength, float angle){
proc = processing;
this.pose = position;
this.defaultRayLength = defaultRayLength;
this.rayLength = defaultRayLength;
this.angle = angle;
}
public void drawRay(){
proc.line(pose.x, pose.y, (pose.x + cos(angle)*rayLength), (pose.y + sin(angle)*rayLength));
}
//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> objects){
this.rayLength = defaultRayLength;
ArrayList<Integer> distances = new ArrayList<>();
//sees what objects it collides with
for(Wall object : objects){
float theta1 = angle;
float theta2 = radians(object.getAngle());
Vector wallPos = object.getPos();
//finds where along the wall the ray collides
float b = (pose.x*sin(theta1) + wallPos.y*cos(theta1) - pose.y*cos(theta1) - wallPos.x*sin(theta1)) / (cos(theta2)*sin(theta1) - sin(theta2)*cos(theta1));
//if the place along the wall is further away than the wall extends, then it didn't collide
if(b < object.getLength() && b > 0){
//finds the length of the ray needed to collide with the wall
float a = (b*sin(theta2) + wallPos.y-pose.y) / sin(theta1);
//add that length to a list
if(a > 0){
distances.add((int)abs(a));
}
}
}
//finds the shortest distance and sets the length of the ray to that distance
if(distances.size() > 0){
for(Integer distance : distances){
if(distance < rayLength){
rayLength = distance;
}
}
}
else this.rayLength = defaultRayLength;
}
public Vector getPos(){ return pose;}
public int getRayLength(){return this.rayLength;}
public float getAngle(){return this.angle;}
public boolean hasCollided(){
return this.defaultRayLength != this.rayLength;
}
//returns the absolute position of the point
public Vector getPoint(){
if(this.rayLength != this.defaultRayLength){
return new Vector(rayLength * (int)cos(this.angle) + pose.x, rayLength * (int)sin(this.angle) + pose.y);
}
else{
return new Vector(0,0);
}
}
public void setPos(Vector newPose){
pose = newPose;
}
public void setRayLength(int rayLength){this.rayLength = rayLength;}
public void setDefaultRayLength(int defaultRayLength){this.defaultRayLength = defaultRayLength;}
public void setAngle(float angle){this.angle = angle;}
}