p5.js Coding Tutorial | Optical Illusion with Sound 🎵

Patt Vira
19 Apr 202428:32

Summary

TLDRIn this coding tutorial, viewers are guided through creating an interactive animation featuring circles oscillating around a center point, accompanied by sound. The tutorial leverages object-oriented programming, incorporating trigonometry for the movement and sound playback when circles pass through the center. It progresses from a single circle to multiple rings, synchronizing visual motion with a soundtrack generated from preloaded MP3 files. The final touch includes adjusting the animation's aesthetics, such as background color and circle transparency, to enhance the visual experience.

Takeaways

  • 🎨 The tutorial focuses on creating an interactive animation with sound using object-oriented programming in JavaScript.
  • 🔄 It builds upon a previous tutorial where circles oscillate around a central point, adding complexity to the existing concept.
  • 🎵 The new feature involves circles playing a note when they pass through a center point, enhancing the visual experience with auditory elements.
  • 📊 The tutorial demonstrates the use of trigonometry to create circular motion, specifically using the equations x = r * cos(angle) and y = r * sin(angle).
  • 🖥️ The origin point of the canvas is translated to the center to facilitate drawing and animation, making the coding process more intuitive.
  • 🎚️ The angle of the circles' movement is mapped to the mouse's x-location, creating an interactive element where user input influences the animation.
  • 🔁 The tutorial introduces the concept of 'push' and 'pop' functions in transformations, essential for managing the canvas state during animations.
  • 🎹 Sound is integrated into the animation by preloading MP3 files and playing them based on the circles' position, creating a dynamic and engaging user experience.
  • 🔢 The tutorial addresses potential issues like buffer zones and modulo operations to ensure smooth playback and prevent errors when the number of circles exceeds available sound files.
  • 🌐 It concludes with tips on adjusting parameters like buffer size and angle velocity to fine-tune the animation and sound synchronization for different numbers of circles and sound files.

Q & A

  • What is the main focus of today's coding tutorial?

    -The main focus of the tutorial is to create an interesting soundtrack by experimenting with sound and movement, specifically by animating circles that play notes as they pass through a center point.

  • What programming concept is used in this tutorial to avoid code duplication?

    -The tutorial uses object-oriented programming and specifically classes to avoid code duplication, encapsulating the behavior and properties of the moving circles within a 'Ball' class.

  • How does the tutorial achieve the oscillating motion of the circles?

    -The oscillating motion is achieved by using trigonometric equations to calculate the x and y coordinates of the circles and adjusting their positions relative to a central point on the canvas.

  • What is the purpose of the 'translate' function used in the tutorial?

    -The 'translate' function is used to change the origin point for drawing, moving it from the top-left corner to the center of the canvas, which facilitates drawing circles around a central point.

  • How does the tutorial synchronize the visual movement of circles with sound?

    -The tutorial synchronizes the visual movement with sound by playing a note each time a circle passes through the center point, which is detected using a collision detection method within the 'Ball' class.

  • What is the significance of the 'angle' and 'shifting angle' variables in the code?

    -The 'angle' variable determines the current position of the circle along its path, while the 'shifting angle' variable offsets this position to create multiple circles that move in phase but start at different points, contributing to the complexity of the animation and sound pattern.

  • How does the tutorial handle the preloading of multiple sound files?

    -The tutorial handles the preloading of multiple sound files by using a for loop within the 'preload' function to load each sound file into an array called 'sound', with each file's name dynamically constructed using the 'assets' array.

  • What is the role of the 'push' and 'pop' functions in the transformation of the circles' positions?

    -The 'push' and 'pop' functions save and restore the transformation state of the canvas, allowing for multiple rotations to be applied without affecting subsequent drawings, which is crucial for creating the illusion of circular motion.

  • How does the tutorial adjust the sound playback to accommodate a varying number of circles?

    -The tutorial adjusts sound playback by using the modulo operator to循环播放音符,确保即使圆圈的数量超过音文件的数量,也能够循环使用现有的音文件,避免出现索引越界的错误。

  • What visual effects are applied to enhance the final presentation of the animation?

    -The tutorial applies a black background with a transparency effect, sets the strokes of the circles to white with a transparency of 50%, and adjusts the circles' size to create a visually appealing final presentation.

Outlines

00:00

🎵 Introduction to Sound and Movement Programming

The tutorial begins by introducing a coding project that combines sound and movement to create an intriguing soundtrack. The presenter references a previous video where circles oscillate around a central point and announces a faster-paced approach using object-oriented programming. The aim is to enhance the previous project by having circles play notes as they pass through a center point and to create multiple rings of circles for a richer audiovisual effect. The first step is to set up the animation by drawing a central circle on the canvas, translating the origin point to the center, and then drawing another smaller circle that will move along a circular path using trigonometric equations.

05:01

🔄 Creating Oscillating Motion with Trigonometry

The paragraph explains how to create an oscillating motion for the circles using trigonometry. The presenter sets the angle to zero and uses the cosine function to determine the x-coordinate of the moving circle. The y-coordinate is manipulated to create the illusion of oscillation. The tutorial then progresses to setting up the mouse-controlled angle, which dictates the position of the circle along the path. The presenter also demonstrates how to create additional circles that follow the x and y coordinates of the primary circle, giving the impression of circular motion. The importance of using 'push' and 'pop' functions for transformations is highlighted to ensure that the transformations do not interfere with one another.

10:03

🎚️ Refactoring Code with Object-Oriented Programming

The focus shifts to refactoring the code using object-oriented programming principles. The presenter creates a 'ball' class in a new JavaScript file, 'ball.js', to avoid duplicating code. The class includes properties for the angle and a shifting angle, which is passed as a parameter. Methods for displaying the ball and updating its position are defined. The tutorial then shows how to integrate this new file into the main program and how to create ball objects with different shifting angles to produce the desired motion. The concept of arrays is introduced to manage multiple ball objects, and a for loop is used to create and update these objects.

15:05

🎶 Integrating Sound with the Animation

The paragraph delves into adding sound to the animation. The presenter guides through the process of uploading MP3 files into an 'assets' folder and using the 'preload' function to load these sound files. A method to play the sound files is demonstrated, and the concept of mapping each ball to a specific sound is introduced. The tutorial then shows how to preload all the sound files using a for loop and how to play a sound when a ball reaches the center point. The presenter also discusses adjusting the buffer to ensure the sound plays correctly and introduces the use of 'angle velocity' to control the speed of the balls' movement.

20:06

🔁 Handling Multiple Sound Files and Balls

The tutorial addresses the challenge of having more balls than sound files by using the modulo operator to循环回声文件数组,确保每个球都能播放声音。The presenter also shows how to adjust the buffer and angle velocity to fine-tune the sound playback. The paragraph concludes with a demonstration of how to create multiple rings of balls with different starting angles, using nested for loops and 2D arrays to manage the balls in each ring.

25:06

🖌️ Final Touches: Aesthetic Enhancements

The final paragraph focuses on the visual enhancements to the project. The presenter sets the background to black with a blur effect and changes the stroke color of the circles to white with a semi-transparent effect. The size of the circles is adjusted, and the tutorial concludes with a playful exploration of different configurations of loops and balls, demonstrating the creative potential of the project. The presenter encourages viewers to experiment with various settings to produce unique sounds and visuals.

Mindmap

Keywords

💡Object Oriented Programming

Object Oriented Programming (OOP) is a programming paradigm that uses objects and classes to design applications and software. In the context of the video, OOP is used to structure the code by creating a 'Ball' class, which encapsulates properties and methods related to the moving circles. This approach simplifies the code and makes it reusable, as seen when the tutorial shifts from duplicating code to creating multiple ball objects with varying properties.

💡Trigonometry

Trigonometry is a branch of mathematics that deals with the relationships between the angles and sides of triangles. In the video, trigonometry is essential for calculating the x and y coordinates of the circles as they move in a circular path. The script uses the equations x = r * cos(angle) and y = r * sin(angle) to determine the position of the circles on the canvas, which is crucial for creating the oscillating motion effect.

💡Oscillating Motion

Oscillating motion refers to the repetitive back-and-forth movement around a central point. The video's theme revolves around creating a visual representation of this motion using circles that move around a central point on the canvas. This motion is achieved through the use of trigonometric functions and is central to the animation's aesthetic and the soundtrack it generates.

💡Canvas

In the context of the video, the canvas refers to the virtual space where the animation is drawn. The script mentions translating the origin point of the canvas to the center, which is necessary for drawing the circles in the correct position relative to the center point. This concept is fundamental to setting up the coordinate system for the animation.

💡Preload

The 'preload' function in programming is used to load resources such as images or sounds before the main execution of the program. In the video, preload is used to load MP3 sound files into the program. This is important for ensuring that the sounds are ready to be played when the circles pass through the center point, which is a key part of the interactive animation.

💡Sound Assets

Sound assets are audio files that are used in a project. In the video, the creator uploads MP3 files as sound assets to be used when the circles hit the center point. These assets are part of the interactive experience, as they contribute to the soundtrack that is created by the movement of the circles.

💡Collision Detection

Collision detection is a technique used in animations and games to determine when two objects have intersected or 'collided'. In the script, a method called 'Collis' is created to check if the ball has passed through the center point. This detection triggers the playing of a sound, which is a core aspect of the video's interactive animation.

💡Modulo Operation

The modulo operation finds the remainder of a division between two numbers. In the video, modulo is used to循环播放 sound assets when the number of balls exceeds the number of available sound files. This ensures that the animation can continue to play sounds without running out of assets, creating a continuous soundtrack.

💡2D Array

A 2D array is a data structure that consists of arrays of arrays, essentially creating a matrix. In the video, the script transitions from using a 1D array for balls to a 2D array to manage multiple rings of balls. This change in data structure allows for more organized and scalable code, especially when dealing with multiple layers of objects in the animation.

💡Transparency

Transparency in digital graphics refers to the degree to which objects allow light to pass through them, often represented as an alpha value. In the video, the script sets the transparency of the circles' strokes and the background to create a visual effect. This use of transparency adds depth and aesthetic appeal to the animation.

Highlights

Introduction to creating an interesting track with sound and movement.

Tutorial builds upon previous work with oscillating circles around a center point.

Use of object-oriented programming to streamline the coding process.

Explanation of creating multiple rings of circles for a soundtrack.

Demonstration of drawing a circle in the middle of the canvas using translate function.

Utilization of trigonometry for circular motion and oscillation.

Setting up the angle to oscillate based on the mouse location.

Adjusting the angle mode to degrees for accurate motion.

Creating the illusion of circular motion with push and pop transformations.

Encapsulating the animation logic into a class for cleaner code.

Introduction of a method to update the angle for oscillation.

Creating an array of balls to manage multiple circles efficiently.

Integration of sound assets into the program using preload function.

Method to play a sound when a circle passes through the center point.

Adjusting the buffer to ensure accurate collision detection for sound playback.

Managing a larger number of balls with a limited number of sound files using modulo.

Creating multiple rings with different starting angles for a richer visual effect.

Use of 2D arrays to manage multiple rings of circles.

Final touches to the visual appearance with background and stroke settings.

Encouragement to experiment with different configurations for unique results.

Transcripts

play00:00

welcome to another coding tutorial and

play00:02

in today's video we'll be experimenting

play00:04

with some sound and movement to create

play00:06

this interesting track that you're

play00:07

hearing right

play00:10

now this tutorial is an add-on to a

play00:12

video that I did in the past where a

play00:14

bunch of circles are moving in an

play00:16

oscillating motion which is a movement

play00:18

back and forth around a center point

play00:20

this time around I'll be going a little

play00:21

bit faster and we'll also be approaching

play00:23

it a little bit differently Cally we'll

play00:25

be using object oriented programming so

play00:28

I recommend you checking out the other

play00:29

video as well if you want a little bit

play00:31

more details the add-on that we're going

play00:33

to do is that one we're going to have a

play00:34

circle play a note once it passes

play00:36

through a center point and then two

play00:38

instead of creating this one ring of

play00:40

circles we're going to be creating

play00:41

multiples to create interesting

play00:43

soundtrack why don't we start by

play00:45

creating the animation so we're going to

play00:47

draw a circle in the middle of the

play00:49

canvas I'm going to actually translate

play00:52

the origin points from the top left

play00:54

corner here to the middle of the canvas

play00:56

and we can do that using a translate

play00:58

function and and then provide the two

play01:00

arguments of where you want to move the

play01:02

origin to so we're going to put width /

play01:05

two and height / two and by doing this

play01:09

now drawing on the lips I can just put

play01:12

in the new origin at 0 comma 0 and now

play01:14

it's going to be in the middle point and

play01:16

I want the radius to be of the variable

play01:19

R which I'm going to declare right now

play01:23

and set it to

play01:26

150 all right and now how about we also

play01:29

do do no

play01:32

fill now how about we draw another

play01:35

Circle a smaller one and have it be

play01:38

traveling on this circular path so we

play01:41

need to use trigonometry specifically

play01:43

the equation x = to R * cosine of angle

play01:47

and y = r * s of anle then let's set

play01:53

anle to be equal to zero then I'm going

play01:56

to draw this ellipse let's color it red

play01:59

and and let's draw it at The X and Y

play02:03

location here and give it a size of

play02:08

20 all right then now I'm going to also

play02:14

actually set the angle map it to my

play02:17

mouse location between zero and the

play02:19

width and between the angle 0 and

play02:24

360° and because I'm using the degrees

play02:26

mode I also need to change the angle

play02:29

mode to degrees and I can do that just

play02:32

by calling this function right here and

play02:34

provide the argument

play02:37

degrees all right so now as I move my

play02:41

mouse the circle travels along this

play02:44

circular

play02:46

path perfect but we don't actually want

play02:49

the circle to travel along the circular

play02:51

path we actually want it to oscillate

play02:53

around a center point which is the

play02:55

origin so what we can do

play02:58

basically we just draw another ellipse

play03:01

at the same X location but we want the Y

play03:04

to be at zero actually and let's give it

play03:07

the same size I'm going to fill it with

play03:10

the color

play03:14

white and as you can see here it is

play03:17

essentially just

play03:19

following the x coordinate of the red

play03:23

circle

play03:26

right perfect how about we also draw

play03:29

draw a line that show the path that it

play03:32

is traveling in so a line function takes

play03:35

in four arguments the two coordinates of

play03:37

the two points that ones connect become

play03:39

a line so it's going to be r z and

play03:45

r0 now let's draw another Circle that

play03:48

actually travels vertically so what we

play03:51

can do is

play03:53

basically how about I stop this from

play03:56

moving first and then I'm going to draw

play03:59

out the same thing but this time around

play04:02

we want to actually rotate the axis by a

play04:07

certain degree so how about we rotate it

play04:11

counterclockwise by

play04:14

90° so if I were to move

play04:17

this so this will not actually give us

play04:21

the illusion that we want of the white

play04:22

circles traveling in a circular path to

play04:25

do that the white circles actually needs

play04:27

to follow the X and Y coordinates of

play04:31

this red circle here so what we need to

play04:33

do actually is that we need to give

play04:37

another variable and add it inside this

play04:40

equation here so I'm going to actually

play04:44

declare a new variable called X2 and it

play04:47

will be R * cosine of angle plus

play04:53

shifting angle and basically this

play04:56

shifting angle it needs to be shifted

play04:59

the same amount as a rotation so how

play05:01

about we set let

play05:06

shifting ankle equals to 90 and please

play05:09

notice that this is positive but this

play05:12

one the rotation of the axis is negative

play05:16

all right so if we do

play05:20

this then

play05:22

now oh we also need to set this to

play05:26

X2 and now the white circles are

play05:29

tracking the X and Y location of the red

play05:33

circles so how about we do two more to

play05:36

get that circular motion illusion so I'm

play05:39

going to actually declare this as

play05:43

two and then how about we do two more

play05:47

and let's set it

play05:49

at in between so 45 and

play05:53

135 and then I'm just going to copy

play05:58

this to more times and then change this

play06:01

to three three three this one to

play06:06

four four and

play06:09

four all right and also in here we need

play06:13

it to be shifting angle of two

play06:16

right this one is of three and then this

play06:20

one is of four all right let's

play06:24

run okay and then this is not what we

play06:28

want what do we

play06:30

miss we missed two very important

play06:33

functions when do any types of

play06:34

Transformations which are push and pop

play06:37

and push basically saves the

play06:38

transformation specifically the rotation

play06:40

here of negative shifting angle and then

play06:43

pop returns it back to the original

play06:45

setting before we call the next rotation

play06:49

or the next transformation so that's

play06:52

what we need to put

play06:53

in before and after each transformation

play06:57

so push and pop

play07:01

push and pop here and then the last one

play07:08

here and this should fix

play07:13

it all right so

play07:17

now the white circles are

play07:20

moving it seems like they're moving in a

play07:22

circular motion great why don't we put

play07:25

this inside a class first so that we

play07:27

don't need to copy and paste the set of

play07:29

cod code multiple times so let's come to

play07:31

this Arrow here click the plus sign and

play07:34

then click create file we're going to

play07:36

call this file ball.

play07:38

JS and then before we start writing this

play07:42

class let's go to

play07:44

index.html then come to this line of

play07:46

code let's copy and

play07:48

paste change the name from sketch to the

play07:51

name of the new file that you just

play07:52

created in my case it's ball. JS and

play07:55

this is how you integrate a new

play07:56

Javascript file into the program all

play07:59

right now let's go back to ball. JS to

play08:02

write a class we start with the word

play08:03

class and then I'm going to call this

play08:05

class ball and then inside the

play08:08

Constructor function what do we need

play08:10

let's go

play08:12

back

play08:14

so we can keep the radius AS Global so

play08:17

we don't need that we want angle and we

play08:20

want shifting angle all right so let's

play08:23

start with these two so how about we set

play08:26

angle to be equal to zero but then we

play08:28

set shifting angle as a parameter

play08:37

here all right and

play08:40

then now inside here we don't really

play08:43

need this red circle anymore so I'm

play08:46

going to actually delete

play08:47

it and what we want is we want the set

play08:51

of codes right and this is going to

play08:54

actually be in another method let's call

play08:56

it display

play09:01

we also want to set X as a variable here

play09:05

so this.x = to R time cosine of this do

play09:08

anle and then this do shifting angle

play09:12

right and then this also has to be this

play09:20

dot all right and then how about we do

play09:23

one more method called update and then

play09:26

we're actually going to change the angle

play09:28

or increment the angle by how about 1°

play09:31

at a time whenever we call update all

play09:34

right let's start with that so let's go

play09:36

back now we can delete all of

play09:41

this and we want to still keep the

play09:43

biggest circle which is up here and we

play09:47

can delete this as well how we just

play09:49

start by creating just one ball object

play09:53

so that b to be equals to a new ball and

play09:57

we need what we need just the shifting

play10:00

angle right so how about we set the

play10:02

shifting angle to be

play10:05

zero so it is actually just going to be

play10:09

at this point here and then we're going

play10:11

to call update and

play10:17

display all right and then now the

play10:19

circle is moving in an oscillating

play10:21

motion around a Center Point great so

play10:24

how about we create more of them so

play10:27

instead I'm going to create an array and

play10:29

and let's call it ball and then also

play10:31

create another variable called num how

play10:34

about we set num to just

play10:37

two then to create new objects we're

play10:41

going to use a for Loop that goes from I

play10:43

equal Z to I less than num

play10:47

i++ and then we're going to Now set

play10:51

balls of I to be equals to a new ball

play10:53

object but here we need to put in the

play10:56

new shifting angle right so let's see

play10:58

how we would calculate

play11:01

that so if we have just one ball object

play11:07

the shifting angle will be zero right

play11:09

but if we have two we want the shifting

play11:11

angle to be at 90° right just like what

play11:15

we did previously we want to rotate the

play11:19

axis 90° to the left so what we need to

play11:23

do is that we need to use an equation

play11:25

180 / num * I so this way if num equal 2

play11:31

180 / 2 is 90 * 0 is 0 and then * 1 is

play11:37

90 so that's exactly what we want then

play11:40

we can just put in shifting angle in

play11:42

here and then now we just need the same

play11:45

for Loop to

play11:47

call these two

play11:50

methods so it's going to be balls of I

play11:54

and here as well

play12:01

all right and then now how about we

play12:03

change it to

play12:07

four how but we change it to

play12:10

10 perfect now that we have the

play12:13

animation done why don't we start to

play12:14

figure out how to add on some sound

play12:17

let's come to this Arrow here click the

play12:19

plus sign and then click create folder

play12:21

we're going to be putting all of the MP3

play12:23

files into this folder right here I'm

play12:25

going to call it

play12:27

assets and then you need to click cck

play12:29

this Arrow here click upload file and

play12:32

then you're going to be drag and

play12:33

dropping your MP3 files in here then

play12:35

once your fil is uploaded you can just

play12:37

click this and as you can see here I

play12:39

have a total of 12 MP3 files and this is

play12:43

off a chromatic scale and a chromatic

play12:45

scale is essentially the 12 notes in one

play12:48

octave on a piano key then now what we

play12:51

want to do is we want to how about we

play12:54

label this first this is

play12:57

ball variables and then I'm going to Now

play13:02

set variables for our sound assets I'm

play13:06

going to start by actually just

play13:07

uploading One MP3 file just to show you

play13:10

how to upload so how about we set this

play13:12

variable called asset and then we want

play13:15

to use a function called preload and

play13:18

then this is going to allow you to

play13:19

preload your sound files onto the

play13:22

program before you start calling the

play13:24

other functions all right so what we

play13:27

want to do is that we want to set the

play13:28

variable asset set to another function

play13:30

called load sound and then you want to

play13:33

put in the location of the sound file

play13:37

that you have so in our case it's in a

play13:39

folder called assets and then let's just

play13:41

do this ash. MP3 file so you need to put

play13:45

in the quotation mark put in the name of

play13:48

your folder and then backs slash and

play13:50

then a sharp.

play13:53

MP3 let's click

play13:56

run all right still working and then now

play14:00

I'm going to show you how to play this

play14:02

file all you need to do is put in the

play14:04

name of your variable here put in Dot

play14:08

and then play the function

play14:15

play and it is as easy as that okay so

play14:19

now that we know how to preload one file

play14:22

how about we do it for all of them

play14:25

instead of setting just a variable

play14:26

called asset I'm going to call it assets

play14:29

so now we have an array of sound files

play14:32

that are arranged in a specific way and

play14:34

this is just how I choose to do it but

play14:37

you can actually play around with how

play14:39

you want to arrange these files all

play14:42

right and now inside preload what you

play14:44

need to do is that you need to write a

play14:47

for Loop that goes from I equal to Z to

play14:49

I less than assets.

play14:55

length i++

play14:59

and then we're going to be doing the

play15:01

same command here but instead of a

play15:04

variable here it's going to be a new

play15:07

array let's call it sound and then let's

play15:11

do sound of I to be equal to load sound

play15:15

of assets

play15:20

plus assets of I let's click run the

play15:25

loading text here tells me that it's not

play15:27

pre-loading properly and I'm not

play15:29

actually sure why but I found a way to

play15:31

fix it and that is by instead of putting

play15:34

the names like this I just change it to

play15:36

this way of putting in the file names so

play15:40

instead of doing assets back slash plus

play15:43

this we can just

play15:45

do this so if you actually know why this

play15:49

error happens please comment down below

play15:52

but if we do this first we need to also

play15:55

comment this out let's click run and now

play15:58

it seems to be pre-load in

play15:59

properly so what we want to do now is

play16:03

that we want to go back

play16:06

to ball. JS and we want to add in this.

play16:10

note as a new

play16:14

parameter and then we're going to create

play16:17

a new method let's call it

play16:21

Collis and essentially this method is

play16:24

going to tell us whether the ball hits

play16:28

the Middle Point

play16:29

so how about we just do a conditional

play16:31

statement that says if this.x is equals

play16:35

to a specific point x so this is going

play16:38

to be our parameter then return true

play16:43

else return false okay and then inside

play16:48

update here we want to call this method

play16:51

and to call a method inside a class all

play16:53

you need to do is put in the word this

play16:55

Dot and then the name of the class and

play16:58

then we want to put in zero right which

play17:00

is the middle

play17:01

point but this actually is going to be a

play17:05

conditional statement that says if this.

play17:07

Collision of zero equals to true then we

play17:11

want to play the

play17:17

note

play17:19

and yeah that's it so let's go back to

play17:22

sketch. JS now we need to add a new

play17:25

argument inside our object here which is

play17:28

going to be

play17:29

sound of

play17:35

I and let's delete this all right so

play17:38

let's

play17:40

play hm but it's not playing why is that

play17:45

so let's go back to ball. JZ

play17:50

and let's print this.x and X

play18:01

so as you can see here the location of

play18:04

the ball actually moves between negative

play18:07

to positive so it actually hits zero but

play18:10

it doesn't hit zero exactly and that's

play18:12

why it's really hard to get this do X to

play18:16

be equals to X so how about we add some

play18:19

buffer to make sure that once it hits

play18:22

but if it's off by a little bit then

play18:23

still return true so what we can do is

play18:27

we can add

play18:29

actually I'm going to set it as a

play18:31

variable called

play18:35

buffer and then add it as a new

play18:37

parameter

play18:39

here so if this.x is greater or equals

play18:44

to x minus buffer and this. X is less

play18:49

than or equals to X+ this.

play18:54

buffer so this has to be this. buffer

play18:59

all right and

play19:02

then now if we go back to sketch. JS we

play19:06

need to add buffer how about we set

play19:08

buffer at

play19:10

one is that too small let's

play19:15

try seems to be

play19:18

okay so I really think that it also

play19:20

depends on the rate at which you move

play19:23

the circle so why don't we add that as

play19:26

an argument too or a parameter so how

play19:29

about I add one more angle V for angle

play19:31

velocity and then just set angle V to be

play19:35

equals to this and then instead of

play19:38

incrementing by one here we're going to

play19:40

set this do angle V here then we're

play19:44

going to come back here and actually

play19:47

instead of putting one here I'm going to

play19:49

set it as buffer a variable buffer an

play19:52

angle V and then I'm going to come up

play19:54

here and then set buffer to one and then

play19:58

how about angle

play19:59

v21 as well so we can adjust it easily

play20:05

here all right then how about we start

play20:10

[Music]

play20:22

smaller you see that it doesn't play all

play20:25

of

play20:26

them so maybe you need to to increase

play20:29

the

play20:32

buffer but you can see that it's kind of

play20:35

played the note twice so this is where

play20:38

you want to adjust it a little bit how

play20:39

about

play20:43

1.5 to get the final result that you

play20:45

want you can address the buffer and the

play20:47

angle V based on the number of balls

play20:49

that you have to create the sound that

play20:51

you want but what if I want to increase

play20:53

the number of balls to be greater than

play20:56

the number of sound files that I have

play20:58

let's say say I have 13 balls

play21:02

here it gives me an error and that is

play21:05

because why it is because there is no

play21:08

sound of index 13 right so what we can

play21:13

do is that we can Loop it back we can do

play21:15

that quite easily by using an expression

play21:16

called modulo and basically modulo is

play21:19

this percentage signs where you want to

play21:21

do I mod and then the length of your

play21:24

array which is sound. length which is 12

play21:28

and that is because this expression here

play21:31

Returns the remainder between these two

play21:34

Division I and sound. length so let's

play21:37

say that you have I = to 0 to 12 when

play21:41

you divide that by the length which is

play21:44

12 you get to return number of between 0

play21:48

and 11 but once it is more than the

play21:52

sound length let's say 13 right so 13

play21:55

mod sound. length which is 13 / 12 you

play21:59

get a remainder of one we will always

play22:01

get an index that is between the length

play22:04

of the sound array right so let's try

play22:08

[Music]

play22:13

this okay so this is how we fix this

play22:16

issue before we move to the second

play22:18

add-on I want to change the number back

play22:20

to 12 and then how about we set ankle

play22:23

velocity to 1.5 and then let's do a

play22:26

buffer of 2.5 this this seems to give a

play22:29

pretty good

play22:32

[Music]

play22:33

sound the last add-on is to create

play22:37

multiple rings and we can do that quite

play22:39

easily by how about just copy and

play22:42

pasting this for a loop here so we

play22:44

create multiple more objects and then we

play22:47

want

play22:48

to create another array let's call it

play22:51

balls

play22:53

2 and then we just need to change balls

play22:56

two here

play22:59

and then we want to call the method here

play23:06

right but you only see one ring and that

play23:09

is because of the starting angle we want

play23:12

the two rings to have a different

play23:13

starting angle so how about we go back

play23:16

to ball. JS and we are going to add

play23:21

starting angle here as a new parameter

play23:24

and let's set this do starting angle to

play23:28

be starting angle and all we need to do

play23:31

is that we just need to add this inside

play23:34

this parenthesis

play23:38

here so instead of having it start at

play23:41

zero all the time it will be starting at

play23:44

a new starting angle so now all we need

play23:47

to do is we how about we set zero and

play23:51

180 here then I'm going to stop it from

play23:54

moving for

play23:56

now so now you can see that

play23:59

we have two rings right that start at

play24:03

zero and 180 all right so you can change

play24:06

it to let's say

play24:09

90 and then now you can see that this

play24:11

ring starts here all right so we can do

play24:14

it this way and it's fine but it is

play24:17

basically copy and pasting which is not

play24:20

as clean as I would like and it's going

play24:22

to be more difficult if we want to add

play24:24

multiple rings so how about we actually

play24:26

create a 2d array

play24:29

so we can delete this balls 2 array and

play24:32

we're going to make this balls array

play24:35

from a 1D array to be a 2d array so what

play24:37

do we need to do what we need to do is

play24:40

we need to create a nesset for a loop so

play24:43

let I equal Z to I less than how about

play24:46

we set another variable called Loop and

play24:50

we're going to set loop to be

play24:52

2 so I less than Loop and then I ++ and

play24:58

then we're basically going to put this

play25:00

other for Loop inside here then I'm

play25:03

going to delete all this first and then

play25:06

we need to make sure that all of this

play25:07

has to be J right so all of the previous

play25:11

I will become

play25:15

J and and then this is for 1D array

play25:20

right so what you want is

play25:22

first inside of this outer loop we want

play25:25

to create a 2d array so we can do that

play25:27

by setting balls of I to be equals to a

play25:31

bunch of empty 1D arrays and then now

play25:34

balls of I of J will be a new ball

play25:40

object and instead of zero here we also

play25:43

want to set the new starting angle right

play25:47

and how would we calculate this if we

play25:49

want to space out the starting angle

play25:51

evenly what we can do is we can do 360

play25:54

which is one revolution divided by the

play25:56

number of loop that we have and then we

play25:58

just multiply it by I so with one Loop

play26:01

it starts at zero and then with two

play26:04

Loops it starts at 180 so now we just

play26:07

need to put in starting angle

play26:11

Here and Now inside

play26:14

here we don't need this second one we

play26:17

just need this one and then we want to

play26:20

actually do a nested for Loop here as

play26:22

well

play26:26

right then this

play26:28

will be inside here and then change all

play26:31

of the previous I to

play26:35

J then this will be balls of i.j because

play26:39

now it is a 2d

play26:41

array so let's actually stop it from

play26:43

moving first so now we have two rings

play26:47

that are spaced out evenly so we can

play26:49

just change the number of Loops here

play26:58

and that's

play27:00

it right I'm going to keep it at

play27:02

three and then how about

play27:06

we uncomment this

play27:10

out and there you go now we have this

play27:12

interesting track that sounds like when

play27:14

Mario meets the final boss okay so how

play27:18

about we make it pretty which is the

play27:19

last step what I want to do is first I

play27:22

want to set the background to Black and

play27:25

I also want to set the transparency to

play27:27

100 to give it some of a blurring

play27:29

effect and for The Strokes

play27:33

of these circles I want it to be white

play27:38

with a transparency of

play27:40

50 and then inside ball. JS I actually

play27:45

want the circles to be smaller at 10

play27:48

comma 10 and I think that's it let's try

play27:55

it and then how about we play around

play27:59

so if I were to do

play28:05

eight at four

play28:08

Loops oo that sounds kind of

play28:12

happy

play28:15

five have

play28:18

24

play28:22

wa so this is so much fun to play around

play28:27

with so I hope that that this gives you

play28:28

a lot of ideas to experiment with so

play28:31

give this one a try

Rate This

5.0 / 5 (0 votes)

Related Tags
Coding TutorialInteractive ArtSound ProgrammingOscillating MotionObject-OrientedTrigonometryAnimationMusic VisualizationCreative CodingP5.js