Hello everyone!
At the moment, I’m developing a visualization of a rope composed by tiny 2d box colliders. Due to the nature of the physics simulation of the rope, the control points are close to each other. Thus, I’ve decided to force the visualization to keep the points at a minimal safe distance in order to avoid getting an ArgumentException thrown by spline. Although, it seems that even though I assure a greater distance than the minimum established by Spline, 0.01f, the exception is thrown anyways.
private void SafeSequentialInsertPointIntoSpline(int index, Vector3 position)
{
if (index < _spriteShapeController.spline.GetPointCount() - 1)
{
throw new InvalidOperationException("Point is not being inserted sequentially.");
}
Vector3 previousPoint;
if (index > 0)
{
previousPoint = _spriteShapeController.spline.GetPosition(index - 1);
float squaredDistance = Vector3.SqrMagnitude(previousPoint - position);
if (squaredDistance < MinimalSafeDistanceBetweenPoints * MinimalSafeDistanceBetweenPoints)
{
Vector3 direction = position - previousPoint;
position += MinimalSafeDistanceBetweenPoints * direction.normalized;
}
}
_spriteShapeController.spline.InsertPointAt(index, position);
}
[Full implementation can be seen on balloondle/RopeVisualizer.cs at gameplay-mechanics · elementalg/balloondle (github.com)]
The method has been debugged, and the distance is assured to be greater than the minimum.
Any ideas on why this may happen?
Thank you for your time!
Kinds Regards,
Gabriel.