Flocker_small.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import sim.engine.SimState;
  2. import sim.util.Bag;
  3. import sim.field.continuous.Continuous2D;
  4. import sim.util.Double2D;
  5. import sim.engine.Steppable;
  6. public class Flocker_small implements Steppable
  7. {
  8. public Double2D loc;
  9. public Double2D lastd;
  10. public Continuous2D flockers;
  11. public Flocking_small theFlock;
  12. public Flocker_small(final Double2D location, final Double2D lastd) {
  13. this.lastd = lastd;
  14. this.loc = location;
  15. }
  16. public Bag getNeighbors() {
  17. return this.flockers.getNeighborsWithinDistance(this.loc, this.theFlock.neighborhood, true);
  18. }
  19. public Double2D momentum() {
  20. return this.lastd;
  21. }
  22. public Double2D consistency(final Bag b, final Continuous2D flockers) {
  23. double x = 0.0;
  24. double y = 0.0;
  25. int i = 0;
  26. int count = 0;
  27. for (i = 0; i < b.numObjs; ++i) {
  28. final Flocker_small other = (Flocker_small)b.objs[i];
  29. if (other != this) {
  30. final Double2D m = ((Flocker_small)b.objs[i]).momentum();
  31. x += m.x;
  32. y += m.y;
  33. ++count;
  34. }
  35. }
  36. if (count > 0) {
  37. x /= count;
  38. y /= count;
  39. }
  40. return new Double2D(x, y);
  41. }
  42. public Double2D cohesion(final Bag b, final Continuous2D flockers) {
  43. double x = 0.0;
  44. double y = 0.0;
  45. int count = 0;
  46. int i;
  47. Flocker_small other;
  48. double dx;
  49. double dy;
  50. for (i = 0; i < b.numObjs; ++i) {
  51. other = (Flocker_small)b.objs[i];
  52. if (other != this) {
  53. dx = other.loc.x - this.loc.x;
  54. dy = other.loc.y - this.loc.y;
  55. x += dx;
  56. y += dy;
  57. ++count;
  58. }
  59. }
  60. if (count > 0) {
  61. x /= count;
  62. y /= count;
  63. }
  64. return new Double2D(x, y);
  65. }
  66. public Double2D avoidance(final Bag b, final Continuous2D flockers) {
  67. double x = 0.0;
  68. double y = 0.0;
  69. int i = 0;
  70. int count = 0;
  71. for (i = 0; i < b.numObjs; ++i) {
  72. final Flocker_small other = (Flocker_small)b.objs[i];
  73. if (other != this) {
  74. final double dx = other.loc.x - this.loc.x;
  75. final double dy = other.loc.y - this.loc.y;
  76. final double lensquared = dx * dx + dy * dy;
  77. ++count;
  78. if (lensquared < 1.0) {
  79. x -= dx;
  80. y -= dy;
  81. }
  82. }
  83. }
  84. if (count > 0) {
  85. x /= count;
  86. y /= count;
  87. }
  88. return new Double2D(x, y);
  89. }
  90. public void step(final SimState state) {
  91. final Flocking_small flock = (Flocking_small)state;
  92. this.loc = flock.flockers.getObjectLocation((Object)this);
  93. final Bag b = this.getNeighbors();
  94. final Double2D avoid = this.avoidance(b, flock.flockers);
  95. final Double2D cohe = this.cohesion(b, flock.flockers);
  96. final Double2D cons = this.consistency(b, flock.flockers);
  97. final Double2D mome = this.momentum();
  98. double dx = flock.cohesion * cohe.x + flock.avoidance * avoid.x + flock.consistency * cons.x + mome.x;
  99. double dy = flock.cohesion * cohe.y + flock.avoidance * avoid.y + flock.consistency * cons.y + mome.y;
  100. final double dis = Math.sqrt(dx * dx + dy * dy);
  101. if (dis > 0.0) {
  102. dx = dx / dis;
  103. dy = dy / dis;
  104. }
  105. this.lastd = new Double2D(dx, dy);
  106. this.loc = new Double2D(flock.flockers.stx(this.loc.x + dx), flock.flockers.sty(this.loc.y + dy));
  107. flock.flockers.setObjectLocation((Object)this, this.loc);
  108. }
  109. }