Placeholder Image

Subtitles section Play video

  • Hello again.

  • Welcome to the wasting time portion of the coating trains.

  • My name is Dan Schiffman, and I have about 40 minutes before I have to go.

  • I just finished a hour of code tutorial on the camera.

  • Of course.

  • Just shut off Where I made this project that tracks your nose.

  • It tracks your nose with P five ml five and the post net model.

  • And I'm here now to waste your time because I have things.

  • I want to experiment with the discuss, but you should probably just go about with your day.

  • If I can manage a good quoting challenge in before I go, I would like to do that.

  • So one thing I want a test here, and I don't know why this isn't working.

  • Let me try refreshing this again.

  • All right, So I'm planning, by the way, is probably gonna be December 20th.

  • I am planning a fundraiser for the Processing Foundation Processing Foundation dot, or ge, if you're not familiar with the Process Foundation.

  • The processing foundation is the non for profit entity that that manages the processing project P five jazz process python, and also a lot of community and education initiatives around art and code and all sorts of stuff.

  • So read the mission statement.

  • Come hang out with us at any number of processing days around the world, which is happening in 2019.

  • Um, and I will be at processing Los Angeles and hopefully it crossing NYC as well.

  • All right, That aside, I wanted to figure out I have this Processing is a 51 C three non for profit.

  • And this setting, I don't see it.

  • I want to enable super chat for good to see if I can, um, to see if I can figure out how to get live donations.

  • And then I can also build something which actually shows the live chat.

  • But maybe what I first need to do.

  • Hold on.

  • So what I'm doing over here is I'm in my admin panel and maybe I'll just build something that manually pops up something when there's a super chat.

  • But why do I not have going toe?

  • I'm goingto live streaming.

  • I'm going into events alive.

  • Edits There's 214 of you apparently watching right now.

  • Um, and under advanced settings under chat, it's not there.

  • So I used to have an option.

  • That was super chat for good.

  • And I could enable the Crossing Foundation.

  • So never mind.

  • I wanted to test that.

  • Now I'm gonna have to figure that out and test that another time.

  • Oh, look, it's me with googly eyes.

  • Thank you.

  • Uh, Thio Bruno Ruchi ge who made this wonderful sketch, right?

  • So let's close all this out.

  • The other thing, that's goofy.

  • Okay, so I don't know what to do now.

  • Hello.

  • Are you Hopefully that, um hopefully that I should make you know what I should do for the fund raiser is on the fundraiser.

  • I should write a P five sketch that communicates with the YouTube a p i and like, makes a little, like heart or something whenever somebody makes a donation.

  • But you need to get that super chat for good thing working.

  • Um, okay, So if anybody knows, I mean, I'll contact YouTube, but if anybody knows about that or have worked with the YouTube a p I actually I know you can write bots and things for the chat, so that's actually probably what That's something I should look into and do So what can I do?

  • Right Now I have a 35.

  • I had this idea.

  • Actually, this is probably insane to try to do this of doing like, a flocking simulation.

  • Let's go to coating train, get hub.

  • How am I not Will signed in.

  • I'm gonna need my authentication here.

  • Uh oh.

  • Let me look up the authenticator.

  • Well, I'm I'm streaming and recording so weird, Um, and go down here.

  • It's signed in.

  • Know that, Zach?

  • My password.

  • That's my, uh, no.

  • Everybody knows my authentication code, but it's already gone.

  • All right, let's go here.

  • Only 871 871.

  • Ideas regarding your previous stream.

  • If you define a to d shape in relation to the points, surely you can in three D as well.

  • Yeah, absolutely.

  • Um all right.

  • No, uh, I really I have no plan.

  • Not whatsoever.

  • Schrodinger equation Create a Gaff ing revisiting pixel sorting through quick sort.

  • I would love to do the quick sort code John's right now, but I don't think I'm going to triple pendulum.

  • Jane, we're actually marching cube.

  • Who?

  • That would be a good one.

  • Stolen necklace, drunken bishop.

  • Spiral map projection coding challenge.

  • Maybe what we could do you know, I have this, like, weird idea that I would like to D'oh, um, which is because I was watching a standup maths video on on unboxing this DVD and was, like, this crazy, infinite Rikers in into the TV thing.

  • I feel like we should just program that That would be fun if I was doing video stuff.

  • Um, but I don't know, uh, plotter, soft body simulation room in numerals.

  • Clicking on object in three D space lizard visualization.

  • What is that of a movement of a lizard?

  • The lizard should have a triangle like head.

  • Body legs should go to the point where them is very specific.

  • Should go to the point.

  • Um, it should go to the point where the mouse was clicked.

  • The hard part will be the way the lizard moves.

  • It should turn and move at the same time and travel the least distance.

  • Ideally, when the lizard is facing one direction and when mouse's click, it should start moving like a real lizard recently.

  • Okay.

  • I don't think you get to this right now, so I don't know why, but in my head this morning, I thought I should do the flocking as a coding challenge.

  • Boyd's Boyd's job.

  • Look, I even assigned this.

  • No, there's no thumbs up on this one.

  • I'll give it a thumbs up.

  • Could this be done in 1/2 an hour?

  • That's the question Why?

  • Come on.

  • Get hub.

  • Come on, Computer.

  • Refresh the page.

  • It doesn't want to Let me give it a thumbs up.

  • No.

  • Here we go.

  • How many thumbs up?

  • How many thumbs up can I get on this page right now?

  • I'd like to see clock at destined Dust dust in its name is destined.

  • Destined office from smarter every day.

  • I don't I didn't I don't know about that clock from smarter every day.

  • Um, but but I'm always interested.

  • Oh, right.

  • Would you Ah, do weird, complex number math on the image to make that spiral pattern in Matt Parker's videos.

  • I also want to do I want to do some, like, fluid, dynamic stuff because I have been watching a lot of this murder everyday videos about, um, shock waves.

  • And I think that would be an interesting thing to d'oh shockwave visualization.

  • I think that no, nobody's thumbs up in this.

  • Just me.

  • I'm the only thumbs up.

  • Emoji too.

  • Yes.

  • All right.

  • Come on, everybody.

  • By the way, this is my This is my livestream for Get that to 10.

  • I'm gonna do it.

  • This is my live stream for the week tomorrow.

  • I'm actually doing some school visits.

  • Four hour of code, which is gonna be fun.

  • Got to number five number six.

  • And, um what I wanted to say, by the way, I am planning by nine.

  • Who are there any?

  • Oh, let's check if there's any coding faces, tweets and please two.

  • I'm just going thio.

  • I'm now always afraid I'm afraid of the Internet, so I'm going.

  • The coding faces page like this.

  • We have got some You've got one from Carol.

  • Um, lips.

  • Here we go.

  • Carol posted Karel de at d r K a r o l on Twitter three minutes ago posted my first ml experiments with coding face is awesome.

  • What number we have now?

  • 15.

  • Okay, so two thumbs down.

  • I don't make my sad face emoji sad face a movie If you don't doubt me, I'm confused.

  • Confused by your thumbs down the hearts.

  • Look at this.

  • This is really like wild to be to see this real 22 people are watching.

  • All right, But I have to I have to go.

  • I have a meeting to be a Do you think we could do the flocking?

  • All right, let's get prepared.

  • We'll check back on coding faces.

  • Let's get prepared to do this.

  • So, flocking video Shiffman, Let's see if that comes up.

  • Well, that's interesting.

  • Uh, I don't know.

  • Let's go.

  • Craig Reynolds steering.

  • No, not videos.

  • Cool.

  • It's original 1986 Boyd simulation.

  • Awesome.

  • I want this page.

  • Boyd's Here we go.

  • And then nature of code.

  • Uh, read now.

  • Uh, Times agents.

  • Here we go.

  • Those diagrams Air better area.

  • There you go.

  • Okay.

  • All right.

  • Uh, fucking Okay, So I've got my references.

  • I have got, um, on here, and we're gonna do this.

  • I think this requires, um I don't want to do this in the web editor.

  • I think there's too much involved with the complexity of this, that, um Come on.

  • Um, I think what I want to do is use us.

  • Um, I'm just gonna run my own server, um, with console.

  • And I am going thio.

  • Uh oh.

  • I already have code editor open.

  • I'm gonna get rid of this.

  • And someday I'm gonna, like, make myself a little template here.

  • But that is not that day.

  • Um oh, I need the cdn, um, on DDE.

  • Let's just in case I need the dom library.

  • Let's get this and let's get snatched.

  • Yes.

  • Let's just give it a fixed size background if you want.

  • And what's going on here?

  • Oh, no.

  • Tonight did I reset the bro?

  • I think I reset the browser here, and yeah, this is not selected any more.

  • There we go.

  • Now, this can be minimized.

  • I don't need that.

  • Open.

  • I don't need this open, uh, and don't say it.

  • Don't save.

  • So and then here, this.

  • So how we doing?

  • I'm standing here.

  • That's good.

  • So not in front of the code, so I can actually do this a little further over.

  • Okay.

  • How's that font size?

  • Um, and I've got this here, which actually also be over, because it could just be above me.

  • Here we go.

  • And what's more, room in the console.

  • Okay.

  • Perfect.

  • All right.

  • 32.

  • All right.

  • We're gonna do the flocking.

  • The Boyd's challenge.

  • This is definitely gonna take longer than 1/2 an hour.

  • That's a really complicated, complicated project.

  • Um, Simon?

  • Yes, I know.

  • I haven't redone the beginner p five tutorials.

  • I think I Maybe I need to schedule a time to do more of those.

  • Okay, So let me actually just go to, uh this is what I'm looking for, by the way.

  • Um, here we go.

  • All right.

  • Why is that so slow?

  • Um, let's open a processing shoot slow.

  • Wait.

  • I like trying pretty image behind me.

  • That'll be interesting.

  • Think I'm spending forever tweaking, which is really wasting time.

  • We should just decoding.

  • All right.

  • Okay.

  • I think I'm ready now.

  • Okay.

  • House.

  • Definitely gonna need the white board.

  • So let me erase that.

  • Okay?

  • Let me just send quick message here.

  • Um um let me, um Let me, uh, just send a quick message that I need to send.

  • Sorry.

  • Old?

  • Probably.

  • Literally.

  • Uh, Okay.

  • All right.

  • Close enough.

  • Close enough.

  • You should get some water.

  • Whatever.

  • We're just gonna we're gonna move forward with this.

  • Just let me just It's just that, like, a few more and make them is they're too big.

  • Let's make him a little bit smaller.

  • and that's this, like, Okay.

  • Oh, perfect.

  • Now it's perfect.

  • Okay.

  • Hello.

  • Welcome to a coding challenge.

  • In this coding challenge, I am going to attempt a program using the P five library from starting from no code.

  • Except for the just one more chance e Have this, like, hang up.

  • Where?

  • When I say from scratch, people always the YouTube.

  • Well, not actually from scraps, because years in the P five library.

  • You know what?

  • What?

  • What can I do?

  • Here we go.

  • Hello.

  • Welcome to coding challenge.

  • I am going to in this coding challenge.

  • Make this.

  • This is a processing sketch.

  • I need a random number.

  • I need to read a random number.

  • Move the mouse Out of the way.

  • Yes.

  • Thank you.

  • Good, good, good, good, good.

  • Thank you.

  • 72475 Have a random number.

  • Everything's gonna be fine.

  • Let me cycle the camera.

  • What's the chance that that could be done in 1/2 an hour?

  • Very, very little chance.

  • We'll also have any lunch.

  • This is not gonna go Well.

  • Hello.

  • Welcome to a coding challenge in this coding challenge, but I probably should go eat lunch, But I'm gonna just try this coding challenge.

  • I am going to attempt to program this.

  • This is a flocking simulation.

  • What you're seeing right now is an example that I made many years ago of a flocking simulation, a Boyd system running in processing.

  • So I'm gonna try to make exactly this or close to it in Java script with the P five library starting from no code at all, Obviously, besides the dependencies.

  • Okay, so this is the example running.

  • So let me just give you a little bit of background here.

  • If you want to learn more, huh?

  • If you want to learn more about the flocking algorithm as invented by Craig Reynolds, you can go to this link to read this explanation of it, and it find many other links.

  • And resource is down here.

  • Well, that's a lot of stuff.

  • Have fun.

  • See you in a few weeks.

  • You can also check out this YouTube video of the original 1986 flocking simulation.

  • That's amazing.

  • Wear all this is so cool.

  • I gotta love that.

  • And you can also find an explanation of it as part of my nature of code book online with some diagrams and code written in processing and an exercise that you could try, which I'll refer you back at the end.

  • But I'm going to start from this.

  • A blank slate.

  • I just have a canvas.

  • You have some amount of pixels in it.

  • There is nothing happening in the console.

  • And, um, I've got this coat set up and draw.

  • All right, so what do I need to do now?

  • There's definitely going to be a big need to re factor of the code that I come in, right.

  • So if you look at my example, it's hopefully somewhat well organized, but I'm gonna thank you, Matthew, for editing for me.

  • All right, let me just let me let me double back for a second.

  • So I'm gonna write this code without worrying about being, like, perfectly organized and being able to scale it very easily.

  • I just want to try to get the algorithm working.

  • But of course, where is the button?

  • If you want to see a re factored example of that, he probably going looking actually at my finished version in nature.

  • So the first thing that I want to do so because I want to add another job uh, a file called particle dot Js actually know what I should rename this to Boyd dot Js.

  • So what is this term, Boyd.

  • When Craig Reynolds invented this algorithm for simulating a flocking system like a flock of birds or a swarm of bees, I suppose he didn't want to use the term burned, but it is kind of like birds.

  • Oh, Boyd.

  • It's kind like droid.

  • But Boyd, go read the original paper about steering behaviors and flocking systems, and you'll find out more about the history about this.

  • But I am going to make a class called Boyd, and I am going to use the P five vector object.

  • And so this climbing to make Boyd objects.

  • And each each time I made a Boyd object.

  • I need a constructor, and I am going to give each boy a position.

  • I'm going to give each Boyd of velocity, and I mean to give each Boyd and acceleration, and all of those are gonna be vector.

  • So I'm doing this in two.

  • D.

  • U.

  • Should after you watch this.

  • If super you watch this, you should make your own three d version of this.

  • All the math will work exactly the same way, But you'll just need to rethink how you're visualizing this and just to give things a start.

  • Let's just put the the position, like in the middle of the window and let's write a function called like show, which will just draw them as a circle right now.

  • Actually, just even use point like stroke.

  • Wait 16 stroke to 55 point this dot position dot Exe.

  • I think visual studio code will do this for me.

  • No.

  • Where does it It fixed that for me once.

  • I had whatever.

  • Okay, so this dot a pointer event would not appoint your event.

  • I'm a point.

  • Okay, So now if I were to, then here, make a flock, which is an array, and I probably should make a flock class.

  • That might be a thing to re factor later, but I'm just going to say a flock dot push new Boyd.

  • And then I just want to say for every Boyd in the flock in a music of amusing afore of Luke.

  • But I set in because that's the way I feel right now.

  • Ah, boy dot show.

  • And then I need to make sure I'm also referencing boy dot Js in my index.

  • Html.

  • So I have the basic board class, which just creates a position, velocity, acceleration and draws.

  • It is a point, and then I have my sketch where I make one Boyd and I draw it in the center of the screen.

  • There it is.

  • There's my boy.

  • Step one complete probably should plan this out, but I have one boy.

  • That's good.

  • Now let's get that boy move.

  • So let's actually right in the Boyd object a function called update.

  • And this is this.

  • I probably covered ad nauseum and a lot of other videos in the whole nature of code Siri's.

  • But the idea here is that position is updated based on the objects based on the Boyds velocity, and its velocity is updated based on the Boyds acceleration.

  • So if I now actually create a, uh, P five dot vector a random to d believe this is a function that will give me a random velocity vector, and I now go in here and say Boyd dot update okay.

  • Cannot read property ad of undefined.

  • Oh, no, I have the vis 0.0, I just spelled it wrong.

  • I thought I missed this starts.

  • Okay, there we go.

  • Look, it's moving.

  • It's moving with Iran in velocity each time.

  • Okay, Now, let's just make a bunch of these.

  • Now I have a system of 100.

  • Boyd.

  • So interesting.

  • Look at this.

  • It fans out in this purpose Perfect circle.

  • Do you know why that ISS?

  • Because I make a Boyd and I give it a random velocity.

  • That random vector is always of unit length one.

  • The direction is something different.

  • So they're all actually moving with the same speed if I wanted them to have This is not an important detail to the flocking stuff, but just to sort of get a sense of how this vector stuff works.

  • I could say this dot velocity, um set mag, which is set the magnitude thio a random value between, like 0.5 and 1.5.

  • And now you can see now they're all moving with a slightly different velocity.

  • So this is, by the way, a nice little, like, almost like explosion motif.

  • And I could have them with a real big burst and they slow down.

  • There's all sorts of physics.

  • See, stuff I could do with this, but I just want to do flocking.

  • So how?

  • How?

  • How?

  • How do I do?

  • Flocking.

  • Let's return to this paper here.

  • This is the key behind Craig Reynolds.

  • Flocking algorithm three rules separation steer to avoid crowding local flock mates Alignment steer towards the average heading of local flock mates.

  • Cohesion steer to move toward the average position of local flock mates.

  • Something so crucial in that description is the word local, local, local.

  • What does that mean?

  • So let's just do I think the easiest one to do actually is alignment.

  • Let's do alignment first.

  • So, um, alignment.

  • Let's make our own diagram.

  • Okay, so let's say there are five.

  • Boyd's actually make a lot more of them.

  • These are all of my boys.

  • This is the one that I'm currently operating around, and it has a velocity vector like this.

  • Let me just make up.

  • Let's just pretend like the ones around it are kind of moving all in the same direction because it'll be easier to think about.

  • So this is the current one that I'm looking at.

  • So, and maybe some of these others are also moving in other directions.

  • Also, I could take the approach to say this particular Boyd needs to align itself to everything.

  • But this is not really how complex systems in nature actually work.

  • The emerging phenomena comes from this boy having a limited perception of its environment.

  • So maybe it's actually only able to see things that are in front of it or behind it or to the right or what's probably the most.

  • The simplest thing to implement is within some radius.

  • So I only want to look at the Boyds, that air within this radius meeting this one, This one This one, This one, This one.

  • This one.

  • So what if I iterated over all of these ones that are within some distance average all of their velocities and shift this velocity in the direction of all those velocities?

  • That's what we want to dio.

  • So I am going thio, right?

  • A function.

  • Now I'm going to put it in the Boyd.

  • I'm gonna call it a line, and it's going to get presumably an array of other boys.

  • Like the idea is this function.

  • Align this Boyd with all the other boys.

  • So what do I need to do?

  • I'm gonna make a variable called average, which is going to be a vector.

  • Then I am going to iterated it over.

  • And I could do all of these with, like, reduce, probably in some higher order array functions.

  • That's a great thing for you to re factor it later.

  • Um, but I'm just gonna do it this this way.

  • I'm gonna say I'm actually going to use I because, um, that's maybe that's maybe gonna help me.

  • I'm not really sure.

  • But cos I right now, actually, no, no, I'll still do use a four of loop.

  • Let.

  • I'm gonna call it other of Boyd's, um and I'm gonna just say average dot add other dot velocity.

  • So I'm adding up all of the velocity.

  • Is this how you do an average right vectors?

  • Right again?

  • You might want to go watch my videos about how vectors work, but a vector a p five vector just holds an X and A Y or an X and Y and Z.

  • So I wanna average the vector its representatives an arrow with an extra bone in a Y component going average it just like my average under ray of numbers.

  • Add them all up and divide by the total.

  • And then I would say, average dot divide by Boyd's dot length.

  • This would be a way of getting a way of getting the average velocity of all of the boys.

  • But remember, I only want the ones within some distance.

  • So actually, let's implement that right now.

  • Let's let's have some sort of distance like, uh, Max are a cult like perception equals, let's just make it through arbitrary like 100.

  • And then I'm going to now say, if the distance between the distance between this stock position dot expo you know what, I think there's a P five.

  • This is fine.

  • It's just gonna be It's kind of along the way.

  • I'm writing this, but I don't mind if the distance let's actually make it in a variable d equals the distance between this Boyd's position and the other Boyd's Position X And why?

  • And if that distance is less than 100.

  • Uh, look at that.

  • Look at this fancy thing that it's doing.

  • I have some, like, prettier stuff that, like, doesn't wanna let me learn a long line of code.

  • I guess I'll keep that in there for right now.

  • So I'm calculate the distance between this boy's position, the other boys position.

  • And if that distance is less than 100 I'm adding it up.

  • And I should say I should also probably have a total number.

  • Total equals zero added up.

  • I also should probably ignore myself and if other does not equal this right, So I basically and I might as well put that first right, as long as the other thing is not me, and the distance is less than 100 added up and then divide by the total.

  • But obviously on Lee want to divide by the total if total is greater than zero.

  • All right, so this is kind of a little bit of a long winded algorithm now, But what?

  • Oh, and perception, if d is less than perception.

  • So okay, I'm starting with the perception of radius of 100.

  • Maybe that would be better, as I call it like perception radius.

  • That's really what it is.

  • I'm starting with a perception rate is 100 on empty vector.

  • I'm adding up.

  • The velocity is of any vector any any boy that's near me, divided by the total and as long as I found one divided by the total.

  • And you know, I probably should, like, run this to check this code, but I'm just I'm going to keep going.

  • So now here's the thing I could just say, Like, for example, I could do something weird.

  • This would be really weird.

  • I could just say this stock velocity equals average.

  • Like what if I did that?

  • And then I said, Boyd, like here I said, boy dot align flock and I I don't what's gonna happen here, but let's refresh this right.

  • They just basically don't go anywhere because they're all instantly equal to each other's velocity.

  • So that's not really a good way.

  • What I need to do now is, if I don't want to actually assign its velocity that average directly, I want to steer towards it.

  • And this is where Craig Reynolds steering formula comes in.

  • A steering force equals some desired velocity, minus the current actual velocity.

  • It's kind of like the error, right if I and moving this way, if I'm sure moving this way.

  • But I want to move this way.

  • The way I'm moving minus the way I want to move is Oh, sorry, Where's my eraser desired?

  • Minus philosophy.

  • The way I want to move minus the way I am moving is the steering force meeting.

  • Push me in that direction, so apply a force.

  • Maybe I'm gonna turn the steering wheel.

  • You could think of these as bees or cars or birds or whatever, but this formula desired minus velocity will give me the steering force.

  • And what is the desired velocity here?

  • The desired velocity is actually that average.

  • I'm actually gonna rename this to desired.

  • And I'm going to add it up and divide by total.

  • And then I'm gonna say this dot velocity equals No, I'm not going to say that.

  • I'm going to say, uh, steering equals And I can actually just I should actually call this steering because I could do all this.

  • I don't need to save anything.

  • As I'm going to call that the steering force, I'm gonna add all the velocities, divide by the total, and then I'm going to say steering fourth is the desired.

  • Subtract this stock velocity and I'm going to say return steering.

  • So I want this function to basically I want this function to return that force.

  • So really what I should be doing is I should have I'm gonna write a function called Flock, maybe, uh or like, uh, yeah, flock with, uh, some of number of Boyd's, and then I'm gonna say, uh, line meant equals a line with those boys.

  • So I'm gonna get this force, and I I I suppose, um, where okay, steering is here.

  • So you know what?

  • I should always return steering, so I want to always return a vector.

  • But if it didn't find anything, I'll just return the zero vector.

  • And then what I'm going to do is I'm gonna say acceleration.

  • Ad alignment.

  • So the idea the way that that physics engine works on this has to do with Andi.

  • This is like a very simple kind of crude physics engine, but force equals mass times acceleration.

  • This is Newton's law of motion or acceleration equals force divided by mass.

  • Or in a world where the massive everything is one acceleration equals force.

  • So if I want to apply a force of this object, I just need to set the acceleration to that force.

  • And actually, So that's what I'm gonna do.

  • Just starting out here, I'm going to say acceleration equals alignment.

  • Okay, So now if I go back to sketch and I say, uh, Boyd dot flock Boyd's, we should now have all the Boyds doing that one with just just implemented with that Aline alignment Rule boy does not define sketch touch as line 14.

  • It was called Flock.

  • Oh, this is my variable.

  • Naming is terrible.

  • Um, but I'm gonna leave it that way right now.

  • A line is not defined, boy dot Js a line Boyd's Oh, this data line, this data line voids hopes acceleration is not defined.

  • Oh, my goodness.

  • That's not much better.

  • Okay, so let's think about this.

  • It's so it's kind of working.

  • Let me let me give them a much higher burst of speed, because I think, um, and all them starting the same plate actually also, let's let's do a couple things.

  • Let me actually not have them all start in the same place because that is just to see this effect happened.

  • Only actually, start them in random places on the screen.

  • Let me change that.

  • Let me make the perception raise a little bit smaller and I don't know why this matters, but let me draw them like a little bit smaller.

  • Okay, so you can see and let me actually have them start out going quite a bit faster.

  • So you can see as they get near each other, they start to they start to get each other's the loss, they start to average each other velocity with their neighbors, and you start seeing these clumps moving together.

  • Now, so much more to do on this.

  • All my goodness.

  • Okay, But first, a couple things one is I have now basically allowed these Boyd's to steer with infinite power in a way.

  • So I'm gonna say, Hey, you're your steering force is the difference between your desired velocity and your current velocity and this apply that directly to the acceleration.

  • But do you think about it if you're driving a car there in a car racing game, for example, is concept of handling like, how well can that car steer?

  • So we should probably have variable.

  • Um, we should probably have a variable.

  • I'm gonna call it Max Force, and I'm gonna set it equal toe one for a second.

  • And then what I'm gonna do is here.

  • I'm gonna say a steering a limit this dot Max force.

  • So this what this does is it limits the magnitude the length of that vector to some maximum force.

  • So this should be exactly the same, right?

  • We're not seeing anything different, but if I were to make maximum force, like 0.1 like really small right now, you're gonna see, like they're not actually changing their velocity.

  • They are, but very, very slowly.

  • And the other thing we're really gonna need to do I just have to, like, give up and do this.

  • I'm gonna add some something for the edges here.

  • So I'm going to say if this stop position dot X is greater than the with this stop position dot X should equal zero else.

  • If this stop position dot X is less than zero, this stop position dot x equals with so and then I'm just gonna do that for the why Why are you here?

  • Why did the terminal open?

  • I have no idea.

  • Uh, I think I hit like a key command that made that happen.

  • And then we're gonna do this for why?

  • And I could do this in so many different, more thoughtful or re factored ways, but this is gonna be good.

  • Oh, no.

  • What am I doing?

  • Okay with with x x y y y y y x x x x x x with with all right now.

  • Ah, here.

  • Let's d'oh boy dot edges.

  • So we should see them reappear wrapping around.

  • And now let's go back to the Boyd and let's make this like 0.2 is kind of reasonable ish, interestingly enough there, kind of like it slows them down, which is sort of which is interesting.

  • But I'm only doing alignment right now, but you could see how this works, right?

  • It's either all starting to align each other what?

  • I could keep them going.

  • It's a minimum velocity, which might make sense and could see how these air like, kind of going back and forth.

  • But you get the idea, Elena, why are they all slowing down?

  • Is that just like a coincidence?

  • One thing I could do is one thing I could do this.

  • This is actually gonna be worth it, because I should also give them a maximum speed like they like this is gonna be a parameter of variable.

  • It's gonna allow me to control the system pretty well, and I could consider their desired velocity in the alignment to not actually be the actual average velocity, but just the average direction because I could then say steering dot set magnitude to this Stott maximum speed.

  • So basically, I'm saying, like, I always want to go in the direction of my neighbors, but at maximum speed, and I don't know that that's really an important detail.

  • But if I add that in here, we might get the effect that I was sort of hoping to get, which now this is what I was expecting to see.

  • As they get near each other, they're all starting to align together.

  • Let me refresh that one more time.

  • You can see that they're clumping, and as a group they all start to align.

  • So this is the alignment rule.

  • This is a very simple rule.

  • It's predictable.

  • It's obvious this is alignment.

  • How many more rules do we have to do on?

  • At one point, I messed up the camera, didn't I?

  • Well, whatever Some fancy postproduction thing that not you could maybe d'oh!

  • Let me pause for a second.

  • This could be an edit point.

  • Um, all right, 12.

  • 06 All right, All right.

  • Half a hour so far.

  • All right.

  • It's gonna move faster now, I think.

  • Let me do cycle the cameras.

  • Okay, so that is alignment Woo alignment.

  • Now all we need to do is add cohesion and separation to separation is gonna be the hardest one to D'oh!

  • So let's do cohesion next.

  • So the same thing.

  • We're going to look at our neighbors.

  • We should go back to what the rule is actually defined as Let's go back to Craig Reynolds.

  • Original page cohesion steer to move toward the average position of local flock mates.

  • Okay, this is actually not gonna be that hard, because we've already calculated the average velocity of local flock mates.

  • Now, let's just duplicate that code, and I know I know we could re factor.

  • I will play the song because we could probably do this all in one fell swoop.

  • There's so many possibilities.

  • But now I just want to get the average location of my local flock mates and steer towards that.

  • All right, so I'm gonna the way I'm gonna do that in a So I'm just gonna I'm just gonna go nuts and copy Paste this whole thing and call it cohesion.

  • And I'm gonna keep the perception radius.

  • I'm gonna keep this idea of steering.

  • Keep this idea of total go through all the Boyds.

  • Check the distance between myself and the other ones.

  • As long as I'm not myself within the perception radius.

  • What am I doing?

  • Steering at?

  • Not the others velocity, but the others position, then right, So this should clearly be re factored into its own function.

  • Whatever.

  • Then as long as I have at least one, I'm going to divide by the total.

  • Now, I don't want to set the magnitude to the maximum speed.

  • So what I have now in steering is the average location.

  • So if this is the average location, well, it states over here, then what I need is a vector that that's clearly not the average location.

  • But let's just say it.

  • Waas.

  • The average location is kind of like where this boy is, but I want to steer in that direction.

  • So to get a vector in that direction, I take the average location minus the current position of meat, which is basically saying now what I want to do is, say steering.

  • Subtract this stop position.

  • So subtract my position out.

  • I have a vector that's pointing from me to the average location.

  • Remember which is in the steering variable?

  • Then let's say I want to go at maximum speed and then I wantto so this is now my desired velocity.

  • I'm gonna subtract out my current velocity limited to Max Force And there we go.

  • That's cohesion.

  • So now in where is that INF luck?

  • Uh, let cohesion equal this stock cohesion Boyd's and then, oh, we have a big problem.

  • This start acceleration equals alignment.

  • This dot acceleration equals cohesion.

  • So how can I set the acceleration to two different forces?

  • But before I even answer that, there's an easy answer.

  • That question.

  • Let's just comment out alignment and let's watch cohesion happen, right?

  • We can see they start to group together.

  • This is cohesion happening bait and that neighbor radius, by the way, is a super important value.

  • Right, that neighbor radius.

  • If I were to change that and I have a different perception radius, if I were to change that to, like 10,000 right, they're all going to come together as one because they all See everybody.

  • If I were to change that to 10 right there, Really?

  • This is a little pears.

  • Like you could see that kind of get in.

  • Little groups of to the force, though, isn't so strong.

  • So, like, if I were to change the maximum force right to like one Now you see them.

  • They should almost like these little like electron thingies that, like, start magnetic things that start to, like, spin around each other and then fan off.

  • Okay, so there's so many possibilities here.

  • Um, you know, I could make the maximum speed too.

  • Well, I'm not actually limited.

  • You could see.

  • There we go.

  • That's what I was looking for.

  • You could see they get into these little groups, but let me go back and say maximum speed is four Maxim forces point, too.

  • And I realized this maximum speed is not actually a maximum speed.

  • Because in update, if I really wanted it to be a maximum speed, I would want to say this dot velocity limit this dot max speed.

  • So let's actually add that in and see what we get.

  • This is Oh, and let's put the perception radius back at 100.

  • And there we go.

  • So now we have cohesion.

  • So we have cohesion.

  • What happens if we have cohesion and alignment?

  • How do we get cohesion and alignment?

  • Okay, this is fun.

  • I think this is really working.

  • I think it'd be a really long video.

  • Alright, cohesion and alignment.

  • The problem rests here.

  • It is actually a really easy thing to solve because this is called force acute.

  • The answer Years forced accumulation in physics.

  • If two forces air acting on an object, the resulting acceleration is the sum of those forces.

  • So all I need to do is say acceleration dot ad alignment and acceleration dot ad cohesion.

  • Right?

  • Because why is this not falling?

  • Because the force of gravity is pointed this way and the other force off my hands holding it up is pointed in the other direction and with an equal magnitude.

  • Therefore, it is at rest.

  • Those two forces have a added together, have a net acceleration of zero, but obviously so I need to add them together.

  • But there is this.

  • If I actually do this right now, we're gonna see some really crazy behavior.

  • Looks like it's kind of working But it really isn't because what it should also have is this acceleration shouldn't accumulate over time.

  • At every moment in time, I start with a net acceleration acceleration of zero and add all the forces together.

  • So what I should do right before I flock is say this dot acceleration set 00 This is setting its values to zero and zero.

  • Another way I could do that is just multiplying it by zero is multiplying a vector by zero takes everything out.

  • It could make a new vector or whatever, but this will work nicely.

  • And technically, this also might make more sense.

  • Here is kind like after I finish updating the velocity, then I can reset the acceleration case.

  • There's other things that play.

  • So now we can see both cohesion and separation.

  • I'm sorry.

  • Cohesion and alignment or look at this Eventually.

  • They're just all gonna become this one clump.

  • Come on, you could catch up.

  • You gotta go.

  • Go, go, Go club.

  • Okay.

  • One thing I would like to dio I really want to attach sliders in those parameters, but I've got to resist because we've gotta add separation.

  • Hey, we've got alignment and cohesion separation.

  • This one's a little bit hard.

  • Let's look at it.

  • Wait.

  • Come on.

  • Grow steer to avoid crowding.

  • Local flock mates steered to avoid crowding local flock mates.

  • Okay, so we have the idea.

  • Where's my razor way?

  • Have the idea of a local flock meat?

  • Let's kind of erase this for a second to just deal with two.

  • Here's me, that's me.

  • Here's my local flock meat.

  • If this local flock mate is too close, it's within some sort of distance threshold.

  • I want to steer to avoid that.

  • What would be my desired velocity?

  • My desired velocity would be to move in the opposite direction.

  • So the idea is my desired velocity is in the absurd.

  • But what if there is one here?

  • My desire velocity would also be in this opposite direction.

  • So then my net desired velocity would be the average of these two, and it even would make sense for the magnitude of that desired velocity to be proportional to the distance.

  • So if this one is here, maybe want to avoid it.

  • But I only really need toe.

  • Think about avoiding it a little bit.

  • If this one's really close, I need to avoid it a lot.

  • So the magnitude the vector is inversely proportional to the distance of the local flock bait.

  • And so this might end up with kind of, like a desired velocity of something like this if I'm averaging those two together.

  • Okay.

  • So let's see if we can implement that.

  • Not gonna be super easy, but let's do it.

  • Okay, So the nice thing is, I'm gonna start with cohesion because we've kind of cohesion is close to separation.

  • And again, boy, like no way wearing my I will back to this later.

  • Okay?

  • So separation.

  • So I'm gonna leave this at 100 steering.

  • I think I might need to use Maur like I kind of like the idea of just having one vector that ends up.

  • It's sort of the average here.

  • Then it's kind of like the difference, but I think I need I think I need to think I need Let's look one step at a time.

  • So I'm creating this vector.

  • If so, this is the same.

  • If it's not me, it's within the perception radius.

  • What do I need?

  • I need a vector.

  • I'm gonna call it a difference, which is the others.

  • I want a vector.

  • Sorry, that points from I want a vector that points from the other from the other to me because I want that to work under the opposite direction.

  • So it's my position minus the other.

  • So I want a vector, a new vector, which is the diff subtraction between my position and the other's position.

  • So this is a way of calling the subtract function, which doesn't operate on a vector but subtracts two vectors and gives us a new vector, which is difference.

  • Then I want the difference to be inversely proportional to the distance.

  • So I'm gonna multiply it or said its magnitude something like that.

  • I mean, this is making it.

  • I'm gonna give our divide by distance.

  • I could just say divide by distance, right?

  • It's inversely proportional.

  • The farther away it is, the lower the the higher the farther away it is, the lower the magnitude, the closer, the higher the magnitude.

  • Okay.

  • And the distance, I guess technically that distance could be zero, and that would be problematic.

  • But with floating point math, we almost never get a value zero.

  • OK, then I want to add, then that is the thing I add up.

  • That's the thing I wanna average all those vectors.

  • That air pointed away from the things near me.

  • I think we're actually i

Hello again.

Subtitles and vocabulary

Click the word to look it up Click the word to find further inforamtion about it