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.*;
public class Line{
private Vector direction = new Vector(0,0);
private Vector position = new Vector(0,0);
protected Vector direction = new Vector(0,0);
protected Vector position = new Vector(0,0);
Line(Vector startPosition, Vector endPosition){
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

@@ -4,7 +4,7 @@ import java.util.Objects;
import static processing.core.PApplet.*;
public class View{
public class View {
Vector pose;
float angle = 0;
float FOV;
@@ -12,7 +12,7 @@ public class View{
private static PApplet proc;
//the x,y position of the view, what angle it's looking at and its FOV
View(PApplet processing, Vector newPose, int numberOfRays, float FOV){
View(PApplet processing, Vector newPose, int numberOfRays, float FOV) {
proc = processing;
this.pose = newPose;
this.FOV = FOV;
@@ -20,11 +20,11 @@ public class View{
}
//sets the number of rays and their starting values in the ray list
public void setRayNum(int numberOfRays, float FOV, float angleOffset){
float rayStep = FOV/numberOfRays;
public void setRayNum(int numberOfRays, float FOV, float angleOffset) {
float rayStep = FOV / numberOfRays;
rays.clear();
float angle = (float)(0.01-angleOffset); //the 0.01 fixes some bugs
for(int i = 0; i < numberOfRays; i++){
float angle = (float) (0.01 - angleOffset); //the 0.01 fixes some bugs
for (int i = 0; i < numberOfRays; i++) {
Ray ray = new Ray(proc, pose, 100000, angle);
angle = angle + rayStep;
rays.add(ray);
@@ -32,46 +32,56 @@ public class View{
}
//sees if the ray will collide with the walls in the wall list
public void look(ArrayList<Wall> walls){
for (Ray ray : rays){
public void look(ArrayList<Wall> walls) {
for (Ray ray : rays) {
ray.castRay(walls);
ray.drawRay();
}
}
//changes the position of the view
public void setPos(Vector newPose){
public void setPos(Vector newPose) {
pose = newPose;
for(Ray ray : rays){ray.setPos(pose);}
for (Ray ray : rays) {
ray.setPos(pose);
}
}
//changes the angle of the view
public void setAngle(float angle){
public void setAngle(float angle) {
this.angle = angle;
this.setRayNum(rays.size(), this.FOV, angle);
}
//changes the field of view of the view
public void setFOV(float FOV){
public void setFOV(float FOV) {
this.FOV = FOV;
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
public ArrayList<Vector> getPoints(){
public ArrayList<Vector> getPoints() {
ArrayList<Vector> points = new ArrayList<>();
for(Ray ray : rays){
if(!Objects.equals(ray.getPoint(), new Vector(0, 0) {
})){
for (Ray ray : rays) {
if (!Objects.equals(ray.getPoint(), new Vector(0, 0) {
})) {
points.add(ray.getPoint());
}
}
@@ -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;}
}