Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by alexander11 · Aug 31, 2016 at 06:03 AM · c#unity 53dsplinelinear

How do i make a Linear Spline??

Hello i have been trying to modify this script i have so it is a Linear spline instead of a (Catmull)Bezier like in Pic1 but my results are in Pic2, would anyone know how to do/fix this?

(My Error is obviously in line 29 to 39 the calculation of the spline, i know this question is rather stupid since it uses simple math)

-Pic1 (what i am trying to achieve) alt text -Pic2 (the results i am getting) alt text

Here is my code so you know what i have done. i believe my mistake is in the Vector3 ReturnCatmullRom.

 using UnityEngine;
 using System.Collections.Generic;
 
 public class LinearSpline : MonoBehaviour {
 
     public List<Transform> controlPointsList = new List<Transform>();
 
     public int segCount = 2;
     public bool isLooping = true;
 
     void OnDrawGizmos()
     {
         Gizmos.color = Color.white;
 
         for (int i = 0; i < controlPointsList.Count; i++)
         {
             Gizmos.DrawWireSphere(controlPointsList[i].position, 0.3f);
         }
         for (int i = 0; i < controlPointsList.Count; i++)
         {
             if ((i == 0 || i == controlPointsList.Count - 2 || i == controlPointsList.Count - 1) && !isLooping)
             {
                 continue;
             }
 
             DisplayLinearSpline(i);
         }
     }
     Vector3 ReturnLinearRom(float t, Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3)
     {
         Vector3 a = p0;
         Vector3 b = p1;
         Vector3 c = p2;
         Vector3 d = p3;
 
         Vector3 pos = a + b + c + d;
 
         return pos;
     }
 
     void DisplayLinearSpline(int pos)
     {
         Vector3 p0 = controlPointsList[ClampListPos(pos - 1)].position;
         Vector3 p1 = controlPointsList[pos].position;
         Vector3 p2 = controlPointsList[ClampListPos(pos + 1)].position;
         Vector3 p3 = controlPointsList[ClampListPos(pos + 2)].position;
 
 
         Vector3 lastPos = Vector3.zero;
 
         for (int t = 0; t < segCount; t++)
         {
             float n = (float)t / segCount;
             Vector3 newPos = ReturnLinearRom(n, p0, p1, p2, p3);
 
             if (n == 0)
             {
                 lastPos = newPos;
                 continue;
             }
 
             Gizmos.color = Color.white;
             Gizmos.DrawLine(lastPos, newPos);
             lastPos = newPos;
 
             Gizmos.DrawSphere(newPos, 0.3f);
         }
 
         Gizmos.DrawLine(lastPos, p2);
     }
 
     int ClampListPos(int pos)
     {
         if (pos < 0)
         {
             pos = controlPointsList.Count - 1;
         }
 
         if (pos > controlPointsList.Count)
         {
             pos = 1;
         }
         else if (pos > controlPointsList.Count - 1)
         {
             pos = 0;
         }
 
         return pos;
     }
 }


capture44.png (5.0 kB)
capture43.png (10.4 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
1
Best Answer

Answer by ldeboer · Aug 31, 2016 at 08:59 AM

You realize the original code was drawing a spline out to screen in a loop because it needed to draw little line segments to make the curve so it had to loop. You have no need to do that you are drawing a line between the control points so use Gizmos.DrawLine directly

// Draw line between each control point
for (int i = 0; i < controlPointsList.Count-1; i++) {
    Gizmos.DrawLine(controlPointsList[i].position, controlPointsList[i+1].position);
}
// Finally line between first and last control point
Gizmos.DrawLine(controlPointsList[controlPointsList.Count-1].position, controlPointsList[0].position);
You sort of show the midpoint as well and if you want that then simply add the two control positions together and divide by two.

Comment
Add comment · Show 3 · 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 ldeboer · Aug 31, 2016 at 10:00 AM 0
Share

I should add that as a catmull-rom $$anonymous$$UST PASS THRU any middle points you could also just use your catmull-rom code as it was but set the two middle control points of the spline to the same value of the mid point between the two points or even the point 1/4 and 3/4 of the way along line if you wanted nice even little line segments.

avatar image alexander11 · Aug 31, 2016 at 06:26 PM 0
Share

Ah forgot about that part. anyway Thanks for the help.

But is there a way to do it in a calculation? ins$$anonymous$$d of just drawing the lines to each point.

avatar image ldeboer alexander11 · Sep 01, 2016 at 04:49 AM 0
Share

There is no such thing as a linear spline ... some spline types can be made to draw a line by organizing the control points in a special way. So the is NO CALCULATION called linear spline in the way you are trying to use it as a parametric equation. That is what the loop function inside the catmull-rom code did it blends the control points.

So your options are

  1. Use a spline like catmull-rom and set the control points from the two end control points

  2. Draw a simple line between the points

  3. Recursively divid the line into small sections

For 1 and 3 what you want is points at set distances along the line so lets look at that using you control points from code above

This is the calculation for the point midway between any two of your control points

Transform mid.position =  (controlPointsList[i].position + controlPointList[i+1].position)/2;
Now if you want the point half way between that point and each end, so that would be points 1/4 of the way along and 3/4 of the way along use the same calc but use the above midpoint

So the 1/4 point would be .. half way between first control and the mid point

Transform onequarter.position =  (controlPointsList[i].position + mid.position)/2;
The 3/4 point would be .. half way between second control point and mid point
Transform threequarter.position =  (controlPointsList[i+1].position + mid.position)/2;

So you could modify your loop code to recurse the subdivision process which is called tweening. Generally it would be easier to organize the function in paramateric form going from 0 to 1 to do that so

xt = (x2-x1)*t + x1 where 0 <= t <= 1

In your code it would look like this

double t;
Transform tpos.position;
tpos.x =  (controlPointsList[i+1].position.x -controlPointList[i].position.x)*t + controlPointList[i].position.x;
tpos.y =  (controlPointsList[i+1].position.y -controlPointList[i].position.y)*t + controlPointList[i].position.y;
tpos.z =  (controlPointsList[i+1].position.z -controlPointList[i].position.z)*t + controlPointList[i].position.z;

If you make t = 0.1 .. tpos will be the point 1/10 of the way along the line, t= 0.25 will be the point quarter of the way along the line etc. So you can loop it like your spline code just changing the t value.

OR you use standard catmull-rom spline you already have using these four points

  1. controlPointsList[i].position

  2. onequarter.position

  3. threequarter.position

  4. controlPointsList[i+1].position

The catmull-rom spline is GUARANTEED to be a straight line and the draw segments will be evenly spaced.

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

231 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 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 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 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do i do this type of spline?? 1 Answer

Adding segments in missing spots 1 Answer

How do i create points along a spline??? 0 Answers

Problem with curved Cornered Spline that i need help with 0 Answers

What is Matrix4x4? 2 Answers


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