Help with ball

edited April 2015 in Questions about Code

Hello guys , im new to processing and i was working on this program which i want a ball(( gray one )) to run from another ball ((red one)) which is located on the mouse pointer and there is circle which define the boundry for the white ball . the problem that i have is that i tried to use color change to define the boundry but it seems to not working the way i want it . so please take a look at my code and help me if you can .

thx

Arash.

balls myBalls;

PVector p1 ;
PVector p2 ;

void setup() {
  size(800, 800);

  p1 = new PVector(400, 400);
  myBalls = new balls(p1);
}


void draw() {
  background(0);


  p2 = new PVector(mouseX, mouseY);



  myBalls.run(p2);
}




class balls {
  PVector p1;
  PVector force;
  PVector acc;
  PVector velo = new PVector();

  float mass = 1.0;
  float stiffness;

  color loc;
  color nextLoc;



  balls(PVector point1) {
    p1 = point1;
  }



  void run(PVector p2) {
    boundry();
    display();
    string(p2);
  }


  void display() {
    loc = get(int(p1.x), int(p1.y));
    nextLoc = get(int(p1.x + velo.x), int(p1.y + velo.y));
    fill(100);
    stroke(100);
    ellipse(p1.x, p1.y, 10, 10);
  }


  void string(PVector point2) {
    fill(255, 0, 0);
    stroke(255, 0, 0);
    ellipse(p2.x, p2.y, 10, 10);

    stiffness =p1.dist(p2);

    force = PVector.sub(p1, p2);
    force.normalize();
    force = PVector.div(force, stiffness);
    acc = PVector.mult(force, mass);
    velo = PVector.add(velo, acc);
    p1.add(velo);
  }


  void boundry() {
    fill(255);
    stroke(255);
    strokeWeight(1);
    ellipse(400, 400, 700, 700);

    if (loc != -1) {
      velo = PVector.mult(velo, -1);
    }
  }
}
Tagged:

Answers

  • thx @GoToLoop , i was not aware of that :) is it ok now !? if it is plz help me :) , i realy stuck here :((

  • edited April 2015

    void string(PVector point2) you don't use your local point2 variable! And names of your variables are same as global - p1 and p2, not sure if that's good. If you want to know if one ellipse touches another, you can use PVector's dist() function and if it is less than sum of balls radiuses then do whatever you wanted.

  • Hello @Ater , thx for your replay but my problem is not between balls but its that i want to know when the gray ball touches the boundry ellipse (( the big ellipse which gray ball is inside it with 700 radius )) , and then do something.

  • @Arash: I don't have PDE by hand to run your program, but I can't see much difference. The ball will touch boundaries of bigger ball when distance between centres will be bigBallRadius - smallBallRadius, isn't it?

  • @Ater , thats pretty smart solution , but it gonna work this way if boundry is a circle but what if my boundry is a free form curve ?! actually i want to know when a point is inside a free form curve , when it is on it , and when it is outside of it . the code that i posted here is a study to find out if i can use color change for this purpose or not which it seems that is not working : /

  • there is no way to find out if a curve is inside or outside of a closed curve !?!?

  • If the bounds are a circle, using dist() should do the job: if distance from center of bounding circle is beyond the radius of this circle, perhaps minus size of the ball, then it is going out.

  • edited April 2015

    @PhiLho , thx for your comment but the problem is about a free form boundry curve , and i want to know when a point is inside the free form closed curve , when on the curve and when outside of the curve.

  • Then the easier way is to draw this curve as a filled form on a separate PGraphics (if that's only a line in your sketch): one color for the inside, another for the outside. Check the position of the ball (its center, or its border): if the pixel at this position has the color of outside, the ball is out.

  • @PhiLho , can you plz explain more about what do you mean by (( separate PGraphics )) . i tried to do this with color change , ((see the code )) but because the ball has speed some times it cant find the right moment of color change and the code the program kinda get bug .

  • See PGraphics. You use createGraphics to create one, then you draw on it like on regular sketch.
    And the speed issue is why I wrote about filling color: even if you overshot, you will find the external color.

  • @PhiLho , i have a problem with overshot , imagine the ball overshot the border , now the ball is on other color((outside the curve)) so my (( if )) activated and the ball turns around but the next balls position right after the turn is also outside the curve , so again (( if )) activate and ball turn again , so ball stuck outside the shape and it will vibrate forever . correct me if im wrong .

  • Yes, it is a common problem. A possible solution is to force the ball to go back into the shape: take the ball trajectory (straight line, I suppose), walk it back pixel per pixel until you reach an inside pixel, can be the point to put back the ball.

  • hmm , sound like a complex code : / , i will try to do it , @PhiLho , thank you for your help and your time :)

  • Simpler (but can have bad rendering, to see): just go back one unit of move (depending on speed of ball, of course).

Sign In or Register to comment.