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 › suggestions
Page Index Toggle Pages: 1
suggestions? (Read 661 times)
suggestions?
Apr 1st, 2006, 8:13pm
 
i'm writing some simple code to make a box move to a random point in the window in a smooth motion. i would like the box to point in that direction, but the code i have added for rotation seems to make the whole thing act rather wonky... i have hashed out the offending code. any suggestions to correct this, i'm stuck.

float xpos;                   //current position on the x axis
float ypos;                   //current position on the y axis
float xrest = random(10, 380);//new random destination on the x axis
float yrest = random(10, 380);//new random destination on the y axis
float speed = 90.0;           //sxp. slew rate (speed)

boolean trig = false;         //state of key press

void setup()

{
 size(400, 400);             //size of window
 background(255);            //initial background color
}

void draw()

{
 background(255);            //background color

 float xdist = xrest - xpos; //xdist is distance between xpos and xrest
 if(abs(xdist) > 1){         //test if xdist is greater than one pixel
   xpos = xpos + xdist/speed;//if it is greater, move closer to xrest at a rate defined by speed
 }
 float ydist = yrest - ypos; //ydist is distance between ypos and yrest
 if(abs(ydist) > 1){         //test if yrest is greater than one pixel
   ypos = ypos + ydist/speed;//if it is greater, move closer to yrest at a rate defined by speed
 }
 if(trig == true){           //test to see if trig variable = true
   xrest = random(10, 380);  //if true, define a new x axis destination...
   yrest = random(10, 380);  //and a new y axis destination
   noLoop();                 //and stop the draw loop
 }
 else{                       //if false...
   xrest = xrest;            //destination x does not change
   yrest = yrest;            //nor does destination y
 }
 //===================================================  
 //float angle = atan2(ydist, xdist);
 //rotate(angle * 180 / PI + 90);
 //===================================================  
 stroke(0);                  //square color
 rect(xpos, ypos, 10, 10);   //draw the square at current x, y, variables.
}

void keyPressed()

{
 if(keyPressed == true){     //test to see if any key is pressed
   trig = true;              //if pressed, change trig variable to true
 }
}

void keyReleased()

{
 if(trig == true){           //test to see if any key is released
   trig = false;             //if released, change trig variable to false
   loop();                   //and start the draw loop
 }
}
Re: suggestions?
Reply #1 - Apr 1st, 2006, 8:20pm
 
Why are you doing lots of unnecessary maths on the result of the atan2?

The result of atan2 is in radians, and rotate takes radians as it's input... so you're converting to degrees, adding 90' to it, then sending that to rotate, which is expecting radians.

To make it work as I think you want it to work, just use:
Code:
float angle = atan2(ydist, xdist);
rotate(angle+HALF_PI); //HALF_PI == 90degrees


Re: suggestions?
Reply #2 - Apr 1st, 2006, 10:20pm
 
hmmm. originally, i used all that math because i was trying to port an old flash program of mine, i guess i really didn't think that much about the differences....

i tried entering in your new amendment, but this doesn't seem to do the trick either. most times, nothing is displayed on the screen at all??? i'm puzzled

new sketch as follows:

float xpos;                   //current position on the x axis
float ypos;                   //current position on the y axis
float xrest = random(10, 380);//new random destination on the x axis
float yrest = random(10, 380);//new random destination on the y axis
float speed = 90.0;           //sxp. slew rate (speed)

boolean trig = false;         //state of key press

void setup()

{
 size(400, 400);             //size of window
 background(255);            //initial background color
}

void draw()

{
 background(255);            //background color

 float xdist = xrest - xpos; //xdist is distance between xpos and xrest
 if(abs(xdist) > 1){         //test if xdist is greater than one pixel
   xpos = xpos + xdist/speed;//if it is greater, move closer to xrest at a rate defined by speed
 }
 float ydist = yrest - ypos; //ydist is distance between ypos and yrest
 if(abs(ydist) > 1){         //test if yrest is greater than one pixel
   ypos = ypos + ydist/speed;//if it is greater, move closer to yrest at a rate defined by speed
 }
 if(trig == true){           //test to see if trig variable = true
   xrest = random(10, 380);  //if true, define a new x axis destination...
   yrest = random(10, 380);  //and a new y axis destination
   noLoop();                 //and stop the draw loop
 }
 else{                       //if false...
   xrest = xrest;            //destination x does not change
   yrest = yrest;            //nor does destination y
 }
 float angle = atan2(ydist, xdist);
 rotate(angle + HALF_PI + 90);
 stroke(0);                  //square color
 rect(xpos, ypos, 10, 10);   //draw the square at current x, y, variables.
}

void keyPressed()

{
 if(keyPressed == true){     //test to see if any key is pressed
   trig = true;              //if pressed, change trig variable to true
 }
}

void keyReleased()

{
 if(trig == true){           //test to see if any key is released
   trig = false;             //if released, change trig variable to false
   loop();                   //and start the draw loop
 }
}

Re: suggestions?
Reply #3 - Apr 1st, 2006, 11:00pm
 
I've managed to make a version that seems to work.

Unfortunately, you've misunderstood a couple of Processing-isms causing some problems.

You were doing a rotate, which rotates the entire screen, making xpos and ypos not where you'd think they should be.

For example if you rotate by 20 degrees, then what would be "right" in terms of an xpos value, becomes "right and down a bit" and ypos becomes "down and left a bit".

To solve that problem, you shoudl do a translate(xpos,ypos) to move the centre of any rotation to the point you want to draw, then the rotate, then draw a square around that new point.

Also, calling noLoop() and loop() is bad ju-ju, if noLoop() has happened, keyPressed() stops being looked at I think, and so once it's been caled once, your sketch is over. If you want to stop drawing for this frame, you just call "return;" which stops the draw() loop at that point for the frame (and subsequent ones if your boolean is true).

The "else" part fo your "if(trig)" was fairly pointless, in that it changed nothing. as was the "if(keyPressed)" in "void keyPressed()". If the function keyPressed is being run, that means a key must be pressed.

Anyway, here's a version of the program that does what it should, baring probbaly not working quite how you want if a key is pressed and held.
Quote:
float xpos;    //current position on the x axis
float ypos;    //current position on the y axis
float xrest;//new random destination on the x axis
float yrest;//new random destination on the y axis
float speed;//sxp. slew rate (speed)

boolean trig = false;    //state of key press

void setup()

{
 size(400, 400);   //size of window
 background(255);  //initial background color
 //moved assignment into setup() just in case...
 xpos=0;
 ypos=0;
 xrest=random(10,380);
 yrest=random(10,380);
 speed=90;
 keyp=false;
}

void draw()

{
 background(255);  //background color

 float xdist = xrest - xpos;
 if(abs(xdist) > 1)
 {    
   xpos = xpos + xdist/speed;
 }
 float ydist = yrest - ypos;
 if(abs(ydist) > 1){    
   ypos = ypos + ydist/speed;
 }
 if(trig == true){      
   xrest = random(10, 380);
   yrest = random(10, 380);
//    noLoop();      
   return; // Maybe does what you want.
 }
/*  else
 {        
   xrest = xrest;  //destination x does not change
   yrest = yrest;  //nor does destination y
 }*/ //This whole block does nothing.. so is useless.
 
 float angle = atan2(ydist, xdist);
 translate(xpos,ypos); // move 0,0 to in reality be xpos,ypos.
 rotate(angle + HALF_PI); //Doesn't need the 90, and now rotates the screen to make the box rotate.
 stroke(0);   //square color
 rect(-5, -5, 10, 10);   //draw a 10 by 10 square, centered around the current "0,0" point.
}

void keyPressed()
{
  //no key is currently pressed.
//  if(keyPressed == true){     //test to see if any key is pressed
   trig = true;    //if pressed, change trig variable to true
//  }
}

void keyReleased()
{
 if(trig == true){      //test to see if any key is released
   trig = false;   //if released, change trig variable to false
//    loop();    //and start the draw loop
 }
}



Re: suggestions?
Reply #4 - Apr 2nd, 2006, 1:22am
 
john, if i wanted to make these into an object and replicate them, would this cause a problem considering that the whole screen is rotating? perhaps i do not understand this aspect of processing at all.
Re: suggestions?
Reply #5 - Apr 2nd, 2006, 1:55am
 
pushMatrix(), popMatrix() and translate will help.
Code:

Spin [] spin;
void setup(){
size(400,400);
spin = new Spin[100];
for(int i = 0; i < spin.length; i++){
spin[i] = new Spin();
}
smooth();
rectMode(CENTER);
}
void draw(){
background(255);
for(int i = 0; i < spin.length; i++){
spin[i].draw();
}
}
class Spin{
float x, y, rotation;
Spin(){
x = random(width);
y = random(height);
rotation = random(TWO_PI);
}
void draw(){
rotation = (rotation + 0.1) % TWO_PI;
pushMatrix();
translate(x,y);
rotate(rotation);
rect(0, 0, 20, 20);
popMatrix();
}
}
Re: suggestions?
Reply #6 - Apr 14th, 2006, 8:14am
 
ok... got a bit further, however, i can't figure out why my little bugs are technically eating before they reach their food... i thought this line:
   if(((abs(xdist) < 1) && (abs(ydist) < 1)) && (full == false)){
would take care of it... can someone tell me what i've missed?

==============================================

import processing.opengl.*;


Bug b1;
Bug b2;
Bug b3;

float xpos = random(10, 390);
float ypos = 0;
float speed = .1;

void setup()

{
 size(400, 400, OPENGL);
 framerate(30);
 b1 = new Bug(xpos, ypos);
 b2 = new Bug(xpos, ypos);
 b3 = new Bug(xpos, ypos);
}

void draw()

{
 smooth();
 background(0);
 b1.move(xpos, ypos);
 b2.move(xpos, ypos);
 b3.move(xpos, ypos);

 rectMode(CENTER);
 rect(xpos, ypos, 5, 5);
 ypos = ypos + speed;
 if(ypos > 397){
   speed = 0;
 }
}
class Bug
{
 float xpos;
 float ypos;
 float xrest;
 float yrest;
 float xrestf;
 float yrestf;
 float speed;
 float ate;
 float xdist;
 float ydist;

 boolean full;

 Bug(float one, float two){
   xpos = random(10, 390);
   ypos = random(10, 390);
   xrest = one;
   yrest = two;
   xrestf = random(10, 390);
   yrestf = random(10, 390);
   speed = 10.0;
   ate = 2;
   full = false;
 }

 void move(float posX, float posY)
 {
   if(full == true){
     xrest = xrestf;
     yrest = yrestf;
     ate -= .1;
   }
   if(((abs(xdist) < 1) && (abs(ydist) < 1)) && (full == false)){
     xrest = posX;
     yrest = posY;
     ate += .1;
     if(ate > 8){
       full = true;
       xrestf = random(10, 390);
       yrestf = random(10, 390);
     }
   }
   if(ate < 2){
     full = false;
   }
   float xdist = xrest - xpos;
   if(abs(xdist) > 1){
     xpos = xpos + xdist / speed;
   }
   float ydist = yrest - ypos;
   if(abs(ydist) > 1){
     ypos = ypos + ydist / speed;
   }

   float angle = atan2(ydist, xdist);
   pushMatrix();
   translate(xpos, ypos);
   rotate(angle + HALF_PI);
   stroke(255);
   noFill();
   rectMode(CENTER);
   rect(0, 15, 10, 14);
   line(0, 0, 0, 10);
   rectMode(CORNER);
   rect(-3, 10, 6, ate);
   popMatrix();
 }
}


Re: suggestions?
Reply #7 - Apr 14th, 2006, 5:54pm
 
ahhh!!!! got it... just a goof in the order.

  float xdist = xrest - xpos;
   if(abs(xdist) > 1){
     xpos += xdist / speed;
   }
   float ydist = yrest - ypos;
   if(abs(ydist) > 1){
     ypos += ydist / speed;
   }
    if(full == true){
     xrest = xrestf;
     yrest = yrestf;
     ate -= .1;
   }
   if(full == false){
     xrest = posX;
     yrest = posY;
     if((abs(xdist) < 2) && (abs(ydist) < 2)){
       ate += .2;
       if(ate > 8){
         full = true;
         xrestf = random(10, 390);
         yrestf = random(10, 390);
       }
     }
   }
   if(ate < 2){
     ate = 2;
     full = false;
   }

works like a charm now
Page Index Toggle Pages: 1