OBJECT ORIENTED PROGRAMMING
Ishan, interested in planetary science, sought help from his father to visually represent his ideas about the solar system. His father helped him visualise his ideas. Can we also assist him?
This project emphasizes the learning of Object Oriented Programming and p5js in which we will build a web that showcases the planets in our solar system.
https://kids.nationalgeographic.com/
When we click on the button, we will see a 3D model of a dog in augmented reality that we can zoom in, zoom out, or rotate to observe from any angle.
For example, if we search for a ‘dog’ or any animal in the Google search on a smartphone, we can see an option, ‘View in 3D’.
Programming and Real World
In this lesson, we will create a web platform using p5js that will display planets in our solar system.
We will use a 3D platform and also include interactive elements to enhance the user experience.
A rough user Interface
Open p5js
One of the planets nearest to the Earth is ‘Mars’, also known as the ‘Red Planet’. It is mostly reddish-orange in colour.
Step 1: Open the p5js editor
Step 1: Change the ‘canvas size’ to ‘500x500 pixels’ and the ‘background colour’ to ‘black’.
Draw mars
Syntax: createCanvas(width, height, WEBGL)
For adding a planet, we can use a ‘sphere’ shape. sphere() – This command draws a sphere with the given radius. Add a sphere instead of a circle using the ‘sphere()’ command.
Step 2: Add the keyword ‘WEBGL’ in the ‘createCanvas()’ method as a third parameter. Add a comma before the ‘WEBGL’ keyword.
Draw mars
Draw mars
Step 1: Upload the texture image provided in the resources to the ‘Sketch Files’ section.
Add Texture
This command sets the current material as a normal material. It is not affected by light.
This command sets the input image or video as the texture to render subsequent shapes.
Step 2: Add a new preload function that will load the image into a variable at the beginning of code Execution.
Now we can see that the texture image is wrapped over the sphere to make it look realistic.
Add Texture
Step 1: Use the ‘rotateY()’ method with ‘millis()/100’ as an input. Add the ‘angleMode(DEGREES)’ command in setup to consider the input values in degrees instead of radians.
We can see that Mars is rotating around its own axis. This looks realistic and engaging compared to any 2D image.
Add Rotation to 3D Object
Display Information
Planet Name | Diameter (km) | Number of moons | Gravity (m/s2) | Length of a day |
Mercury | 4,879 | 0 | 3.7 | 59 days |
Venus | 12,104 | 0 | 8.9 | 243 days |
Earth | 12,760 | 1 | 9.8 | 24 hours |
Mars | 6,792 | 2 | 3.7 | 24 hrs 37 min |
Jupiter | 1,42,984 | 92 | 23.1 | 9 hr 55 min |
Saturn | 1,20,536 | 146 | 9.0 | 10 hr 40 min |
Uranus | 51,118 | 27 | 8.7 | 16 hr 50 min |
Neptune | 49,528 | 14 | 11.0 | 6 hr 11 min |
Create variables for displaying information
Step 1: Create new variables ‘name’ and ‘dia’ to store the name and the diameter of the planet, respectively.
‘name’ as “Mars” and ‘dia’ - “6,792 km”.
Create Variables
Step 1: Download the ‘Kanit-Bold.tff’ file from the resources folder shared earlier using QR code.
Upload the file in the p5js editor sketch files.
Add Information Text
Now, we will start adding the code to display text after the code is added to display Mars in 3D.
Var names = [“Mars”,“Earth”,“Jupiter”];
var dia = [“6792 km”,“12,760 km”,“1,42,984 km”]
What is OOP?
Example
Object – An Object is a unique data entity that can contain the properties and methods. Objects can be created independently, similar to defining a variable, or they can be created from a class. If the object is created using a class, then it is termed an ‘instance of a class’.
Example
A ‘car’ is a real-life Object, which has some characteristics like colour, type, and model, and performs certain actions like driving.
What properties can we identify of the planet in the code we created earlier?
Object Literal
Following are the syntax rules:
var mars = {
imgPath : “mars.jpg”,
name : “Mars”,
dia : “6,787 km”,
};
‘mars.imgPath’.
Implement the concept of an object into code.
Step 1: Comment out or remove the variable declaration and initialization in the setup.
Create mars object
Note that we had created the ‘marsImg’ variable to store the texture image of the mars.
Add image object property
Update the code with the object properties
Update the ‘marsImg’ variable in the rest of the code with the ‘mars.img’ object property in the ‘texture()’ command.
Also, update the variables ‘name’ and ‘dia’ with the object properties ‘mars.name’ and ‘mars.dia’ in the text command, respectively
Now we can see the same result as earlier, with a rotating Mars sphere and basic information displayed at the bottom.
We can also add and display more properties from the table.
Update the code
To add more planets, we need to create multiple objects.
For example, to add the Earth we can create a new object as we did earlier. Instead of creating multiple objects, we can create an array of objects.
Array of Objects: Just like a normal data array, we can create an array of objects. Each object in an array is identified by an index number that represents its position in the array. In arrays, the first element index in the array is [0], the second element index is [1], and so on.
We can create an array by adding objects in place of data separated by commas. In this activity, we can create an array of objects for planets.
For example, ‘planets[1].name’ will have value as ‘Earth’. We can check other properties using the console.
Step 1: Add an ‘Earth’ texture image from the resources in the Sketch Files.
Add multiple Planets
Adding texture image for all objects in the array
Step 2: Comment out the code to add a texture image for Mars in the ‘preload()’ function.
Add multiple Planets
Now, we have data and image files that can display multiple planets.
However, at a time, we can show only one planet with the proper size on the screen.
In this case, we can use a key to display the next planet and its information in the same position as a carousel.
Create counter for planet to be displayed
Step 3: Declare a variable ‘count’ at the beginning and initialize it to ‘0’ in the setup.
Add multiple Planets
Add functionality to change the planet
Step 4: Update the object as ‘planets[count]’ from ‘Mars’ throughout the code i.e., in texture and text commands.
Now, based on the value of the ‘count’ variable, the planet from the array will be displayed.
Add multiple Planets
Step 1: Add the ‘if ()’ statement to check the condition > ‘count < 0’. If the condition is ‘true’, set the ‘count’ value as the index of the last object in the array i.e., (planets.length – 1).
This way, we will have a cyclic counter. If the first planet is displayed and the ‘left arrow’ key is pressed, the last planet will be displayed on the array list.
Circular Switching through array
With this code, the entire application is ready. We can now use arrow keys to go through the planets shown in 3D with their details. This type of interactive 3D application is the future of Digital Encyclopedia.
Update the code to include more planets and celestial objects with their detailed information data.