Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Math question about interpolation

Discussion in 'General Discussion' started by deLord, Jan 14, 2015.

  1. deLord

    deLord

    Joined:
    Oct 11, 2014
    Posts:
    306
    Hi

    I don't know if it is too late already or if I am too stupid. Somehow, I wanted to write my own interpolation code for moving objects. The idea is to have a starting point, an end point and a waypoint. Now this waypoint shall be interpolated so that start = start, end = end and half way through the waypoint is either tangented for a split second or not (I wanna make this configurable). So imagine a -y = x² curve where (0/0) is my waypoint to be tangented or "missed" by like 5% of the way between start (somewhere on (-1/-1)) and wp and (1/-1) being end.

    My idea is to Lerp to a vector which is t% vector wp and 1-t% vector end where t=percentual time it takes to move. My method so far is this:
    Code (CSharp):
    1.     public IEnumerator moveWithinTime(Vector3 wp, Vector3 target, float timeToMove){
    2.         Vector3 currentPos = gameObject.transform.position;
    3.         wp += originalPos;
    4.         target += originalPos;
    5.         float t = 0f;
    6.         while(t < 1) {
    7.             t += Time.deltaTime / timeToMove;
    8.             Vector3 vTemp = new Vector3();
    9.             float factor = 1-t;
    10.             vTemp.x = wp.x * factor + target.x * (1-factor);
    11.             vTemp.y = wp.y * factor + target.y * (1-factor);
    12.             vTemp.z = wp.z * factor + target.z * (1-factor);
    13.             gameObject.transform.position = Vector3.Lerp(currentPos, vTemp, t);
    14.             yield return null;
    15.         }
    16.     }
    But the result is not what I want (way too flat). Also tried all sorts of Pow(), t-1, 1-t, etc. But I think my main idea of composing a vector like this may be wrong. Maybe I missed sth and I can even use existing functions, but I couldn't understand how to use SmoothDamp() so I decided to "quickly" write my own function. Good thing I only wasted roughly 2h on that ...
     
  2. ShilohGames

    ShilohGames

    Joined:
    Mar 24, 2014
    Posts:
    2,980
    Try using Slerp instead of Lerp.
     
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Sounds like a job for a spline.
     
    angrypenguin likes this.
  4. boolfone

    boolfone

    Joined:
    Oct 2, 2014
    Posts:
    289
    Can you give an example of a specific object that it might make sense to move in this fashion?

    What you are describing sounds somewhat similar to a quadratic Bezier curve:

    http://en.wikipedia.org/wiki/Bézier_curve
     
  5. deLord

    deLord

    Joined:
    Oct 11, 2014
    Posts:
    306
    As an example I might have a magic spell that moves in a specific way. Imagine a wizard summoning a growing power ball that moves from (0/2/0) to (0/1/0) but shortly before reaching (0/1/0) moves quickly to the location of the opponent, e.g. (5/1/1).

    @BoredMormon what does this tell me? Are there existing functions for splines?

    @ShilohGames can you post an example of how this would work?
     
  6. R-Lindsay

    R-Lindsay

    Joined:
    Aug 9, 2014
    Posts:
    287
    If you want to move something along a curved path, you are after bezier curves as mentioned. If you want to specify the points the curve should pass though and have something calculate the curve automatically, you are after splines.

    If you want to specify how thing move along your path or curve (e.g. slow at first, or slow at the end, etc) you are probably interested in easing functions.
     
    angrypenguin likes this.
  7. deLord

    deLord

    Joined:
    Oct 11, 2014
    Posts:
    306
    My waypoints are just very few, you maybe I am not talking a bout a curved path,
    splines sound like the ones I am searching for,
    I can achieve the easing myself, as my script already includes a time it takes to go the way.

    So it seems I need to delve more into splines. Is there something ready-to-use already?
     
  8. chelnok

    chelnok

    Joined:
    Jul 2, 2012
    Posts:
    680
    I needed something similar and found one: http://wiki.unity3d.com/index.php?title=Spline_Controller ..how ever it looked like too complicated for my case, and i ended up using AnimationCurve.

    I would probably do this with two steps: use animation curve or perhaps Animation itself for fancy movement after summoning, and then just move towards target.

    try this test script to get idea how to use animation curve for this kind of movement:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class AniCurveTest : MonoBehaviour {
    5.  
    6.     public AnimationCurve aniCurve;
    7.  
    8.     [Range(0f,1f)]
    9.     public float normalizedTime;
    10.     public float scale =1f;
    11.  
    12.     public float curveValue;
    13.  
    14.     float curveLenghtInSeconds;
    15.     Vector3 tempVector =new Vector3();
    16.  
    17.     void Update () {
    18.         Keyframe lastframe= aniCurve[aniCurve.length -1];
    19.         curveLenghtInSeconds = lastframe.time;
    20.  
    21.         float t = normalizedTime *curveLenghtInSeconds;
    22.         curveValue = aniCurve.Evaluate(t) *scale;
    23.  
    24.         tempVector.y = curveValue;
    25.         transform.position = tempVector;
    26.     }
    27. }
    Add the script to cube, edit curve in inspector, hit play, use normalizedTime slider in inspector to see curveValue changinh. Script is for testing different curves at runtime, and you can tweak curve and scale and use curveValue for whatever you wish. I just added it to transform.position.y to get some visual representation.