logo

[J]eepresesCreepers

Projects details

Code Creates Random Creeper Agents moving around in 2d & 3d. Their proximity is indexes by links between creepers. We will continue building off of this class – C# 

—Quick Study building off of Creepers example. Here we have creepers moving randomly in 2d & 3d. When they each the boundary they turn around and go the opposite direction. I wanted to diagram their proximity as a visual cue for the later studies. The link connects any creepers that are within a threshold of each other. We will use this value to trigger interaction between them. Performance of the component is very quick, we are using the timer to run the component live without a number of iterations.—

Using Grasshopper C# components for the code snippets below – All Test code is provided on an ‘as-is’ basis for evaluation, testing and commenting purposes. I am not responsible for any damage incurred by its usage. Use at your own risk.

Base Creeper Agent Class - C# Code

//Code written by Luis Quinones @ [complicitMatter]
  //Feel free to use and modify the code in any way you want, please comment below with suggestions, ideas of any relevant throughts
  
  private void RunScript(bool reset, int ptCount, int dimension, bool connect, double conn_thresh, double moveValue, ref object A, ref object B, ref object C)
  {
    Random rnd = new Random();
    List<Vector3d> vecs = new List<Vector3d>();
    List<Point3d> returnedvecs = new List<Point3d>();

    if(reset){
      this.moveVecAmp = moveValue;
      this.moveList = new List<Vector3d>();
      this.dimensions = dimension;

      if(this.dimensions == 0){
        this.bb = new BoundingBox(-250, -250, 0, 250, 250, 0);
      }else{
        this.bb = new BoundingBox(-250, -250, -250, 250, 250, 250);
      }

      this.startList = new List<Vector3d>();

      for(int i = 0; i < ptCount; i++){
        if(this.dimensions == 0){
          this.moveVec = new Vector3d(rnd.Next(-1, 2), rnd.Next(-1, 2), 0);
          this.speedVec = new Vector3d(rnd.NextDouble(), rnd.NextDouble(), 0);
          this.startPos = new Vector3d(rnd.Next((int) bb.Min[0], (int) bb.Max[0]), rnd.Next((int) bb.Min[1], (int) bb.Max[1]), 0);

        }else{
          this.moveVec = new Vector3d(rnd.Next(-1, 2), rnd.Next(-1, 2), rnd.Next(-1, 2));
          this.speedVec = new Vector3d(rnd.NextDouble(), rnd.NextDouble(), rnd.NextDouble());
          this.startPos = new Vector3d(rnd.Next((int) bb.Min[0], (int) bb.Max[0]), rnd.Next((int) bb.Min[1], (int) bb.Max[1]), (int) bb.Min[1]);

        }
        this.startList.Add(this.startPos);

        int switchDir = rnd.Next(-1, 1);
        if(switchDir < 0){
          this.speedVec *= this.moveVecAmp;
        }else{
          this.speedVec *= -this.moveVecAmp;
        }
        this.moveVec += this.speedVec;
        this.moveList.Add(this.moveVec);
      }
      creeperDudes = this.startList;

    }else{

      creepers creeperPts = new creepers(this.startList, this.moveList, bb, this.dimensions, connect, conn_thresh);
      creeperPts.move();

      this.startList = creeperPts.outputList;

      creeperDudes = this.startList;
      connectors = creeperPts.lineList;
      boundBox = this.bb;
    }
  }

  private List<Vector3d> moveList = new List<Vector3d>();
  private List<Vector3d> startList = new List<Vector3d>();

  private Vector3d startPos = new Vector3d();
  private Vector3d moveVec;
  private Vector3d speedVec;
  private BoundingBox bb;
  private int dimensions;
  private double moveVecAmp;

  public class creepers{

    private BoundingBox bbox;
    public Vector3d pos = new Vector3d();
    public List<Vector3d> posList = new List<Vector3d>();
    public List<Vector3d> posMoveList = new List<Vector3d>();
    public List<Vector3d> outputList = new List<Vector3d>();
    public List<Line> lineList = new List<Line>();
    private bool flag;
    private int dim;
    private bool conn;
    private double ct;

    public creepers(List<Vector3d> vecs, List<Vector3d> move, BoundingBox bb, int dimension, bool connect, double connThresh){

      this.posList = vecs;
      this.posMoveList = move;
      this.bbox = bb;
      this.dim = dimension;
      this.conn = connect;
      this.ct = connThresh;
    }

    public void move(){
      for(int i = 0; i < this.posList.Count; i++){
        this.pos = new Vector3d();
        this.pos += this.posList[i];
        this.checkLoc();

        if(this.flag){
          Vector3d temp = this.posMoveList[i];
          if(this.dim == 0){
            if(this.pos.X >= (int) this.bbox.Max[0] || this.pos.X <= (int) this.bbox.Min[0]){
              temp.X *= -1;
            }
            if(this.pos.Y >= (int) this.bbox.Max[1] || this.pos.Y <= (int) this.bbox.Min[1]){
              temp.Y *= -1;
            }
          }else{
            if(this.pos.X >= (int) this.bbox.Max[0] || this.pos.X <= (int) this.bbox.Min[0]){
              temp.X *= -1;
            }
            if(this.pos.Y >= (int) this.bbox.Max[1] || this.pos.Y <= (int) this.bbox.Min[1]){
              temp.Y *= -1;
            }
            if(this.pos.Z >= (int) this.bbox.Max[2] || this.pos.Z <= (int) this.bbox.Min[2]){
              temp.Z *= -1;
            }
          }
          this.posMoveList[i] = temp;
        }
        this.pos += this.posMoveList[i];
        this.outputList.Add(this.pos);
        if(this.conn){
          link();
        }
      }
    }

    public void link(){
      for(int i = 0; i < this.outputList.Count; i++){
        Point3d othercreeper = (Point3d) this.outputList[i];
        double dist = othercreeper.DistanceTo((Point3d) this.pos);

        if(dist > 0 && dist < this.ct){
          Line l = new Line(othercreeper, (Point3d) this.pos);
          lineList.Add(l);
        }
      }
    }
    
    public void checkLoc(){
      flag = new bool();
      if(!this.bbox.Contains((Point3d) this.pos)){
        this.flag = true;
      }else{
        this.flag = false;
      }
    }
  }
Update

 

 

  • Share

Leave a reply

Your email address will not be published. Required fields are marked *