C#_change parameters without resetting component

rhinocommon
grasshopper

(OmKar) #1

Hello,
I have made c# component by following some online tutorial, the class in it does not respond to parameter changes, I have to reset the component every time to see the changes. please let me know what to change in my code to fix that.

find attached .gh file,video nd script
it would be a great help!
thanks!

private void RunScript(bool iReset, bool play, Point3d target, double maxSpeed, double maxForce, ref object vehiclePt, ref object vector)
{
if(iReset || vh == null)
{
vh = new Vehicle(new Point3d(0, 0, 0),maxSpeed,maxForce);
}

else
{
  vh.Seek(target);
  vh.Update();
  vehiclePt = vh.Display();
  vector = vh.Orient(target);

  if(play)
  {
    Component.ExpireSolution(true);
  }
}

//

Vehicle vh;

class Vehicle
{
public Point3d position;
public Vector3d velocity;
public Vector3d acceleration;

// double r;
double maxForce; // max steering speed
double maxSpeed; // max speed

public Vehicle(Point3d Position, double max_speed, double max_force)
{
  acceleration = new Vector3d(0, 0, 0);
  velocity = new Vector3d(0, 0, 0);
  position = Position;
  // r = 6;
  maxSpeed = max_speed;
  maxForce = max_force;
}

// method to update position
public void Update()
{
  //Update Velocity
  velocity = Vector3d.Add(velocity, acceleration);

  //limit speed
  if(velocity.SquareLength > maxSpeed * maxSpeed)
  {
    velocity.Unitize();
    velocity = Vector3d.Multiply(velocity, maxSpeed);
  }
  position = position + velocity;

  //reset acceleration to 0 each cycle
  acceleration = Vector3d.Multiply(acceleration, 0);
}

public void ApplyForce(Vector3d force)
{
  // we could add mass here if we want A = F/M
  acceleration = Vector3d.Add(acceleration, force);
}

// A method that calculates a steering a force towards a target
//Steer = desired minus velocity
public void Seek(Point3d target)
{
  Vector3d desireVelocity = target - position; //A vector pointing from the position to the target

  //scale to the maximum speed
  desireVelocity.Unitize();
  desireVelocity = Vector3d.Multiply(desireVelocity, maxSpeed);

  //Steering = desired minus velocity
  Vector3d steer = Vector3d.Subtract(desireVelocity, velocity);

  //limit max steering force

  if(steer.SquareLength > maxForce * maxForce)
  {
    steer.Unitize();
    steer = Vector3d.Multiply(steer, maxForce);
  }
  ApplyForce(steer);
}

// orient vector towrds target
public Vector3d Orient(Point3d target)
{
  Vector3d orient = target - position;
  orient.Unitize();
  //orient = Vector3d.Multiply(orient, 25);
  return orient;
}

//dispaly target
public Point3d Display()
{
  return new Point3d(position.X, position.Y, position.Z);
}

}
// </Custom additional code>

01_Autonomus_Vehicle.gh (9.8 KB)
https://discourse-cdn-sjc1.com/mcneel/uploads/default/original/3X/b/5/b5c643fdf0104d925f1e64e1c77561f1e6aa8765.mp4


#2

As far as I can see the parameters of the component (maxSpeed, maxForce) are only set in the constructor of Vehicle. The constructor is only called at the beginning or when hitting reset.

I guess you need something like
vh.maxSpeed = maxSpeed;
vh.maxForce = maxForce;
inside your RunScript method. Probably before the if(play) clause, not sure though.


(OmKar) #3

yeah it does the work.
thanks!