07-20-2022, 05:39 PM
Hello,
I am using Curvy to make train tracks in a game, and I am very impressed with it. However, I have got a bit of a problem, and I would greatly appreciate some advice please.
In my game the player can place the tracks in runtime, by clicking on the map and placing control points. Then, when they have finished placing a rail track, the generator converts this into a mesh, using a spline shape for the rail's cross-section like in demo scene 25. This all works well.
The problem I have is getting the generated track meshes to match up to the spline segments. I would like both to be the same length, so that if the player clicks on a track mesh segment, they can delete it, and also delete the spline segment underneath.
At the moment the segments and mesh objects do not match up. Before the generator creates the rail track meshes, I have code that redistributes the control points so that their segments are the same length as the Volume Mesh modules SplitLength value. However, when I check the result in the scene view, the length of the rail meshes is bigger than the segment length. This means that the meshes overlap more than one segment.
Is there a way to get the length of the track meshes to be the same as the length of the spline segments, so that each mesh starts at the start of the segment and stops at its end?
So each mesh equals the segment's F0-F1 length and position all along the spline?
Here is the code I am using (it uses part of your Equalize function):
I am using Curvy to make train tracks in a game, and I am very impressed with it. However, I have got a bit of a problem, and I would greatly appreciate some advice please.
In my game the player can place the tracks in runtime, by clicking on the map and placing control points. Then, when they have finished placing a rail track, the generator converts this into a mesh, using a spline shape for the rail's cross-section like in demo scene 25. This all works well.
The problem I have is getting the generated track meshes to match up to the spline segments. I would like both to be the same length, so that if the player clicks on a track mesh segment, they can delete it, and also delete the spline segment underneath.
At the moment the segments and mesh objects do not match up. Before the generator creates the rail track meshes, I have code that redistributes the control points so that their segments are the same length as the Volume Mesh modules SplitLength value. However, when I check the result in the scene view, the length of the rail meshes is bigger than the segment length. This means that the meshes overlap more than one segment.
Is there a way to get the length of the track meshes to be the same as the length of the spline segments, so that each mesh starts at the start of the segment and stops at its end?
So each mesh equals the segment's F0-F1 length and position all along the spline?
Here is the code I am using (it uses part of your Equalize function):
Code:
public void RegulariseCPDistance(CurvySpline theSpline)
{
float splineLength = theSpline.Length;
float numOfSegmentsNeeded;
float numOfControlPointsNeeded;
float numOfActualCPs;
float numOfCPsToAdd;
numOfSegmentsNeeded = Mathf.Ceil(splineLength / rail_Segment_Length);
numOfControlPointsNeeded = numOfSegmentsNeeded + 1.0f;
numOfActualCPs = theSpline.ControlPointCount;
numOfCPsToAdd = numOfControlPointsNeeded - numOfActualCPs;
if (numOfCPsToAdd > 0)
{
int numToAddInt = (int)numOfCPsToAdd;
for (int i = 0; i < numToAddInt; i++)
{
theSpline.Add();
}
}
else if (numOfCPsToAdd < 0)
{
// THIS BIT DOES NOT WORK YET!!!
/*
theSpline.Clear();
int numToAddInt = (int)numOfControlPointsNeeded;
for (int i = 0; i < numToAddInt; i++)
{
theSpline.Add();
}
*/
}
int startCPIndex = Mathf.Clamp(theSpline.GetControlPointIndex(theSpline.ControlPointsList[0]), 0, theSpline.ControlPointCount - 2);
int endCPIndex = Mathf.Clamp(theSpline.GetControlPointIndex(theSpline.ControlPointsList[theSpline.ControlPointCount - 1]), startCPIndex + 2, theSpline.ControlPointCount - 1);
if (endCPIndex - startCPIndex < 2)
{
Debug.Log("CurvySpline.Equalize: Not a valid range selection!");
return;
}
float segmentLength = theSpline.ControlPointsList[endCPIndex].Distance - theSpline.ControlPointsList[startCPIndex].Distance;
float equal = buildVolumeMesh.SplitLength;
float dist = theSpline.ControlPointsList[startCPIndex].Distance;
Vector3[] newCpPositions = new Vector3[endCPIndex - startCPIndex - 1];
for (int i = startCPIndex + 1; i < endCPIndex; i++)
{
int iterationIndex = i - startCPIndex - 1;
newCpPositions[iterationIndex] = theSpline.InterpolateByDistance(dist + (iterationIndex + 1) * equal);
}
for (int i = startCPIndex + 1; i < endCPIndex; i++)
{
int iterationIndex = i - startCPIndex - 1;
theSpline.ControlPointsList[i].SetLocalPosition(newCpPositions[iterationIndex]);
}
theSpline.Refresh();
}