Hello,
I’ve created a component, where input is a line an boolean. I would like to move a point along this line, therefore a I’ve divided a line into 10 segments. From the one of the segments I’ve defined a vector, so the point has the distance and direction to move.
To move point in grasshopper I am using timer (also trigger) Component. How can I stop this timer/trigger component, if the point reaches the end of the line. Or at least, will change the direction and make the point to go back to the start of the line.
Here is the C# code of this component:
public class Motion : GH_Component
{
public Motion()
: base("MyComponent1", "Nickname",
"Description",
"Category", "Motion")
{
}
protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager)
{
pManager.AddCurveParameter("Curve", "C", "Offset curve for motion", GH_ParamAccess.item);
pManager.AddBooleanParameter("Reset", "Reset", "Reset", GH_ParamAccess.item);
}
protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager)
{
pManager.AddPointParameter("Particle", "Particle", "Particle", GH_ParamAccess.item);
pManager.AddPointParameter("Start", "Start", "Start", GH_ParamAccess.item);
pManager.AddPointParameter("End", "End", "End", GH_ParamAccess.item);
pManager.AddVectorParameter("Vector", "Vector", "Vector", GH_ParamAccess.item);
pManager.AddPointParameter("Division points", "Division poins", "Division poins", GH_ParamAccess.item);
}
Point3d currentPosition;
protected override void SolveInstance(IGH_DataAccess DA)
{
Curve road = new LineCurve();
DA.GetData(0, ref road);
bool iReset = true;
DA.GetData(1, ref iReset);
double road_length = road.GetLength();
double seg_length = road_length / 10.0;
Point3d[] points;
road.DivideByLength(seg_length, true, out points);
List<Point3d> pointsList = points.ToList();
Vector3d iVelocity = new Vector3d(points[1].X - road.PointAtStart.X, points[1].Y - road.PointAtStart.Y, 0);
if (iReset)
{
currentPosition = new Point3d(road.PointAtStart.X, road.PointAtStart.Y, 0.0);
return;
}
currentPosition += iVelocity;
DA.SetData(0, currentPosition);
DA.SetData(1, road.PointAtStart);
DA.SetData(2, road.PointAtEnd);
DA.SetData(3, iVelocity);
DA.SetDataList(4, pointsList);
}
}
Here is a screenshot, where you can see, that the point is out of the line (for now):
Thank you in advance!