Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Wisearn · Apr 14, 2014 at 01:39 PM · smoothpathalgorithmcurvespline

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: alt text

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!!!

spline.jpg (114.5 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Wisearn · Apr 14, 2014 at 02:09 PM 0
Share

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.

avatar image robertbu · Apr 14, 2014 at 02:21 PM 0
Share

You might also take a look at a simple Laplacian smoothing for the line.

avatar image Wisearn · Apr 14, 2014 at 02:57 PM 0
Share

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.

avatar image robertbu · Apr 14, 2014 at 03:21 PM 1
Share

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;
     }
 }
avatar image robertbu · Apr 15, 2014 at 04:21 AM 0
Share

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.

alt text

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

importing spline curves from maya to unity? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges