Vectors in Processing are exactly what they are in real life, a quantity that has both magnitude and direction. In this example we will create the movement mechanics of the famous game Pong using vectors. In Processing, the PVector class is used to describe a two or three dimensional Euclidean vector. It has the following fields –
- x – The x component of the vector
- y – The y component of the vector
- z – The z component of the vector
The PVector class also has several functions for manipulation of vectors, of which we will be using the add()
function for the following program.
Implementation of sketch –
/* SKETCH */ Ball b; // Ball object
void setup()
{ // Called at the beginning once
size( 600 , 400 );
// Initializing the new walker object with radius of the ball
b= new Ball( 20 );
} void draw() // Called every frame
{ background( 0 ); // A black background
b.display(); // Displaying the Ball object
b.move(); // Function to move the Ball
b.bounce(); // Function to bounce the Ball
} |
Now we have to define the Ball class. We have to define two PVectors location and velocity as data members. We initialize them as follows –
PVector location, velocity; Ball( float r)
{ // Initial position is set at the center of the screen
location = new PVector(width/ 2 , height/ 2 );
// Initial velocity set to 2 for in both x and y direction
velocity = new PVector( 2 , 2 );
// Initializing radius
radius=r;
} |
Now we have to move the ball at constant velocity. This can be done using the following code without using any vector function –
void move()
{ location.x += velocity.x;
location.y += velocity.y;
} |
This can also be achieved using the add() function which is a member of the PVector class. Since we want to add the velocity vector to location vector, we write the following code –
void move()
{ // Equivalent way of thinking:
// location += velocity
location.add(velocity);
} |
Finally we need to check, whether the ball has reached the edge of the screen or not and if so, the ball needs to bounce off the edge of the screen. To do so, we just reverse the direction of velocity once the ball reaches edge of the screen. We can do so using the following code –
void bounce()
{ // Code for making the ball bounce of the edges
if ((location.x + radius > width) || (location.x + radius < 0 ))
velocity.x *= - 1 ;
if ((location.y + radius > height) || (location.y + radius < 0 ))
velocity.y *= - 1 ;
} |
Implementation of the Ball class –
/* Implementation of the Ball class */ class Ball
{ PVector location, velocity;
float radius;
Ball( float r)
{
// Initial position is set at the center of the screen
location = new PVector(width/ 2 , height/ 2 );
// Initial velocity set to 2 for in both x and y direction
velocity = new PVector( 2 , 2 );
// Initializing radius
radius=r;
}
void move()
{
// Equivalent way of thinking:
// location += velocity
location.add(velocity);
}
void bounce()
{
// Code for making the ball bounce of the edges
if ((location.x > width) || (location.x < 0 ))
velocity.x *= - 1 ;
if ((location.y > height) || (location.y < 0 ))
velocity.y *= - 1 ;
}
void display()
{
// Code to display the ball
noStroke();
fill( 255 );
ellipse(location.x, location.y, radius * 2 , radius * 2 );
}
} |