A ball moving between points; overlapping linear and cosinusoidal movement.
I was cleaning up some old code and sharing a minimal version; I wrote some of the functions as an exercise.
// GLV
// 2019-11-3
// Plots a trajectory between 2 points.
// Linear speed and cosinusoidal modulation of speed.
PVector m1, m2, p1, p2, mid;
PVector v1, v2, v3;
PVector [] plot = new PVector[10000];
float theta;
float rad = 20;
boolean update = false;
boolean save = false;
boolean toggle = false;
int inc;
public void settings()
{
size(900, 400, P3D);
}
public void setup()
{
ortho();
v1 = new PVector(0, 0, 0);
v2 = new PVector(0, 0, 0);
textSize(36);
textAlign(CENTER);
background(0);
}
public void draw()
{
surface.setTitle("fps: "+ frameRate);
background(0);
lights();
if (frameCount < 4*60)
{
fill(255, 255, 0);
text("Please wait patiently...", width/2, height/2);
return;
}
pushMatrix();
translate(width/2, height/2, 0);
if (toggle)
{
v1.set(-300, 0, 0);
v2.set(+300, 0, 0);
plot2(2, v1, v2);
}
else
{
v1.set(+300, 0, 0);
v2.set(-300, 0, 0);
plot2(2, v1, v2);
}
popMatrix();
int steps = 200;
inc += 1;
step = inc*1.0/steps;
theta = inc*(TAU/2)/steps;
if (inc >= steps)
{
inc = 0;
step = 0;
theta = 0;
update = true;
toggle = !toggle;
}
if (save)
{
saveFrame("frame//###.tga");
}
}
public void mousePressed()
{
save = !save;
noCursor();
}
//****************************************************************************************************
float count = 0;
float step = 0;
public void plot2(int dir, PVector p1, PVector p2)
{
fill(255, 0, 0);
sphere1(p1);
fill(255, 0, 0);
sphere1(p2);
float l = dist(p1.x, p1.y, p1.z, p2.x, p2.y, p2.z);
//modulated cosinusoidal
if (true)
{
count = 1-(0.5*cos(theta) + 0.5);
m1 = midPoints(dir, count, p1, p2);
stroke(255, 255, 0);
strokeWeight(2);
line(p1.x, p1.y, p1.z, m1.x, m1.y, m1.z);
fill(255, 255, 0);
sphere1(m1);
print(count, " ");
}
//linear
if(true)
{
count = step;
m2 = midPoints(dir, count, p1, p2);
stroke(0, 255, 0);
strokeWeight(2);
line(p1.x, p1.y, p1.z, m2.x, m2.y, m2.z);
fill(0, 255, 0);
sphere1(m2);
println(count);
}
}
void sphere1(PVector v)
{
noStroke();
pushMatrix();
translate(v.x, v.y, v.z);
sphere(rad);
popMatrix();
}
//****************************************************************************************************
public float lenPoints(PVector p1, PVector p2)
{
float len = 0;
len = sqrt((p1.x-p2.x)*(p1.x-p2.x) + (p1.y-p2.y)*(p1.y-p2.y) + (p1.z-p2.z)*(p1.z-p2.z));
return len;
}
//****************************************************************************************************
public PVector midPoints(int a, float r, PVector p1, PVector p2)
{
mid = new PVector(p1.x, p1.y, p1.z);
float d;
float n;
d = 1/r;
n = d-1;
//midpoint
if (a==0)
{
mid.x = (p1.x+p2.x)/2;
mid.y = (p1.y+p2.y)/2;
mid.z = (p1.z+p2.z)/2;
}
//forward
else if (a==1)
{
mid.x = (p1.x+n*p2.x)/d;
mid.y = (p1.y+n*p2.y)/d;
mid.z = (p1.z+n*p2.z)/d;
}
//reverse
else if (a==2)
{
mid.x = (n*p1.x+p2.x)/d;
mid.y = (n*p1.y+p2.y)/d;
mid.z = (n*p1.z+p2.z)/d;
}
return mid;
}
Original post: