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

Collision detection #91

Merged
merged 16 commits into from
Feb 14, 2022
Merged
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# ignored files
/shelf/
/.idea/

/build/
# Project exclude paths
/.gradle/
24 changes: 24 additions & 0 deletions bin/main/map_1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name = map_1
gameFile = //
gameMode = 44
height = 95
width = 144
scaling = 0.2
numGuards = 7
numIntruders = 4
baseSpeedIntruder = 13.0
sprintSpeedIntruder = 21.0
baseSpeedGuard = 13.0
sprintSpeedGuard = 21.0
timeStep = 0.5
targetArea = 20 40 25 45
spawnAreaIntruders = 2 2 20 10
spawnAreaGuards = 2 2 20 10
wall = 10 10 110 110
wall = 200 200 220 220
wall = 400 100 440 420
tower = 0 23 50 63
tower = 12 40 20 44
teleport = 20 70 25 75 90 50 0.0
shaded = 10 20 20 40
texture = 10 20 20 40 0 0
26 changes: 25 additions & 1 deletion src/main/java/app/controller/GameEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import app.controller.graphicsEngine.RayTracing;
import app.controller.linAlg.Vector;
import app.model.agents.Agent;
import app.model.boundary.Boundary;
import app.model.furniture.Furniture;
import app.model.map.Map;
import app.view.Renderer;
import javafx.animation.Animation;
Expand All @@ -14,6 +16,8 @@

import java.time.Instant;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Arrays;

public class GameEngine
{
Expand All @@ -33,9 +37,18 @@ public GameEngine(Map map, Renderer renderer)

public void tick()
{
map.getAgents().forEach(a -> a.move());
for (Agent a :map.getAgents())
{
Vector startPoint = a.getPosition();
Vector endPoint = a.move();

if (legalMove(startPoint, endPoint))
a.updateLocation(endPoint);
}

map.getAgents().stream()
.forEach(a -> a.updateView(graphicsEngine.compute(map, a)));

renderer.render();
}

Expand All @@ -53,4 +66,15 @@ public void handleKey(KeyEvent e)
case "d" -> map.walk(new Vector(1, 0));
}
}


private boolean legalMove(Vector start, Vector end)
{
for (Boundary bdy : map.getBoundaries())
{
if(bdy.validMove(start, end))
return false;
}
return true;
}
}
23 changes: 23 additions & 0 deletions src/main/java/app/controller/linAlg/Intersection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package app.controller.linAlg;

public abstract class Intersection
{
public static boolean findIntersection(Vector p_1, Vector p_2, Vector p_3, Vector p_4)
{
double x_1 = p_1.getX();
double y_1 = p_1.getY();
double x_2 = p_2.getX();
double y_2 = p_2.getY();
double x_3 = p_3.getX();
double y_3 = p_3.getY();
double x_4 = p_4.getX();
double y_4 = p_4.getY();

double dom = (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))/ dom;
double u = ((x_1 - x_3)*(y_1 - y_2) - (y_1 - y_3)*(x_1 - x_2))/dom;

return (t <= 1 && t >= 0 && u <= 1 && u >= 0);
}

}
4 changes: 3 additions & 1 deletion src/main/java/app/model/agents/Agent.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@

public interface Agent extends Boundary
{
void move();
void updateLocation(Vector endPoint);

Vector move();

Vector getPosition();

Expand Down
15 changes: 13 additions & 2 deletions src/main/java/app/model/agents/AgentImp.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@ public AgentImp(Vector position, Vector direction, double radius)
}

@Override
public void move()
public void updateLocation(Vector endPoint)
{
position = endPoint;
}

@Override
public Vector move()
{
double x = Math.random() * MAX_WALK;
if(Math.random() > 0.5)
Expand All @@ -34,7 +40,7 @@ public void move()
if(Math.random() > 0.5)
y = y * -1;

position = position.add(new Vector(x, y));
return new Vector(x, y);
}

@Override
Expand Down Expand Up @@ -85,4 +91,9 @@ public Vector intersection(Ray ray)
{
return null;
}

@Override
public boolean validMove(Vector startPoint, Vector endPoint) {
return false;
}
}
11 changes: 9 additions & 2 deletions src/main/java/app/model/agents/Human.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,17 @@ public Human(Vector position, Vector direction, double radius)
}

@Override
public void move()
public void updateLocation(Vector endPoint)
{
position = position.add(nextMove);
position = endPoint;
}

@Override
public Vector move()
{
Vector temp = position.add(nextMove);
nextMove = new Vector();
return temp;
}

public void walk(Vector nextMove)
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/app/model/boundary/Boundary.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ public interface Boundary
boolean isHit(Ray ray);

Vector intersection(Ray ray);

boolean validMove(Vector startPoint,Vector endPoint );
}
3 changes: 2 additions & 1 deletion src/main/java/app/model/boundary/BoundaryFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ public static ArrayList<Boundary> make(FurnitureType f, Rectangle2D rectangle)
return objects;
}


private static Boundary create(FurnitureType f, Vector a, Vector b)
{
switch (f)
{
case WALL -> { return new VisibleBoundary(a, b);}
case SHADE -> { return new InvisibleBoundary(a, b);}
case GLASS -> { return new TransparentBoundary();}
case GLASS -> { return new TransparentBoundary(a, b);}
}
return null; // Redundant by design
}
Expand Down
28 changes: 16 additions & 12 deletions src/main/java/app/model/boundary/InvisibleBoundary.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package app.model.boundary;

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

Expand All @@ -9,21 +10,18 @@ public class InvisibleBoundary implements Boundary
protected Vector a;
protected Vector b;

public InvisibleBoundary(Vector a, Vector b)
{
public InvisibleBoundary(Vector a, Vector b) {
this.a = a;
this.b = b;
}

@Override
public void draw(GraphicsContext gc)
{
public void draw(GraphicsContext gc) {

}

@Override
public boolean isHit(Ray ray)
{
public boolean isHit(Ray ray) {
double x_1 = a.getX();
double y_1 = a.getY();
double x_2 = b.getX();
Expand All @@ -34,13 +32,13 @@ public boolean isHit(Ray ray)
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);
double denominator = (x_1 - x_2) * (y_3 - y_4) - (y_1 - y_2) * (x_3 - x_4);

if(denominator == 0)
if (denominator == 0)
return false;

double t = ((x_1 - x_3)*(y_3 - y_4) - (y_1 - y_3)*(x_3 - x_4)) / denominator;
double u = ((x_1 - x_3)*(y_1 - y_2) - (y_1 - y_3)*(x_1 - x_2)) / denominator;
double t = ((x_1 - x_3) * (y_3 - y_4) - (y_1 - y_3) * (x_3 - x_4)) / denominator;
double u = ((x_1 - x_3) * (y_1 - y_2) - (y_1 - y_3) * (x_1 - x_2)) / denominator;

return 0 < t && t < 1 && u > 0;
}
Expand All @@ -57,10 +55,16 @@ public Vector intersection(Ray ray)
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;
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;
double x = x_1 + (t * (x_2 - x_1));
double y = y_1 + (t * (y_2 - y_1));
return new Vector(x, y);
}

@Override
public boolean validMove(Vector startPoint, Vector endPoint)
{
return Intersection.findIntersection(startPoint,endPoint,a,b);
}
}
14 changes: 9 additions & 5 deletions src/main/java/app/model/boundary/TransparentBoundary.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
package app.model.boundary;

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

public class TransparentBoundary implements Boundary
public class TransparentBoundary extends InvisibleBoundary
{
@Override
public void draw(GraphicsContext gc) {}
public TransparentBoundary(Vector a, Vector b)
{
super(a, b);
}

@Override
public boolean isHit(Ray ray)
{
return false;
return false; // Overrides super -> light goes through it
}

@Override
public Vector intersection(Ray ray)
{
return null;
return null; // Overrides super -> light goes through it
}

}
2 changes: 2 additions & 0 deletions src/main/java/app/model/boundary/VisibleBoundary.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ public void draw(GraphicsContext gc)
gc.setStroke(Color.BLACK);
gc.strokeLine(a.getX(), a.getY(), b.getX(), b.getY());
}


}
2 changes: 1 addition & 1 deletion src/main/java/app/model/furniture/FurnitureType.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ public enum FurnitureType
{
WALL,
SHADE,
GLASS
GLASS,
}
12 changes: 10 additions & 2 deletions src/main/java/app/model/map/Map.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import app.controller.linAlg.Vector;
import app.model.agents.Agent;
import app.model.agents.Human;
import app.model.boundary.Boundary;
import app.model.furniture.Furniture;
import app.model.furniture.FurnitureFactory;
import app.model.furniture.FurnitureType;
Expand All @@ -23,15 +24,15 @@ public Map(Settings settings)
this.settings = settings;

agents = new ArrayList<>();
human = new Human(new Vector(400, 250), new Vector(1,0), 10);
human = new Human(new Vector(380, 250), new Vector(1,0), 10);
agents.add(human);

furniture = new ArrayList<>();
settings.getWalls().forEach(e -> addFurniture(FurnitureType.WALL, e));
settings.getShade().forEach(e -> addFurniture(FurnitureType.SHADE, e));

// Temp test of glass
Rectangle2D window = new Rectangle2D(300,150, 50,50);
Rectangle2D window = new Rectangle2D(200,150, 50,50);
furniture.add(FurnitureFactory.make(FurnitureType.GLASS, window));

System.out.println("done.");
Expand Down Expand Up @@ -63,4 +64,11 @@ public ArrayList<Furniture> getFurniture()
{
return furniture;
}

public ArrayList<Boundary> getBoundaries()
{
ArrayList<Boundary> boundaries = new ArrayList<>();
furniture.forEach(e -> boundaries.addAll(e.getBoundaries()));
return boundaries;
}
}
Loading