Refactored the ray class to have its interesections work more consistently.
This commit is contained in:
37
src/Ray.java
37
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<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};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user