How can adjust the directions?

edited March 2018 in Questions about Code

Hi everybody, I am new to processing language programming, I have an example of bouncing the ball and when executing the program the system gave me (sometimes) wrong directions as output: Ex. if xmove = 1and ymove =1the default direction = south_East

the code :

// initial position for our circle

float circle_x = 300;

float circle_y = 20;

// how much to move the circle on each frame float move_x = 2;

float move_y = -2;

void setup() {

size(400, 200);

stroke(#D60DFF);

strokeWeight(7);

} void draw() {

background(#21EA73);

ellipse(circle_x, circle_y, 40, 40);

circle_x = circle_x + move_x;

circle_y = circle_y + move_y;

if(circle_x > width) {

circle_x = width;

move_x = -move_x;

println(" north_east");

}

if(circle_y > height) {

circle_y = height;

move_y = -move_y;

println(" south_west");

} if(circle_x < 0) {

circle_x = 0;

move_x = -move_x;

println(" north_west");

} if(circle_y < 0) {

circle_y = 0;

move_y = -move_y;

println(" south_east");

} }

Tagged:

Answers

  • edited March 2018 Answer ✓

    you need to learn how to post code in the forum

    independent of that you can use ctrl-t in processing to get auto-format

    Your question

    if xmove = 1and ymove =1the default direction = south_East

    I think, it's because of your println. E.g. if (circle_x > width) you assume println(" north_east");

    Now the east part is correct because we just set move_x = -move_x; BUT the north part can be also south because the ball can at the moment fly north or south. It's whether move_y is at the moment positive or negative.

    To get precise println use a new if clause independent of the reflections (like if (circle_x > width)).

    In this new if clause you would analyse move_x and move_y.

    Example below

    Chrisir ;-)

    // initial position for our circle
    float circle_x = 300;
    float circle_y = 20;
    
    // how much to move the circle on each frame 
    float move_x = 2;
    float move_y = -2;
    
    void setup() {
      size(400, 200);
      stroke(#D60DFF);
      strokeWeight(7);
    } 
    
    void draw() {
    
      background(#21EA73);
    
      ellipse(circle_x, circle_y, 40, 40);   // display
    
      circle_x = circle_x + move_x;  // move 
      circle_y = circle_y + move_y;
    
      if (circle_x > width) {   // boundaries 
        circle_x = width;
        move_x = -move_x;
        println(" north_east");
      }
    
      if (circle_y > height) {
        circle_y = height;
        move_y = -move_y;
        println(" south_west");
      }
    
      if (circle_x < 0) {
        circle_x = 0;
        move_x = -move_x;
        println(" north_west");
      }
    
      if (circle_y < 0) {
        circle_y = 0;
        move_y = -move_y;
        println(" south_east");
      }
    
      String direction="";   //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
      if (move_y < 0)
        direction= "north ";
      else  direction= "south ";
    
      if (move_x < 0)  
        direction+= "west";
      else  direction+= "east";
    
      text(direction, 18, 18);
    }
    
  • Thanks for advising

Sign In or Register to comment.