Create a box randomly flying away, then randomly flying back to the oringinal position.

edited February 2018 in Questions about Code

Hi Guys~!

I've created a box can randomly flying away, but I want it randomly flying back when it outside the screen. Could you help me~?


float x= 50;
float y= 50;
float m1;
float m2;


float xspeed =1;
float yspeed =1;

void setup() {
  size(700, 700);
 
  frameRate(60);
}

void draw() {
   m1 =  random(3);
   m2 =  random(3);
  background(255);

  //1st rect
  x = x+xspeed;
  y = y+yspeed;


  fill(0);
    if (mousePressed && (mouseButton == LEFT)) { //make random movement
      xspeed = xspeed + m1*random(-1, 1);
      yspeed = yspeed + m2*random(-1, 1);
    }
  
    if (mousePressed && (mouseButton == RIGHT)) { // stop movement
      xspeed = 0;
      yspeed = 0;

    }


  strokeWeight(0.2);
  stroke(100);
  rect(x, y, 10, 10);
  println(x);
}

Thank you very much!!

Answers

  • https://forum.processing.org/two/discussion/15473/readme-how-to-format-code-and-text

    Forum regulars tend to ignore your code unless it is formatted properly.

  • Thanks TfGuy44, I edite the code and repost it. ;;)

  • float x= 50;
    float y= 50;
    float m1;
    float m2;
    
    
    float xspeed =1;
    float yspeed =1;
    
    void setup() {
      size(700, 700);
      rectMode(CENTER);
    }
    
    void draw() {
      m1 =  random(3);
      m2 =  random(3);
      background(255);
    
      //1st rect
      x = x+xspeed;
      y = y+yspeed;
    
    
      fill(0);
      if (mousePressed && (mouseButton == LEFT)) { //make random movement
        xspeed = xspeed + m1*random(-1, 1);
        yspeed = yspeed + m2*random(-1, 1);
      }
    
      if (mousePressed && (mouseButton == RIGHT)) { // stop movement
        xspeed = 0;
        yspeed = 0;
      }
      if ( x < -10 || x > width + 10 || y < -10 || y > height + 10 ) {
        reset();
      }
      strokeWeight(0.2);
      stroke(100);
      rect(x, y, 10, 10);
      println(x);
    }
    
    void reset() {
      switch(int(random(4))) {
      case 0:
        y = -5;
        yspeed = random(0, 10);
        x = random(width);
        xspeed = random(-10, 10);
        break;
      case 1:
        y = height+5;
        yspeed = random(-10, 0);
        x = random(width);
        xspeed = random(-10, 10);
        break;
      case 2:
        x = -5;
        xspeed = random(0, 10);
        y = random(height);
        yspeed = random(-10, 10);
        break;
      case 3:
        x = width+5;
        xspeed = random(0, 10);
        y = random(height);
        yspeed = random(-10, 10);
        break;
      }
    }
    
  • HI TfGuy44, Thanks! but I think I didn't explain my question very well, my English is poor. Let me organize the question again.

    I create the little black box, and when I press the mouse, it will make random movement, until it outside the screen, but I want the black box randomly fly back to its original position which is x=50; y = 50; when it outside the screen, and then stop there. You can ignore the mouse press function when it flying back :D

  • Answer ✓

    Alright. Instead of giving it a new random position and direction when it reaches the edge, you want to give it a very specific direction instead - one that aims it back to the starting point. You know where it is at that moment ((x,y)). You know where it should be going to ((50,50)). So what's the specific direction it needs to go in? (A diagram would help! Draw one! You might need to use PVectors...)

    How will you detect when it is back at the starting point? Will it be an exact position? What should happen when it is "close enough" to the starting point?

    How are you going to remember if it is moving in a random direction or heading back to the starting point? What shouldn't happen if it is returning?

    It might help immensely if you write out the step your sketch should take in plain words, and then translate that process into code. Attempt this yourself first, and post the code - or plain words - of your attempt for more help.

  • Alright! Thanks TfGuy44! I will try this first by myself, if I have any question I will ask you again!

  • There is another way of approaching this.

    1. You have a fixed point, 50,50
    2. On click, pick a random point outside the screen -- for example, a point on the circumference of a large circle.
    3. Over time, use interpolation to find a point on the path between those two points. This can be done by using lerp, or by using PVector.lerp
Sign In or Register to comment.