Using Vectors in Processing Language


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 –



filter_none

edit
close

play_arrow

link
brightness_4
code

/* 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
}

chevron_right


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 –

filter_none

edit
close

play_arrow

link
brightness_4
code

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;
}

chevron_right


Now we have to move the ball at constant velocity. This can be done using the following code without using any vector function –

filter_none

edit
close

play_arrow

link
brightness_4
code

void move()
{
    location.x += velocity.x;
    location.y += velocity.y;
}

chevron_right


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 –

filter_none

edit
close

play_arrow

link
brightness_4
code

void move()
{
    // Equivalent way of thinking:
    // location += velocity
    location.add(velocity);
}

chevron_right


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 –

filter_none

edit
close

play_arrow

link
brightness_4
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;
}

chevron_right


Implementation of the Ball class –

filter_none

edit
close

play_arrow

link
brightness_4
code

/* 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);
    }
}

chevron_right


This article is contributed by Soumik Rakshit. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.