Okay, the line I’ve drawn is shown in white.
A green ball follows the line.
But why is the big green ball still there in this case?
I don’t think this is true. He’s recording throughout - or what do you mean?
not sure, but measure can’t be < 1 AND > 1 at the same time. Hence you mean measure==1
?
movementInterpolated
not clear. Maybe you can communicate what your final goal is?
You are referring to
movementInterpolated=map (y, 0, 400, 0, TWO_PI);
(it’s TWO_PI, not TWO)
(always nice when you tell us the code line
you are referring to )
here is what you can do with movementInterpolated
:
// see y as 0 to TWO_PI
movementInterpolated=map (y,
0, 400,
0, TWO_PI);
// show 2 circles at right screen border
circle( width-11, movementInterpolated+55, 12); // minimal movement
circle( width-11, movementInterpolated*100+255, 12); // movement enhanced by *100
// calc circle pos x1,y1 with movementInterpolated as an angle
float x1=cos(movementInterpolated) * 100 + (width-120);
float y1=sin(movementInterpolated) * 100 + (255);
fill(0, 0, 255);
circle(x1, y1, 9); // show point on circle with movementInterpolated as angle
circle((width-120), 255, 9); // show center of circle
Remark
By the way in processing the order is (mostly (it’s only by convention))
- global vars and objects
- setup and draw
- other functions
- classes
It is unwise to name a method in a class draw(), just use display() instead
Full Sketch
int actualSec, lastSec, measure, measureToStartRecording;
boolean bRecording = false;
boolean mouseRecorded = true;
int nbBalls=1;
float movementInterpolated;
Sampler sampler;
// ------------------------------------------------------------------------------------------------------
// Two core functions
void setup() {
size( 800, 800, P3D );
frameRate( 30 );
sampler = new Sampler();
}//setup()
void draw() {
background( 0 );
activeSampling();
stopSampling();
if (actualSec!=lastSec) {
lastSec=actualSec;
measure++;
}
textSize(100);
text(measure, 100, 100 );
actualSec = (int) (millis()*0.001); //
if (bRecording) {
// draw circle
circle(mouseX, mouseY, 10 );
sampler.addSample( mouseX, mouseY );
} else {
if (sampler.fullTime() > 0 )
sampler.display();
}
drawBall(1, movementInterpolated);
}//draw()
// ------------------------------------------------------------------------------------------------------
// Inputs
void mousePressed() {
bRecording = true; // draw circle
mouseRecorded = true;
measure=0;
}
// ------------------------------------------------------------------------------------------------------
// other functions
void activeSampling() {
if (measure<=1 && measure>=1 &&actualSec!=lastSec && mouseRecorded == true) {
sampler.beginRecording();
println ("here 18");
}
}
void stopSampling() {
if (measure<=3 && measure>=3 && actualSec!=lastSec) {
mouseRecorded = false;
//**REPEAT
bRecording = false;
sampler.beginPlaying();
}
}
void drawBall(int n, float phase) {
pushMatrix();
translate(-400, -400, -2000);
noStroke();
float side = height*0.15*1/this.nbBalls;
float rayon = width/2;
float x = rayon*cos(phase);
float y = rayon*sin(phase);
translate (x, y, 200+(50*5*n));
colorMode(RGB, 255, 255, 255);
fill( 0, 255, 0 );
sphere(side*3);
popMatrix();
}
// ====================================================================
// Two classes
class Sample {
int t, x, y;
//constr
Sample( int t, int x, int y ) {
this.t = t;
this.x = x;
this.y = y;
}//constr
//
}//class
// ----
class Sampler {
ArrayList<Sample> samples;
ArrayList<Sample> samplesModified;
int startTime;
int playbackFrame;
//constr
Sampler() {
samples = new ArrayList<Sample>();
samplesModified = new ArrayList<Sample>();
startTime = 0;
}//constr
void beginRecording() {
samples = new ArrayList<Sample>();
samplesModified = new ArrayList<Sample>();
playbackFrame = 0;
}
void addSample( int x, int y ) { // add sample when bRecording
int now = millis();
if ( samples.size() == 0 ) startTime = now;
samples.add( new Sample( now - startTime, x, y ) );
}
int fullTime() {
return
samples.size() > 1 ?
samples.get( samples.size()-1 ).t : 0;
}
void beginPlaying() {
// called only once
println("here 12");
startTime = millis();
playbackFrame = 0;
println( samples.size(), "samples over", fullTime(), "milliseconds" );
if (samples.size() > 0) {
int deltax = samples.get(0).x - samples.get(samples.size()-1).x;
int deltay = samples.get(0).y - samples.get(samples.size()-1).y;
float sumdist = 0;
for (int i = 0; i < samples.size() - 1; i++) {
sumdist += sqrt((samples.get(i).x - samples.get(i +1 ).x)*(samples.get(i).x - samples.get(i +1 ).x) + (samples.get(i).y - samples.get(i +1 ).y)*(samples.get(i).y - samples.get(i +1 ).y));
}
samplesModified.add( new Sample(samples.get(0).t, samples.get(0).x, samples.get(0).y ) );
float dist = 0;
for (int i = 0; i < samples.size() - 1; i++) {
dist += sqrt((samples.get(i).x - samples.get(i +1 ).x)*(samples.get(i).x - samples.get(i +1 ).x) + (samples.get(i).y - samples.get(i +1 ).y)*(samples.get(i).y - samples.get(i +1 ).y));
samplesModified.add( new Sample(samples.get(i+1).t, (int) (samples.get(i +1).x + (dist * deltax) / sumdist), (int) (samples.get(i+1).y +( dist * deltay )/ sumdist)) );
print(samples.get(i).x);
print(",");
print(samples.get(i).y);
print(",");
print( " good data x " + samplesModified.get(i).x);
print(",");
print( " good data y " + samplesModified.get(i).y);
println("");
}
}
}
void display() {
stroke( 255 );
//**RECORD
beginShape(LINES);
for ( int i=1; i<samples.size(); i++) {
vertex( samplesModified.get(i-1).x, samplesModified.get(i-1).y ); // replace vertex with Pvector
vertex( samplesModified.get(i).x, samplesModified.get(i).y );
}
endShape();
//**ENDRECORD
//**REPEAT
int now = (millis() - startTime) % fullTime();
if ( now < samplesModified.get( playbackFrame ).t ) playbackFrame = 0;
while ( samplesModified.get( playbackFrame+1).t < now )
playbackFrame = (playbackFrame+1) % (samples.size()-1);
Sample s0 = samplesModified.get( playbackFrame );
Sample s1 = samplesModified.get( playbackFrame+1 );
float t0 = s0.t;
float t1 = s1.t;
float dt = (now - t0) / (t1 - t0);
float x = lerp( s0.x, s1.x, dt );
float y = lerp( s0.y, s1.y, dt );
circle( x, y, 10 );
println( " good data y " + y);
// see y as 0 to TWO_PI
movementInterpolated=map (y,
0, 400,
0, TWO_PI);
// show 2 circles at right screen border
circle( width-11, movementInterpolated+55, 12); // minimal movement
circle( width-11, movementInterpolated*100+255, 12); // movement enhanced by *100
// calc circle pos x1,y1 with movementInterpolated as an angle
float x1=cos(movementInterpolated) * 100 + (width-120);
float y1=sin(movementInterpolated) * 100 + (255);
fill(0, 0, 255);
circle(x1, y1, 9); // show point on circle with movementInterpolated as angle
circle((width-120), 255, 9); // show center of circle
//
}//method
//
}//class
//