We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › fix length "line", please help [RESOLVED]
Page Index Toggle Pages: 1
fix length "line", please help [RESOLVED] (Read 1297 times)
fix length "line", please help [RESOLVED]
Jul 14th, 2008, 1:08pm
 
I am trying to make a "string" with a fix length to follow the mouse. But I don't want it to behave like a string, but more like a snake or a train. So the string follows the mouse path.

I experimented with physics/particles libs and making my own thing but it's not really working.

I store the mouse position in a array of x slots, and then draw lines between those positions. It works but I can't have a fix length with this technique. The faster you move the mouse, the longer the snake gets.

Any advises, I really need this fix length?

here is a quick code to show how I am doing it for the moment:
Quote:


float[] X = new float[20];
float[] Y = new float[20];

void setup(){
 size(500, 500);  
}


void draw(){

 background(255);
 smooth();
 fill(0);
 stroke(0);

 X[0] = mouseX;
 Y[0] = mouseY;

 for(int i=X.length-1; i>0; i--){
   if ( i>0){
     X[i] = X[i-1];
     Y[i] = Y[i-1];
   }
 }

 beginShape(LINES);
 for( int i=0; i<X.length; i++){
   if ( i>0){
     float t = map(i, 0, X.length, 10, 2);
     strokeWeight(t);
     vertex(X[i], Y[i]);
     vertex(X[i-1], Y[i-1]);
   }
 }
 endShape();

}



ps: I can't get any answers from the forum. I usually get answer when it's about syntax or libs. Is it to complicated to do?
Re: fix length "line", please help
Reply #1 - Jul 14th, 2008, 10:29pm
 
I answered on the other thread too, but I'll post the code here, since I think this is more appropriate.

Here is a solution for a maximum length. Hope it helps.

Quote:
 float sum = 0.0;
 int maxlen = 100;
 int lastVertex = 1;
 while (lastVertex < X.length && sum < maxlen) {
   sum += dist(X[lastVertex], Y[lastVertex], X[lastVertex-1], Y[lastVertex-1]);
   lastVertex++;
 }
 lastVertex--;
 
 for (int i = X.length-1; i>lastVertex; i--) {
   X[i] = X[lastVertex];
   Y[i] = Y[lastVertex];
 }
 
 for(int i=lastVertex; i>0; i--){
   X[i] = X[i-1];  
   Y[i] = Y[i-1];
 }
Re: fix length "line", please help
Reply #2 - Jul 15th, 2008, 10:21am
 
All right, cheers.
I am going to try that today then.

Thanks
Re: fix length "line", please help
Reply #3 - Jul 15th, 2008, 10:51am
 
You got no answer probably because of lack of code: it is hard to give advices on a concept...
Nice effect, I like it.
Note you make some unnecessary tests. In the first loop, it just won't happen, in the second, just start the loop at 1...

Here is my own take at the issue, inspired by antiplastik's one... (I don't have tried their solution, so I don't know how it differ, if it does.)

static final int V_MAX = 20; // Max nb of vertices
static final float L_MAX = 200.0; // Max length

float[] X = new float[V_MAX];
float[] Y = new float[V_MAX];

void setup() {
 size(500, 500);  
 smooth();
}

void draw() {

 background(255);
 fill(0);
 stroke(0);

 X[0] = mouseX;
 Y[0] = mouseY;

 for (int i = V_MAX-1; i > 0; i--) {
   X[i] = X[i-1];  
   Y[i] = Y[i-1];
 }
 float len = 0.0;
 int lastV = 1;
 for (; lastV < V_MAX; lastV++) {
   float d = dist(X[lastV], Y[lastV], X[lastV - 1], Y[lastV - 1]);
   if (len + d > L_MAX)
     break;
   len += d;
 }

 beginShape(LINES);
 for (int i = 1; i < lastV; i++) {
   float t = map(i, 0, lastV, 10, 2);
   strokeWeight(t);
   vertex(X[i], Y[i]);
   vertex(X[i-1], Y[i-1]);
 }
 endShape();

}
Re: fix length "line", please help
Reply #4 - Jul 15th, 2008, 12:50pm
 
PhiLho  wrote on Jul 15th, 2008, 10:51am:
You got no answer probably because of lack of code: it is hard to give advices on a concept...
Nice effect, I like it.


I know but I have a lack of code because I don't have the solution Smiley

PhiLho  wrote on Jul 15th, 2008, 10:51am:
Note you makes some unnecessary tests. In the first loop, it just won't happen, in the second, just start the loop at 1...


I just realised that few minutes ago Wink

Thanks for the code. It's funny to see different versions of the same thing, how people name their variables, etc...
Re: fix length "line", please help
Reply #5 - Jul 15th, 2008, 1:01pm
 
I usually give longer variable names, but I was lazy there... Tongue
Particularly when I do quick hacks in the PDE, I don't have the facilities (auto-completion, drag'n'drop) I have in SciTE.
Re: fix length "line", please help
Reply #6 - Jul 15th, 2008, 1:17pm
 
Didn't know SciTE. Looks like a "every language" editor like smultron or aptana for net-based stuff.

Thanks again for the modification you made. I couldn't get my hands on the code we made at the beginning for that. I definitely need to organise myself on this game.

I tried it with curveVertex() instead of vertex(). It curves the snake for me, but it sometime jitter...
Re: fix length "line", please help
Reply #7 - Jul 15th, 2008, 5:34pm
 
This is how I was suggesting using the curve() function to remove jaggedness (as mentioned here http://processing.org/discourse/yabb_beta/YaBB.cgi?board=Programs;action=display;num=1215603574;start=0)

the code below is philho's code, I just added the curve() instead of shape/vertex.

also I wasn't sure if you wanted the snake to reduce to a point when the mouse wasn't moving, so I moved some code into mouseMoved, and increased the max vertices so if the mouse is moving very slowly it still has enough buffer to store the positions.

You will notice that with this solution if you move quite fast, then slowly, the tail of the snake will move quite jerkily - because there is a big distance between stored mouse positions. To solve this you would need to use curvePoint() to interpolate and find inbetween points for the stored mouse positions. This is not too complex, but complex enough for me to not have time to write it out right now Tongue
(P.S. you could also do a physical version, where your snake is a fixed number of segments and just move that along - quite a different approach, but might work better)

Code:

static final int V_MAX = 200; // Max nb of vertices
static final float L_MAX = 200.0; // Max length

float[] X = new float[V_MAX];
float[] Y = new float[V_MAX];
int lastV;

void setup() {
size(500, 500);
smooth();
}

void draw() {

background(255);
fill(0);
stroke(0);

for (int i = 2; i < lastV-1; i++) {
float t = map(i, 0, lastV, 10, 2);
strokeWeight(t);
curve(X[i-2], Y[i-2], X[i-1], Y[i-1], X[i], Y[i], X[i+1], Y[i+1]);
}
}

void mouseMoved() {
X[0] = mouseX;
Y[0] = mouseY;

for (int i = V_MAX-1; i > 0; i--) {
X[i] = X[i-1];
Y[i] = Y[i-1];
}

float len = 0.0;

for (lastV = 1; lastV < V_MAX; lastV++) {
float d = dist(X[lastV], Y[lastV], X[lastV - 1], Y[lastV - 1]);
if (len + d > L_MAX)
break;
len += d;
}

}


Re: fix length "line", please help
Reply #8 - Jul 15th, 2008, 6:13pm
 
All Right.
Cheers for doing this memo.
Looks good but the jitter is a bit annoying Tongue

Your second solution was the one that we were trying to do but we didn't have any success. I coding isn't that good.

We are trying with curveVertex and some tan function to draw the snake. But know we have to find a way to rewrite our collision function Sad and that going to be really @*&ing hard.

CURVE VERTEX:

Quote:


static final int V_MAX = 20; // Max nb of vertices
static final float L_MAX = 500.0; // Max length

float[] X = new float[V_MAX];
float[] Y = new float[V_MAX];
float[] V = new float[V_MAX];
float[] W = new float[V_MAX];

float len;
int lastV;
float detail, weight = 20;

int type;

void setup() {
 size(500, 500);  
}

void draw() {
 smooth();
 background(255);
 fill(0);
 stroke(0);

 X[0] = mouseX;
 Y[0] = mouseY;

 if ( mouseX != pmouseX || mouseY != pmouseY){
   for (int i = V_MAX-1; i > 0; i--) {
     X[i] = X[i-1];  
     Y[i] = Y[i-1];
   }
   len = 0.0;
   lastV = 1;
   for (; lastV < V_MAX; lastV++) {
     float d = dist(X[lastV], Y[lastV], X[lastV - 1], Y[lastV - 1]);
     if (len + d > L_MAX)
       break;
     len += d;
   }
 }


 for (int i = 1; i < lastV-1; i++) {
   println(i);
   if (i==2 || i==lastV-3){
     weight = 0;
   }
   else{
     weight = 10;
   }
   V[i] = thicknessX(X[i-1], X[i], X[i+1], Y[i-1], Y[i], Y[i+1], weight);
   W[i] = thicknessW(X[i-1], X[i], X[i+1], Y[i-1], Y[i], Y[i+1], weight);

 }
 // println(W);
 noFill();
 curveTightness(0);
 beginShape();
 for (int i = 1; i < lastV-1; i++) {
   strokeWeight(1);
   curveVertex(X[i], Y[i]);
 }
 endShape();

 beginShape();
 for (int i = 1; i < lastV-1; i++) {
   strokeWeight(1);
   curveVertex(V[i], W[i]);
 }
 endShape();

}
/*The parrallel line fuctions*/

float thicknessX(float Xa, float Xb, float Xc, float Ya, float Yb, float Yc, float h){
 float T = atan2(Ya-Yc, Xa-Xc);
 float V = Xb+h*cos(T+HALF_PI);
 return V;
}
float thicknessW(float Xa, float Xb, float Xc, float Ya, float Yb, float Yc, float h){
 float T = atan2(Ya-Yc, Xa-Xc);
 float W = Yb+h*sin(T+HALF_PI);
 return W;

}




the weight variable is here to adjust the thickness of the snake.
I have to leave the office now, but I will be back tomorrow morning and I will look at the forum this evening.

Thanks a lot to everyone, it just helps lot to have different point of view or people reinforcing your idea.
Re: fix length "line", please help
Reply #9 - Jul 15th, 2008, 6:57pm
 
Hi TM, i'm not sure what you mean by jitter? I'm not getting any jitter on a Mac Pro or Macbook Pro...
Re: fix length "line", please help
Reply #10 - Jul 15th, 2008, 10:43pm
 
Oups, sorry. My english is not as good as I though. I meant 'jerkily' no jitter. Your code works like you said it does.

Smiley
Re: fix length "line", please help
Reply #11 - Jul 16th, 2008, 4:51pm
 
Our curVertex() solution might be a dead end. We are going to follow the curvePoint solution. Which was our first idea, but we were using vertex() and lerp().

If you have a minute and you don't know what to do, I would love to see a fix number of fixLenght segments following the mouse ;P . Which like I said, I couldn't work out how to code. I tried atan2 for the orientation but couldn't get something satisfying enough.

Thanks a lot again for the help. I will post our code when done Smiley
Re: fix length "line", please help
Reply #12 - Jul 16th, 2008, 5:50pm
 
Ok, so we are using your curve tech,  we won't bother with the curvePoint() as in fact curve doesn't make more jerks than our old vertex tech. We realized that curve and curveVertex are both using the Catmull-Rom formula. I don't know why I was convince that curve was using Bezier. Silly me.

We found out that the guy who invented it he's Pixar's Boss and one of the guy behind the ToyStories, etc...
http://en.wikipedia.org/wiki/Edwin_Catmull

Thanks a lot to you two, PhiLho and memo. That really help us a lot.
I really love this forum, a real good and positive spirit. I will post a link here and in the exhibition when will be doing public beta testing.

thanks again ( I mark this subject as resolved )
Re: fix length "line", please help [RESO
Reply #13 - Jul 16th, 2008, 10:57pm
 
Glad you solved your issue. And you are welcome.

PS.: Je vois que tu es palois ! Peut-être d'adoption... Je suis orthézien ! Amusant.
Re: fix length "line", please help [RESO
Reply #14 - Jul 17th, 2008, 10:18am
 
En fait je suis Toulousain Smiley
J'etais a Pau pour mes 3 premieres annees au Beauzart. C'est marrant. Merci encore pour ton aide.
Page Index Toggle Pages: 1