Path-Creator icon indicating copy to clipboard operation
Path-Creator copied to clipboard

Editing control points in script

Open Gwemlin opened this issue 4 years ago • 7 comments

I need to change the control point positions at either end of an open path in real time. I didn't see any function for changing the position of control points in the documentation or anywhere in the code suggestions. Am I missing something?

Gwemlin avatar Apr 28 '20 17:04 Gwemlin

Hi,

It took me time to understand how it works and I just discovered it today. It is indicated in the BezierPath script that the path between 2 points is created thanks to 4 points, 2 anchors and 2 control points which are in this order : [anchor1, control1, control2, anchor2]. There is a public function to edit the point at a certain index i. So here you just have to put the index 1 and 2 where you want to adjust the tangent !

Hope this solve your problem !

YuudachiPoiii avatar May 11 '20 16:05 YuudachiPoiii

I need to change the control point positions at either end of an open path in real time. I didn't see any function for changing the position of control points in the documentation or anywhere in the code suggestions. Am I missing something?

there is no functions for this it's the Handles.FreeMoveHandle() function which is a unity editor function that makes gameobjects positioning easier it returns a Vector3 as result depending on mouse position

Oseryx avatar May 16 '20 16:05 Oseryx

I need to change the control point positions at either end of an open path in real time. I didn't see any function for changing the position of control points in the documentation or anywhere in the code suggestions. Am I missing something?

To change this you can use Handles.FreeMoveHandle() which is a unity editor function or use the Hanfles.PositionHandle() function which is also a function of the unity editor API and the cool thing about this function is having a transform tool when you click on the target point or object

Oseryx avatar May 16 '20 16:05 Oseryx

I think he wants to change the point during runtime. To gather the position he wants to put the point he can still use raycast. By gathering the BezierPath (not the VertexPath !) and setting the Vector3 of the anchor to the correct index, he can create the shape he wants to. taxi_yellow2.GetComponent<PathCreator>().bezierPath = bezierPath6; taxi_yellow2.GetComponent<PathCreator>().bezierPath.SetPoint(1,point_1,false); taxi_yellow2.GetComponent<PathCreator>().bezierPath.SetPoint(2,point_1,false);

For a 2 anchor point bezier curve, this code sets the control point of the two points at the same position.

YuudachiPoiii avatar May 16 '20 17:05 YuudachiPoiii

I think he wants to change the point during runtime. To gather the position he wants to put the point he can still use raycast. By gathering the BezierPath (not the VertexPath !) and setting the Vector3 of the anchor to the correct index, he can create the shape he wants to. taxi_yellow2.GetComponent<PathCreator>().bezierPath = bezierPath6; taxi_yellow2.GetComponent<PathCreator>().bezierPath.SetPoint(1,point_1,false); taxi_yellow2.GetComponent<PathCreator>().bezierPath.SetPoint(2,point_1,false);

For a 2 anchor point bezier curve, this code sets the control point of the two points at the same position.

maybe but for this I recommend to have a bool that allows to still calculating bezier path when running because there is no interest of this only if the path changes when the game runs

Oseryx avatar May 16 '20 18:05 Oseryx

At each update, you can recreate a Bezier path with new list of waypoints or change only one using the previous code

YuudachiPoiii avatar May 16 '20 18:05 YuudachiPoiii

@YuudachiPoiii You are correct about manipulating the control points.

For anyone else trying to figure out how to manipulate control points through code, the constructor of the BezierPath won't directly let you manipulate the control points because it always defaults the control points into the "Automatic" control mode.

After you create a BezierPath via code, change ControlPointMode of your BezierPath to "Free" or "Mirrored". The index mapping of the points in a BezierPath is setup like this: 1st index = Anchor Point 1 2nd index = Control Point 1 (for Anchor 1) 3rd index = Control Point 2 (for Anchor 2) 4th index = Anchor Point 2 ^ These would be the indices of the first segment of the bezier path. This structure repeats for additional segments starting with the same index the last segment ended with; so for example the indices of the points for your second segment are 4 - 7 with the 4th index = Anchor Point 2, 5th index = Control Point 3 (for Anchor 2), 6th index = Control Point 3 (for anchor 3), 7th index = Anchor Point 3.

You can check the values of the control points via GetPoint(int index), and then set the values via SetPoint(int index). You would use SetPoint() for the control point index of your choice using the index mapping structure mentioned.

kirbyderby2000 avatar Sep 29 '20 01:09 kirbyderby2000