Skip to content

Commit

Permalink
Init
Browse files Browse the repository at this point in the history
  • Loading branch information
Creepler13 committed Jun 11, 2023
0 parents commit a6a1632
Show file tree
Hide file tree
Showing 13 changed files with 951 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jdk-19.0.2">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/Processing"/>
<classpathentry kind="output" path="bin"/>
</classpath>
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/bin/
17 changes: 17 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Octree</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
12 changes: 12 additions & 0 deletions .settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=9
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=9
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.release=enabled
org.eclipse.jdt.core.compiler.source=9
126 changes: 126 additions & 0 deletions src/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@

import java.util.ArrayList;
import java.util.Iterator;

import Octree.Octree;
import Octree.OctreeNode;
import Octree.OctreeObject;
import boid.Boid;
import core.Boundary;
import processing.core.PApplet;
import processing.event.KeyEvent;
import processing.event.MouseEvent;

public class Main extends PApplet {

public static void main(String[] args) {
PApplet.main("Main");
}

Octree world = new Octree(1800, 900, 3, 5);
ArrayList<Boundary> walls = new ArrayList<>();

int boids = 60;

public void settings() {
size(world.getWidth(), world.getHeight());
}

public void setup() {

for (int i = 0; i < boids; i++) {
world.add(new Boid(200, 200, world, this));
}

walls.add(new Boundary(0, 0, world.getWidth(), 0));
walls.add(new Boundary(0, 0, 0, world.getHeight()));
walls.add(new Boundary(world.getWidth(), 0, world.getWidth(), world.getHeight()));
walls.add(new Boundary(0, world.getHeight(), world.getWidth(), world.getHeight()));

}

public void draw() {

if (paused)
return;

clear();
background(255);

ArrayList<OctreeNode> nodes = new ArrayList<>();
nodes.add(world);
nodes.addAll(world.getAllChildren());

for (OctreeNode node : nodes) {
int x = node.getX();
int y = node.getY();
int width = node.getWidth();
int height = node.getHeight();

line(x, y, x + width, y);
line(x + width, y, x + width, y + height);
line(x + width, y + height, x, y + height);
line(x, y + height, x, y);

}

for (Boundary wall : walls) {
line((int) wall.a.x, (int) wall.a.y, (int) wall.b.x, (int) wall.b.y);
}

ArrayList<OctreeObject> objects = world.getAllObjects();

for (OctreeObject object : objects) {
Boid boid = ((Boid) object);

boid.update();

line((int) boid.getX(), (int) boid.getY(), (int) (boid.getX() + boid.velocity.x * 10),
(int) (boid.getY() + boid.velocity.y * 10));



fill(((Boid)object).color);
circle((int) object.getX(), (int) object.getY(), boidSize);

}

}

boolean paused = false;

@Override
public void keyReleased(KeyEvent event) {
switch (event.getKeyCode()) {
case 32:
paused = !paused;
break;
}
;
}

Boid selectedBoid;

int boidSize = 10;

@Override
public void mouseReleased(MouseEvent event) {
int mouseX = event.getX();
int mouseY = event.getY();

System.out.println(mouseX + " " + mouseY);
for (OctreeObject obj : world.getAllObjects()) {
Boid boid = (Boid) obj;

double x = boid.getX(), y = boid.getY();

line((int) x, (int) y, (int) (x + boidSize), (int) (y + boidSize));
if (mouseX >= x && mouseX < x + boidSize && mouseY >= y && mouseY < y + boidSize)
boid.drawDebug = true;
else
boid.drawDebug = false;

}

}
}
25 changes: 25 additions & 0 deletions src/Octree/Octree.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package Octree;

public class Octree extends OctreeNode {

public int maxNodeSize, maxNodes, minWidth, minHeight;

public Octree(int width, int height, int maxNodeSize, int maxNodes, int minWidth, int minHeight) {
super(0, 0, width, height, null, null);
this.octree = this;
this.maxNodeSize = maxNodeSize;
this.maxNodes = maxNodes;
this.minWidth = minWidth;
this.minHeight = minHeight;
}

public Octree(int width, int height, int maxNodeSize, int maxNodes) {
super(0, 0, width, height, null, null);
this.octree = this;
this.maxNodeSize = maxNodeSize;
this.maxNodes = maxNodes;
this.minWidth = 10;
this.minHeight = 10;
}

}
188 changes: 188 additions & 0 deletions src/Octree/OctreeNode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
package Octree;

import java.util.ArrayList;

public class OctreeNode {

private int x, y, width, height, distanceToTop = 0;

Octree octree;

private OctreeNode parent;

public OctreeNode(int x, int y, int width, int height, Octree octree, OctreeNode parent) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.octree = octree;
this.parent = parent;

if (parent != null) // means its the octree
distanceToTop++;

}

private ArrayList<OctreeObject> objects = new ArrayList<>();

private ArrayList<OctreeNode> children = new ArrayList<>();

public void add(OctreeObject obj) {
octree.addFull(obj);
}

void addFull(OctreeObject obj) {
obj.setOctree(octree);

OctreeNode node = getQuadrant(obj);

//means no children (this node)
if (node == null) {
objects.add(obj);
obj.setOctreeNode(this);
obj.setOctreeParentNode(parent);
} else {
node.addFull(obj);
}

if (!hasChildren() && objects.size() >= octree.maxNodeSize && distanceToTop < octree.maxNodes
&& this.width > octree.minWidth && this.height > octree.minHeight) {
children = newNodes(x, y, width, height, octree, this);

for (OctreeObject object : objects) {
OctreeNode temp = getQuadrant(object);
temp.addFull(object);
}

objects.clear();

}

}

public void remove(OctreeObject object) {
if (hasChildren())
for (OctreeNode octreeNode : children) {
octreeNode.remove(object);
}
else
objects.remove(object);

if (hasChildren() && objects.size() < octree.maxNodeSize) {

objects.addAll(getAllObjects());

children.clear();
}
}

private OctreeNode getQuadrant(OctreeObject object) {

if (!hasChildren())
return null;

for (OctreeNode octreeNode : children) {

if (object.getX() >= octreeNode.getX() && object.getY() >= octreeNode.getY()
&& object.getX() <= octreeNode.getX() + octreeNode.getWidth()
&& object.getY() <= octreeNode.getY() + octreeNode.getHeight())
return octreeNode;
}

// maybe problematisch
// if (parent != null)
// return parent.getQuadrant(object);

return children.get(0);
}



public boolean hasChildren() {
return !children.isEmpty();
}

public int getX() {
return x;
}

public int getY() {
return y;
}

public int getWidth() {
return width;
}

public int getHeight() {
return height;
}

public Octree getOctree() {
return octree;
}

public OctreeNode getParent() {
return parent;
}

public ArrayList<OctreeObject> getObjects() {
return (ArrayList<OctreeObject>) objects.clone();
}

public ArrayList<OctreeObject> getAllObjects() {

ArrayList<OctreeObject> c = getObjects();

for (OctreeNode octreeNode : getChildren()) {
c.addAll(octreeNode.getAllObjects());
}

return c;
}

public ArrayList<OctreeNode> getChildren() {
return (ArrayList<OctreeNode>) children.clone();
}

public ArrayList<OctreeNode> getAllChildren() {

ArrayList<OctreeNode> c = getChildren();

for (OctreeNode octreeNode : getChildren()) {
c.addAll(octreeNode.getAllChildren());
}

return c;
}

public int size() {
return objects.size();
}

private static ArrayList<OctreeNode> newNodes(int parenX, int parentY, int parentWidth, int parentHeight,
Octree octree, OctreeNode parent) {
ArrayList<OctreeNode> nodes = new ArrayList<>();

int width = parentWidth / 2;
int height = parentHeight / 2;

nodes.add(new OctreeNode(parenX, parentY, width, height, octree, parent));
nodes.add(new OctreeNode(parenX + width, parentY, width, height, octree, parent));
nodes.add(new OctreeNode(parenX, parentY + height, width, height, octree, parent));
nodes.add(new OctreeNode(parenX + width, parentY + height, width, height, octree, parent));

return nodes;

}

@Override
public String toString() {
String s = this.x + " " + this.y + " " + this.width + " " + this.height + " [ allChildren:"
+ getAllChildren().size() + ", objects:" + getObjects().size() + ", allObjects:"
+ getAllObjects().size() + " ]";

return s;
}

}
Loading

0 comments on commit a6a1632

Please sign in to comment.