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 !



