- Home /
Smoothing a path
I have a game where I let the user draw out a path using his mouse, so I get a list of points (I have both versions that save new points per time and per distance).
But the vehicle that follows them I'm having problem with it looking natural, as if the driver doesnt know the full path.
From what I understand is that I need to make a spline, but the problem with every method I've tried so far is this:
The path goes way off course when it enters at a steep angle and exits at a steep angle. I drew a green line that would be more appropriate, and I circled with red the problem I'm trying to describe.
This is why I can't use AnimationCurve and I have not yet found a spline or path smoothing algorithm or function that does not have this problem.
Has anyone dabbled with this? What is the best practice in the described case? Where can I find a solution? What are some possible alternatives to recieving the effect I'm looking for?
Thanks a lot in advance!!!
Answer by robertbu · Apr 14, 2014 at 01:56 PM
I don't know what it will do to the rest of your points, but you could try a level or two of Chaikin smoothing before passing it to a spline algorithm.
http://graphics.cs.ucdavis.edu/education/CAGDNotes/Chaikins-Algorithm/Chaikins-Algorithm.html
If I let the user draw purely by himself it would be too detailed and the vehicle would be driving with the much too responsive and shaky input of a mouse, but now I can let the user draw out more logical paths and then we detail the path by the Chaikins Algorythm ins$$anonymous$$d of a vector spline, as the movement is already natural by its own navigeering system Chaikins will make the path itself "natural enough".
I'll get back here once I've tested it out, currently stuck trying to figure out how to put it into practice as a C# function, if anyone can help out with this that'd be awesome, otherwise I'll post the solution here once I have it.
You might also take a look at a simple Laplacian smoothing for the line.
I'm not sure if Laplacian would be suitable as it would stray from the position of the (logified by Nth distance) points placed by the user, which is something I'm trying to avoid, ins$$anonymous$$d just make them more approchable and "using" the exact points in the creation of a more sensible path to actually follow them.
Here is an example class for Chaikin Smoothing. Attach to an empty game object. Hitting 'G' will generate a new random path. Each time you hit space, the path will be smoothed.
using UnityEngine;
using System.Collections;
[RequireComponent (typeof (LineRenderer))]
public class Example : $$anonymous$$onoBehaviour {
private float $$anonymous$$Radius = 1.5f;
private float maxRadius = 3.5f;
private int num = 25;
private LineRenderer lr;
private Vector3[] points;
void Start() {
lr = GetComponent<LineRenderer>();
GenerateRandomPath();
SetLR(points);
}
void GenerateRandomPath() {
points = new Vector3[num];
float angle = 360.0f / num;
for (int i = 0; i < num; i++) {
points[i] = Quaternion.AngleAxis ((angle * i), Vector3.forward) * Vector3.up * Random.Range($$anonymous$$Radius, maxRadius);
}
}
void SetLR(Vector3[] pts) {
lr.SetVertexCount (pts.Length);
for (int i = 0; i < pts.Length; i++) {
lr.SetPosition (i, pts[i]);
}
}
void Update() {
if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.Space)) {
points = Chaikin(points);
SetLR(points);
}
if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.G)) {
GenerateRandomPath();
SetLR(points);
}
}
Vector3[] Chaikin(Vector3[] pts) {
Vector3[] newPts = new Vector3[(pts.Length - 2) * 2 + 2];
newPts[0] = pts[0];
newPts[newPts.Length-1] = pts[pts.Length-1];
int j = 1;
for (int i = 0; i < pts.Length - 2; i++) {
newPts[j] = pts[i] + (pts[i+1] - pts[i]) * 0.75f;
newPts[j+1] = pts[i+1] + (pts[i+2] - pts[i+1]) * 0.25f;
j += 2;
}
return newPts;
}
}
Here are four screen shots from the code above to give anyone looking at this question an idea Chaikin smoothing. The images are the original path and then with 1, 2, and 3 levels of smoothing.
Your answer
Follow this Question
Related Questions
Moving rigidbody along spline, and being able to use physics 1 Answer
How can I make a Lerp move in an arc instead of a straight line? 2 Answers
how do I add abilities(Scripts/GameObjects) to my path finding agent 1 Answer
Orientation along spline path / Mesh extrusion along path, problem with smooth orientation 1 Answer