Nature of Code - creating a boundary around the screen that repels a ball

edited February 2015 in Questions about Code

I'm going through Nature Of Code at the moment and this exercise from Chapter 2 :

So, I have the ball and stuff and I've made a boarder, but I can't seem to get anything working properly for a boundary that repels the ball. I'm gradually descending into an atrocious piece of code without any real solution.

I created a method called 'ping' that took as it's input a pixel size which would represent the border that would repel the ball object.

This was set to 100, so when the ball got to 100 pixels away from the edge of the screen it should start to be repelled by this ping method.

What I do either doesn't work or it gradually makes the ball shoot out faster and faster, because the ping method is increasing the balls acceleration in an opposite but greater amount to when it came in... Which keeps happening, hence the speed up.

I thought that maybe I could map the border size to the value of the incoming acceleration and only effect it by that amount, but I didn't get anywhere with that.

I've currently tried to make it so that the ping method will only affect the value of the acceleration while it's less than the opposite of what it came in as. So if it came into the border with an acceleration value of 4, it could only leave with a value of -4, r at least the ping would only work to that point.

That didn't really work though, and I'm getting kind of lost with it now.

Here's the code, sorry it's a bit urjhj

cheers :)

void setup() {
  size(350, 350);
  b = new Balloon();

Balloon b;

float nx, ny, x, y;

void draw() {

  float dev = 0.1;
  x = random(-noise(nx)*dev, noise(nx)*dev);
  y = random(-noise(ny)*dev, noise(ny)*dev);
  PVector helium = new PVector(random(-dev, dev), random(-dev, dev));



  helium.x = 0.0;
  helium.y = 0.0;

class Balloon {

  PVector acceleration;
  PVector location;
  PVector velocity;
  float mass = 1;
  float ping;

  Balloon() {
    location = new PVector(width/2, height/2);
    acceleration = new PVector(0, 0);
    velocity = new PVector(0, 0);

  void display() {
    fill(200, 10, 60);
    ellipse(location.x, location.y, 40, 40);
  void update() {
  void applyForce(PVector force) {
    PVector f = PVector.div(force, mass);

  void ping(float b) {
    float p = 0.02;
    PVector ca = new PVector(acceleration.x, acceleration.y);
    PVector push = PVector.div(velocity, b);
    if (location.x < b) { //negative initial
        acceleration.x += p;
    } else if (location.x > width -b) { 
        acceleration.x -= p;
    if (location.y < b) { //negative initial 
        acceleration.y += p;
    } else if (location.y > height-b) { // positive initial 
        acceleration.y -= p;
  void checkEdges() {
    if (location.y < 0) {
      location.y = 0;
      velocity.y *= -1;
    } else if (location.y > height) {
      location.y = height;
      velocity.y *= -1;

    if (location.x > width) {
      location.x = width;
      velocity.x *= -1;
    } else if (location.x < 0) {
      location.x = 0;
      velocity.x *= -1;

// ----------------------------------------------

void drawBoarder() {
  int size = 100;
  fill(255, 30);
  rect(0, 0, size, height);
  rect(0, 0, width, size);
  rect(width-size, 0, width, height);
  rect(0, height-size, width, height-size);


  • float ang = random(TWO_PI);
    PVector p = new PVector(200,200,0);
    PVector v = new PVector(2*cos(ang),2*sin(ang),1);
    PVector a = new PVector(0,0,0);
    void setup(){
    void draw(){
      if(p.x<40) a.x=0.1;
      if(p.x>width-40) a.x=-0.1;
      if(p.y<40) a.y=0.1;
      if(p.y>height-40) a.y=-0.1;
      v.x = constrain(v.x,-2,2);
      v.y = constrain(v.y,-2,2);
Sign In or Register to comment.