public class BezierCurve
{
//Starts following bezier curve.
public void StartFollow()
{
//some code here.
}
}
public class BezierCurveBatch : MonoBehaviour
{
[SerializeField]
List<BezierCurve> m_lstChildren;
[SerializeField]
float m_delayStartCurve = 10;
float m_timeLeftToStartNextChild = 0;
bool m_isRunBatchCurve = false;
/// <summary>
/// Start batch follow after each interval.
/// </summary>
public void StartBatch()
{
m_isRunBatchCurve = true;
}
private void Update()
{
if (!m_isRunBatchCurve)
return;
m_timeLeftToStartNextChild -= Time.deltaTime;
if (m_timeLeftToStartNextChild <= 0.0f)
{
if (m_lstChildren.Count > 0) //if we have children left.
{
BezierCurve l_bCurveToStart = m_lstChildren[0]; //Getting top object.
m_lstChildren.RemoveAt(0); //removing top object.
l_bCurveToStart.StartFollow(); //Start follow bezier curve
m_timeLeftToStartNextChild = m_delayStartCurve; //resetting time.
}
if (m_lstChildren.Count == 0) //After processing last object, check if need to continue for next object.
m_isRunBatchCurve = false;
}
}
}
void recursive_bezier(double x1, double y1,
double x2, double y2,
double x3, double y3,
double x4, double y4)
{
// Calculate all the mid-points of the line segments
//----------------------
double x12 = (x1 + x2) / 2;
double y12 = (y1 + y2) / 2;
double x23 = (x2 + x3) / 2;
double y23 = (y2 + y3) / 2;
double x34 = (x3 + x4) / 2;
double y34 = (y3 + y4) / 2;
double x123 = (x12 + x23) / 2;
double y123 = (y12 + y23) / 2;
double x234 = (x23 + x34) / 2;
double y234 = (y23 + y34) / 2;
double x1234 = (x123 + x234) / 2;
double y1234 = (y123 + y234) / 2;
// Try to approximate the full cubic curve by a single straight line
//------------------
double dx = x4-x1;
double dy = y4-y1;
double d2 = fabs(((x2 - x4) * dy - (y2 - y4) * dx));
double d3 = fabs(((x3 - x4) * dy - (y3 - y4) * dx));
if((d2 + d3)*(d2 + d3) < m_distance_tolerance * (dx*dx + dy*dy))
{
add_point(x1234, y1234);
return;
}
// Continue subdivision
//----------------------
recursive_bezier(x1, y1, x12, y12, x123, y123, x1234, y1234);
recursive_bezier(x1234, y1234, x234, y234, x34, y34, x4, y4);
}
void bezier(double x1, double y1,
double x2, double y2,
double x3, double y3,
double x4, double y4)
{
add_point(x1, y1);
recursive_bezier(x1, y1, x2, y2, x3, y3, x4, y4);
add_point(x4, y4);
}