Using the Processing Language found at http://www.processing.org

Continuing from here

Some words about oop

Object Oriented Programming (oop) allows you to organise your code as the objects are in real life.

Our ball code has

1/ lines to calculate the values

2/ lines to make it bounce / check the edges

3/ lines to dispaly the ball

We’ll create something called a class, which is a plan for the ball

We’ll put the characteristics of the ball organised in what looks like functions called methods

Up to that point it’s only definitions, we’ll then call the above in our main program.

 

See our ball code :

PVector location;
PVector velocity;
PVector acceleration;

void setup(){
size(400,400);
location=new PVector(200,height);
velocity = new PVector(0,0);
acceleration=new PVector(0,0);
}

void draw(){
background(0);
location.add(velocity);
velocity.add(acceleration);

acceleration.add(0,0.001);
velocity.limit(5);

fill(255);
ellipse(location.x,location.y,30,30);

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

}

.

After organising into classes :

.

class Ball{
  
  PVector location;
  PVector velocity;
  PVector acceleration;

  Ball(){
  location=new PVector(200,height);
  velocity = new PVector(0,0);
  acceleration = new PVector(0,0);
  
  }
  
void update(){
location.add(velocity);
velocity.add(acceleration);

acceleration=new PVector(0,1);
velocity.limit(15);
}

void display(){
fill(255);
ellipse(location.x,location.y,30,30);
}

void checkEdges(){
if (location.x < 0 || location.x >width ){
velocity.x *= -1;
}
if (location.y < 0 || location.y >height ){
velocity.y *= -1;
}
}

}

 And then we write our setup and draw :

Ball b;

void setup(){
size(400,400);
b = new Ball();
}

void draw(){
background(0);


b.update();
b.display();
b.checkEdges();


}

much cleaner in the main program !

//////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\

\\\\\\\\\\\\\\\\\\\\\\\\\\//////////////////////////

Explanations

1…

class Ball {

}

//1 classes name normally starts with a Capital letter

//2 notice no brackets after Ball

2 …

You then declare global variables at the top, just as in the normal way

PVector location;
PVector velocity;
PVector acceleration;

3…

  Ball(){
  location=new PVector(200,height);
  velocity = new PVector(0,0);
  acceleration = new PVector(0,0);
  
  }

//1 notice the () after Ball

//2 this is called the constructor

//3 you put in it whatever you want to happen when we create the ball

//4 just like functions. you can pass in arguments to constructors

4…

void update(){
location.add(velocity);
velocity.add(acceleration);

acceleration=new PVector(0,1);
velocity.limit(15);
}

//1 this is called a method

//2 it organises codes doing specific functions together

//3 this one is the values calculation part

5…

void display(){
fill(255);
ellipse(location.x,location.y,30,30);
}

//1 this one occupies with the displaying of the ball

6…

void checkEdges(){
if (location.x < 0 || location.x >width ){
velocity.x *= -1;
}
if (location.y < 0 || location.y >height ){
velocity.y *= -1;
}
}

//1 this one occupies with bounce logic

7…

//calling starts

Ball b;

//1 we are declaring the ball

//2 just like PVector w

8…

b = new Ball();

//1 creating the ball

//2 the code in the constructor gets called

//3 if you had any parameters you can pass it here

//4 just like w = new PVector() which tells that PVector is actually a class !

9…

b.update();
b.display();
b.checkEdges();

//1 calling the three methods,

Classes in files

You can also make a new tab and give it the name of your class and put your code in it, same thing !

tab.png

new tab

That is also why processing programs run only in folders as classes can be stored as files.

Last words

The display method should be called last as then, your display will be updated as the display is based on calculations

 

 

 

 

 

 

Advertisements