[ALMOST SOLVED] Sine Wave Between 2 points - Close [I think] but need help

Trying to build a sine wave between 2 points…
I think I might be close, but it still eludes me.

I couldn’t Google-up anything in C#, but I did find AS3 and Processing takes on the matter.
The ActionScript was inscrutable.
The Processing version understandable, but am unsure how to convert to Unity.

void sineTo(PVector p1, PVector p2, float freq, float amp)
{
// #######################  NOT C# !!!!
  float d = PVector.dist(p1,p2);
  float a = atan2(p2.y-p1.y,p2.x-p1.x);
  noFill();
  pushMatrix(); 
    translate(p1.x,p1.y);
    rotate(a); 
    beginShape();
      for (float i = 0; i <= d; i += 1) {
        vertex(i,sin(i*TWO_PI*freq/d)*amp);
      }
    endShape();
  popMatrix();
}

Hmmm… pushMatrix? translate? rotate? The best I could come up with was:

  public Transform Origin;
    public Transform Terminus;

    public GameObject GO;

    public float Frequency = 4;
    public float Amplitude = 0.25f;


    protected void Start()
    {
        SineTo(Origin.position, Terminus.position, Frequency, Amplitude);
    }

    void SineTo(Vector2 p1, Vector2 p2, float freq, float amp)
    {
        float d = Vector2.Distance(p1, p2);
        float a = Mathf.Atan2(p2.y - p1.y, p2.x - p1.x);

        Vector2 nv = new Vector2(Mathf.Cos(a), Mathf.Sin(a));

        for (int i = 0; i <= d; i += 1)
        {
            Instantiate(GO, (nv * i) + p1, Quaternion.identity);
        }

    }

Which gives me a nice tangent from p1 to p2…

But how to make the sine wave?

I know it’s (i, Mathf.Sin(i))… But applying the tangent vector has me stymied.

Any math wizzes about about to help me out?
Thanks !

is this what you want? or should the distance be the frequency?

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Example : MonoBehaviour {

    public Transform origin;
    public Transform terminus;
    public GameObject GO;
    public float amplitude = 10f;
    public float frequency = 10f;

    private List<GameObject> objects = new List<GameObject>();
    protected void Start()
    {
        SineTo(origin.position, terminus.position, amplitude, frequency);
    }
    void SineTo(Vector2 p1, Vector2 p2, float amp, float freq)
    {
        for(int i=objects.Count-1; i>= 0; i--)
            Destroy(objects[i]);
        objects.Clear();
        float d = Vector2.Distance(p1, p2);
        for (int i = 0; i <= d; i ++)
        {
            float f = freq * i;
            Vector2 nv = new Vector2(i, amp * Mathf.Sin(f*Mathf.Deg2Rad));
            GameObject clone = (GameObject)Instantiate(GO, (nv) + p1, Quaternion.identity);
            objects.Add(clone);
        }
    }
    void Update()
    {
        if(Input.GetKeyDown(KeyCode.Space))
            SineTo(origin.position, terminus.position, amplitude, frequency);
    }
}

added the space to recalculate if you move one of the objects

Hi Hi
Thanks for giving it a try.
Your therom creates a sine wave which extends as far as the X position of p2 [more or less], but the sine wave does not approach p2

2889995--212453--NoTan.png

Here is the example from Processing:

Thanks for taking the time.

Hmmm…
Had I had more of a head for numbers, I would have realized sooner that there exists no formulae for plotting a sine wave away from -1 | 1

The answer was to rotate the wave using the the radian attained through Atan2

But I really did enjoy these past 2 days learning all about Trigonometry :wink:

Here’s my answer:
For the sake of brevity, I didn’t bother implementing the amplitude nor frequency variables.

  void SineTo(Vector2 p1, Vector2 p2, float freq, float amp)
    {
        float d = Vector2.Distance(p1, p2);
        float a = Mathf.Atan2(p2.y - p1.y, p2.x - p1.x);

        var c = Instantiate(new GameObject("Container"));

        for (float i = 0; i <= d; i += 1)
        {

            var g = Instantiate(GO, new Vector3(p1.x + i, Mathf.Sin(i)), Quaternion.identity);
            g.transform.parent = c.transform;
        }

        c.transform.Rotate(Vector3.forward, a * Mathf.Rad2Deg);

    }

###Edit#

Upon further consideration, I do realize that this is a clumsy solution derived from pure ignorance, and that a more elegant mathmatical means must exist, but sometimes you just want to go to sleep…

Okay… I ditched the hack-y container and instead use Quaternion rotation …
Only problem is I’m rotating around World Space.
How to rotate around the pivot [p1] ?

    void SineTo(Vector2 p1, Vector2 p2, float freq, float amp)
    {
        float direction = Vector2.Distance(p1, p2);
        float radian = Mathf.Atan2(p2.y - p1.y, p2.x - p1.x);

        for (float i = 0; i <= direction; i += 1)
        {
            Instantiate(GO, GetVectorAngle(new Vector3(p1.x + i, p1.y + Mathf.Sin(i)), radian, p1), Quaternion.identity);
        }
    }


    Vector3 GetVectorAngle(Vector3 v3, float radian, Vector3 pivot)
    {
        v3 = Quaternion.AngleAxis(radian * Mathf.Rad2Deg, Vector3.forward) * v3;
        return v3;
    }