Skip to content

Commit

Permalink
Improve separation behaviour for flocking
Browse files Browse the repository at this point in the history
  • Loading branch information
fullwall committed Oct 21, 2015
1 parent e449378 commit 7e517da
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
5 changes: 2 additions & 3 deletions src/main/java/net/citizensnpcs/api/ai/flocking/Flocker.java
Expand Up @@ -4,10 +4,10 @@
import java.util.Collection;
import java.util.List;

import net.citizensnpcs.api.npc.NPC;

import org.bukkit.util.Vector;

import net.citizensnpcs.api.npc.NPC;

public class Flocker implements Runnable {
private final List<FlockBehavior> behaviors;
private final NPCFlock flock;
Expand All @@ -29,7 +29,6 @@ public void run() {
for (FlockBehavior behavior : behaviors) {
base.add(behavior.getVector(npc, nearby));
}
// base.multiply(1.0 / (nearby.size() * 50));
base = clip(maxForce, base);
npc.getEntity().setVelocity(npc.getEntity().getVelocity().add(base));
}
Expand Down
Expand Up @@ -2,10 +2,10 @@

import java.util.Collection;

import net.citizensnpcs.api.npc.NPC;

import org.bukkit.util.Vector;

import net.citizensnpcs.api.npc.NPC;

public class SeparationBehavior implements FlockBehavior {
private final double weight;

Expand All @@ -17,12 +17,17 @@ public SeparationBehavior(double weight) {
public Vector getVector(NPC npc, Collection<NPC> nearby) {
Vector steering = new Vector(0, 0, 0);
Vector pos = npc.getEntity().getLocation().toVector();
int c = 0;
for (NPC neighbor : nearby) {
if (!neighbor.isSpawned())
continue;
Vector repulse = pos.subtract(neighbor.getEntity().getLocation().toVector()).multiply(1.0 / 3.0);
double dist = neighbor.getEntity().getLocation().toVector().distance(pos);
Vector repulse = pos.subtract(neighbor.getEntity().getLocation().toVector()).normalize()
.divide(new Vector(dist, dist, dist));
steering = repulse.add(steering);
c++;
}
return steering.multiply(weight);
steering = steering.divide(new Vector(c, c, c));
return steering.subtract(npc.getEntity().getVelocity()).multiply(weight);
}
}

0 comments on commit 7e517da

Please sign in to comment.