Hello,
Idea of this code is that, few spheres(bees) would come to a common point from different location and will keep hovering over it. I am able to create behavior for one single sphere getting attracted towards the point, but when I am making array of an object, there is no error but output is not visible.
Why the output of c# component is giving "Script_Instance+(Mover)’’, it should give a list of surfaces(spheres). 01_Swarm bees.gh (15.2 KB)
Any help would be great! attaching GH file too.
// List<Mover> bees = new List<Mover>();
List<Mover> Allbees = new List<Mover>();
for(int i = 0; i < movers.Length; i++)
{
movers[i] = new Mover();
// bees.Add(movers[i]);
}
for(int i = 0;i < movers.Length;i++)
{
movers[i].Update(point);
movers[i].Display(radius, reset);
Allbees.Add(movers[i]);
}
swarms = Allbees;
}
//
Mover movers = new Mover[5];
class PVector{
public double X;
public double Y;
//public double Z;
// constructor
public PVector(double x, double y)
{
X = x;
Y = y;
//Z = z;
}
// create a method for vector addition
public void Vadd(PVector v)
{
X = X + v.X;
Y = Y + v.Y;
//Z = Z + v.Z * zVelocity;
}
// create a method for vector subtraction
static public PVector Vsub(PVector v1, PVector v2)
{
double X = v1.X - v2.X;
double Y = v1.Y - v2.Y;
// Z = Z - v.Z * zVelocity;
return new PVector(X, Y);
}
// create a method for vector multiplication
public void Vmul(double scale)
{
X = X * scale;
Y = Y * scale;
// Z = Z * scale;
}
// create a method for vector division
public void Vdiv(double scale)
{
X = X / scale;
Y = Y / scale;
// Z = Z * scale;
}
// create a method for vector magnitude
public double Vmag()
{
return Math.Pow(X * X + Y * Y, 1 / 2);
}
// create a method for vector length
public double VmagSq()
{
return X * X + Y * Y;
}
// create a method for vector Normalization
public void Vnormalize()
{
double m = Vmag();
if(m != 0){
Vdiv(m);
}
}
// create a method for vector magnitude limit
public void Vlimit(double max)
{
if(VmagSq() > max * max ){
Vnormalize();
Vmul(max);
}
}
//Random Vector
static public PVector Random2d(){
double vx; double vy; PVector target;
Random rnd = new Random();
vx = rnd.NextDouble() * rnd.Next(-1, 2);
vy = rnd.NextDouble() * rnd.Next(-1, 2);
target = new PVector(vx, vy);
target.Vnormalize();
return target;
}
}
// make another class “Mover” to oerate PVector dynamically
class Mover{
// accessible parameters
public PVector location;
public PVector velocity;
public PVector acceleration;
public double topSpeed;
//public Random rnd;
//constructor
public Mover(){
Random rnd = new Random();
location = new PVector(rnd.Next(0, 50), rnd.Next(0, 50));
velocity = new PVector(1.5, 3);
acceleration = new PVector(-0.001, 0.01);
topSpeed = 0.9;
}
//class METHODS (Update location Method)
public void Update(Point3d point){
PVector attractor = new PVector(point.X, point.Y);
PVector dir = PVector.Vsub(attractor, location);
dir.Vnormalize();
dir.Vmul(0.05);
acceleration = dir;
velocity.Vadd(acceleration);
velocity.Vlimit(topSpeed);
location.Vadd(velocity);
}
// MEthod to display the object(swarm/sphere/circle/whatever)
public Sphere Display(double radius, bool reset){
if(reset == false){
return new Sphere(new Point3d(0, 0, 0), radius);
}
else
return new Sphere(new Point3d(location.X, location.Y, 0), radius);
}
// define function for rectangle boundary
public Polyline Boundary(double width, double height)
{
List<Point3d> rPts = new List<Point3d>();
rPts.Add(new Point3d(0, 0, 0));
rPts.Add(new Point3d(width, 0, 0));
rPts.Add(new Point3d(width, height, 0));
rPts.Add(new Point3d(0, height, 0));
rPts.Add(new Point3d(0, 0, 0));
Polyline pl = new Polyline(rPts);
return pl;
}
}