Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ray beam refactor #51

Merged
merged 3 commits into from
Feb 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 0 additions & 45 deletions src/main/java/app/controller/Beam.java

This file was deleted.

32 changes: 17 additions & 15 deletions src/main/java/app/controller/GameEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import app.controller.graphicsEngine.GraphicsEngine;
import app.controller.graphicsEngine.RayTracing;
import app.controller.linAlg.Vector;
import app.model.agents.Agent;
import app.model.Map;
import app.view.Renderer;
Expand All @@ -22,25 +23,11 @@ public GameEngine(Map map, Renderer renderer)
this.map = map;
this.renderer = renderer;
this.graphicsEngine = new RayTracing();
Timeline timeline = new Timeline(new KeyFrame( Duration.millis(100), ae -> tick()));
Timeline timeline = new Timeline(new KeyFrame( Duration.millis(10), ae -> tick()));
timeline.setCycleCount(Animation.INDEFINITE);
timeline.play();
}

public void handleKey(KeyEvent e)
{
System.out.println(e.getCharacter());
switch (e.getCharacter())
{
case "w" -> map.moveHuman(new Vector(0,-10));
case "s" -> map.moveHuman(new Vector(0,10));
case "a" -> map.moveHuman(new Vector(-10,0));
case "d" -> map.moveHuman(new Vector(10,0));

}

}

public void tick()
{
for(Agent a: map.getAgents())
Expand All @@ -50,4 +37,19 @@ public void tick()
}
renderer.render();
}

public void handleKey(KeyEvent e)
{
switch (e.getCharacter())
{
case "W" -> map.run(new Vector(0, -1));
case "S" -> map.run(new Vector(0, 1));
case "A" -> map.run(new Vector(-1, 0));
case "D" -> map.run(new Vector(1, 0));
case "w" -> map.walk(new Vector(0, -1));
case "s" -> map.walk(new Vector(0, 1));
case "a" -> map.walk(new Vector(-1, 0));
case "d" -> map.walk(new Vector(1, 0));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package app.controller.graphicsEngine;

import app.controller.Beam;
import app.model.agents.Agent;
import app.model.Map;

import java.util.ArrayList;

public interface GraphicsEngine
{
ArrayList<Beam> compute(Map map, Agent agent);
ArrayList<Ray> compute(Map map, Agent agent);
}
37 changes: 24 additions & 13 deletions src/main/java/app/controller/graphicsEngine/Ray.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,44 @@
package app.controller.graphicsEngine;

import app.controller.Vector;
import app.controller.linAlg.Vector;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;

public class Ray
{
public Vector origin;
public Vector direction;
public Vector u;
public Vector v;
private Color colour = Color.RED;
private final double LINE_WIDTH = 1;

public Ray(Vector origin, Vector direction)
public Ray(Vector u, Vector direction)
{
this.origin = origin;
this.direction = direction;
this.u = u;
this.v = direction;
}

public Vector getDirection()
public Vector getV()
{
return direction;
return v;
}

public Vector getOrigin()
public Vector getU()
{
return origin;
return u;
}

public Ray rotate(double degrees)
{
Vector unitVector = direction.sub(origin);
Vector unitVector = v.sub(u);
Vector rotatedVector = unitVector.rotate(degrees);
Vector newVector = origin.add(rotatedVector);
return new Ray(this.origin, newVector);
Vector newVector = u.add(rotatedVector);
return new Ray(this.u, newVector);
}

public void draw(GraphicsContext gc)
{
gc.setStroke(colour);
gc.setLineWidth(LINE_WIDTH);
gc.strokeLine(getU().getX(), getU().getY(), getV().getX(), getV().getY());
}
}
14 changes: 6 additions & 8 deletions src/main/java/app/controller/graphicsEngine/RayTracing.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
package app.controller.graphicsEngine;

import app.controller.Beam;
import app.controller.Vector;
import app.controller.linAlg.Vector;
import app.model.agents.Agent;
import app.model.Map;
import app.model.Placeable;

import app.model.objects.Placeable;
import java.util.ArrayList;

public class RayTracing implements GraphicsEngine
{
private int noOfRays = 360;
private double angle = 1;

public ArrayList<Beam> compute(Map map, Agent agent)
public ArrayList<Ray> compute(Map map, Agent agent)
{
ArrayList<Beam> beams = new ArrayList<>();
ArrayList<Ray> output = new ArrayList<>();
ArrayList<Ray> rays = scatterRays(agent);
Vector origin = agent.getPosition();

Expand All @@ -37,9 +35,9 @@ public ArrayList<Beam> compute(Map map, Agent agent)
}
}
if(intersection != null)
beams.add(new Beam(origin, intersection));
output.add(new Ray(origin, intersection));
}
return beams;
return output;
}

private ArrayList<Ray> scatterRays(Agent agent)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package app.controller;
package app.controller.linAlg;

public class RotationMatrix
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package app.controller;
package app.controller.linAlg;

public class Vector
{
Expand Down
13 changes: 10 additions & 3 deletions src/main/java/app/model/Map.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package app.model;

import app.controller.Vector;
import app.controller.linAlg.Vector;
import app.model.agents.Agent;
import app.model.agents.Human;
import app.model.objects.Placeable;
import app.model.objects.Wall;

import java.util.ArrayList;

Expand All @@ -24,9 +26,14 @@ public Map()
//agents.add(new AgentImp(new Vector(100, 100), new Vector(1,0), 10));
}

public void moveHuman(Vector v)
public void walk(Vector v)
{
human.setNextMove(v);
human.walk(v);
}

public void run(Vector v)
{
human.run(v);
}

private ArrayList<Placeable> createObjects()
Expand Down
11 changes: 5 additions & 6 deletions src/main/java/app/model/agents/Agent.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package app.model.agents;

import app.controller.Beam;
import app.controller.Vector;
import app.model.Placeable;

import app.controller.linAlg.Vector;
import app.controller.graphicsEngine.Ray;
import app.model.objects.Placeable;
import java.util.ArrayList;

public interface Agent extends Placeable
Expand All @@ -14,9 +13,9 @@ public interface Agent extends Placeable

Vector getDirection();

ArrayList<Beam> getView();
ArrayList<Ray> getView();

void updateView(ArrayList<Beam> view);
void updateView(ArrayList<Ray> view);

double getHearing();
}
14 changes: 6 additions & 8 deletions src/main/java/app/model/agents/AgentImp.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
package app.model.agents;

import app.controller.Beam;
import app.controller.Vector;
import app.controller.linAlg.Vector;
import app.controller.graphicsEngine.Ray;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;

import java.util.ArrayList;

public class AgentImp implements Agent
{
protected final double MAX_WALK = 10;
protected final double MAX_RUN = 20;
protected final double MAX_WALK = 5;
protected final double MAX_RUN = 10;

protected Vector position;
protected Vector direction;
protected double radius;
protected ArrayList<Beam> view;
protected ArrayList<Ray> view;

public AgentImp(Vector position, Vector direction, double radius)
{
Expand Down Expand Up @@ -46,13 +44,13 @@ public Vector getDirection()
}

@Override
public ArrayList<Beam> getView()
public ArrayList<Ray> getView()
{
return view;
}

@Override
public void updateView(ArrayList<Beam> view)
public void updateView(ArrayList<Ray> view)
{
this.view = view;
}
Expand Down
11 changes: 8 additions & 3 deletions src/main/java/app/model/agents/Human.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package app.model.agents;

import app.controller.Vector;
import app.controller.linAlg.Vector;

public class Human extends AgentImp
{
Expand All @@ -19,8 +19,13 @@ public void move()
nextMove = new Vector();
}

public void setNextMove(Vector nextMove)
public void walk(Vector nextMove)
{
this.nextMove = nextMove;
this.nextMove = nextMove.scale(MAX_WALK);
}

public void run(Vector nextMove)
{
this.nextMove = nextMove.scale(MAX_RUN);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package app.model;
package app.model.objects;

import app.controller.graphicsEngine.Ray;
import app.controller.Vector;
import app.controller.linAlg.Vector;
import javafx.scene.canvas.GraphicsContext;

public class Boundary implements Placeable
Expand All @@ -28,10 +28,10 @@ public boolean isHit(Ray ray)
double y_1 = a.getY();
double x_2 = b.getX();
double y_2 = b.getY();
double x_3 = ray.getOrigin().getX();
double y_3 = ray.getOrigin().getY();
double x_4 = ray.getDirection().getX();
double y_4 = ray.getDirection().getY();
double x_3 = ray.getU().getX();
double y_3 = ray.getU().getY();
double x_4 = ray.getV().getX();
double y_4 = ray.getV().getY();

// Source: https://en.wikipedia.org/wiki/Line%E2%80%93line_intersection
double denominator = (x_1 - x_2)*(y_3 - y_4) - (y_1 - y_2)*(x_3 - x_4);
Expand All @@ -52,10 +52,10 @@ public Vector intersection(Ray ray)
double y_1 = a.getY();
double x_2 = b.getX();
double y_2 = b.getY();
double x_3 = ray.getOrigin().getX();
double y_3 = ray.getOrigin().getY();
double x_4 = ray.getDirection().getX();
double y_4 = ray.getDirection().getY();
double x_3 = ray.getU().getX();
double y_3 = ray.getU().getY();
double x_4 = ray.getV().getX();
double y_4 = ray.getV().getY();

double denominator = (x_1 - x_2)*(y_3 - y_4) - (y_1 - y_2)*(x_3 - x_4);
double t = ((x_1 - x_3)*(y_3 - y_4) - (y_1 - y_3)*(x_3 - x_4)) / denominator;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package app.model;
package app.model.objects;

import app.controller.graphicsEngine.Ray;
import app.controller.Vector;
import app.controller.linAlg.Vector;
import javafx.scene.canvas.GraphicsContext;

public interface Placeable
Expand Down
Loading