Coding Challenge

  Рет қаралды 322,146

The Coding Train

The Coding Train

Күн бұрын

In this coding challenge, I create a flocking simulation in JavaScript based on Craig Reynolds "boids" algorithm. Code: thecodingtrain.com/challenges...
🕹️ p5.js Web Editor Sketch: editor.p5js.org/codingtrain/s...
🎥 Previous video: • Coding Challenge #123:...
🎥 Next video: • Coding Challenge 125: ...
🎥 All videos: • Coding Challenges
References:
📄 Craig Reynolds' Paper on Boids: www.red3d.com/cwr/boids/
📕 Autonomous Agents - Nature of Code: natureofcode.com/book/chapter...
📘 The Computational Beauty of Nature: mitpress.mit.edu/books/comput...
Videos:
🌐 Original Boids Simulation: • Craig Reynolds - Origi...
➡️ Vectors - The Nature of Code: • 1.1 What is a Vector? ...
🪂 Autonomous Steering Agents: • 5.1 Autonomous Steerin...
🔴 Coding Train Live 162: • Coding Train Live 162:...
Related Coding Challenges:
🚂 #69 Evolutionary Steering Behaviors: • Coding Challenge #69: ...
Timestamps:
0:00 Introduction to flocking!
2:07 What are boids?
2:33 Creating a system of boids
7:06 Discussing the flocking algorithm
7:38 Alignment!
21:48 Cohesion!
28:37 Separation!
37:00 Things to try!
Editing by Mathieu Blanchette
Animations by Jason Heglund
Music from Epidemic Sound
🚂 Website: thecodingtrain.com/
👾 Share Your Creation! thecodingtrain.com/guides/pas...
🚩 Suggest Topics: github.com/CodingTrain/Sugges...
💡 GitHub: github.com/CodingTrain
💬 Discord: thecodingtrain.com/discord
💖 Membership: kzbin.infojoin
🛒 Store: standard.tv/codingtrain
🖋️ Twitter: / thecodingtrain
📸 Instagram: / the.coding.train
🎥 Coding Challenges: • Coding Challenges
🎥 Intro to Programming: • Start learning here!
🔗 p5.js: p5js.org
🔗 p5.js Web Editor: editor.p5js.org/
🔗 Processing: processing.org
📄 Code of Conduct: github.com/CodingTrain/Code-o...
This description was auto-generated. If you see a problem, please open an issue: github.com/CodingTrain/thecod...
#flocking #creativecoding #autonomousagents #processing #p5js

Пікірлер: 254
@MasterHigure
@MasterHigure 5 жыл бұрын
32:00 This makes diff have length 1, not be inversely proportional to d. You have to divide by d^2. I do think the "vortex" formations you get (where boids fly through the center of the local flock, out to the side, back around, and through the center again) kinda cool, though. Also, I don't think the boids' perception range goes past the edge-wrap of the screen (that looks like a hassle to code, though).
@TheCodingTrain
@TheCodingTrain 5 жыл бұрын
Omg yes indeed, oops! Thank you for this correction. Pinning the comment!
@VladSandu79
@VladSandu79 5 жыл бұрын
"perception range goes past the edge-wrap of the screen": we can see this "bug" at 20:23 on the right of the viewport how the boids keep apearing or dissapearing, also lining up vertically on the edge.
@PhilBoswell
@PhilBoswell 5 жыл бұрын
How about if every time you had let d = dist(this.position.x, this.position.y, other.position.x, other.position.y); you substituted let d = dist((this.position.x + width) % width, (this.position.y + height) % height, (other.position.x + width) % width, (other.position.y + height) % height); That seems to work for me…I suspect it would help if we abstracted out the selection code…is that the reFactor Song I hear playing? ;-) Here's my current version of this sketch, I added a couple of extra little touches ;-) editor.p5js.org/NotACat/full/SkoYSoCyV
@MasterHigure
@MasterHigure 5 жыл бұрын
​@@PhilBoswell I don't see how that fixes the wrapping. In fact, it wouldn't change a thing at all, as (this.position.x + width) % width == this.position.x and same with other, and same with y and height (except possibly for a boid lying EXACTLY on the edge, I don't know enough about JS to tell). If they are on opposite sides of the screen, you'd have to SUBTRACT width or height from the one furthest to the right or furthest down (and you'd have to figure out whether it's this or other, and whether it's just x, just y, both x and y, or x for one and y for the other). That looks like a hassle to me, with mins and maxes all around, and lots of ifs flying around. Definitely put it in its own function.
@PhilBoswell
@PhilBoswell 5 жыл бұрын
@@MasterHigure I had a fit of sense and looked on StackOverflow ;-) How about let d = sqrt(pow(min(abs(this.position.x - other.position.x), width - abs(this.position.x - other.position.x)), 2) + pow(min(abs(this.position.y - other.position.y), height - abs(this.position.y - other.position.y)), 2)) That seems to work rather better than my feeble effort and it's not too expensive either ;-)
@potatoes8169
@potatoes8169 5 жыл бұрын
last video i watched, he didnt eat lunch, this video he still hasn't eaten lunch. this makes me feel he was hungry for weeks man. lol
@toastyPredicament
@toastyPredicament Жыл бұрын
Potatoes
@user-ll3kw7ov2g
@user-ll3kw7ov2g 22 күн бұрын
@@toastyPredicament potaTOES are good
@KristianPedersen
@KristianPedersen 5 жыл бұрын
3:44 To disable autocomplete in VS Code, turn off "Accept suggestion on commit character".
@TheCodingTrain
@TheCodingTrain 5 жыл бұрын
This from the the creator of the THIS DOT song everyone!!!! ❤🙏❤🙏❤🙏❤🙏❤🙏❤🙏❤
@abdimalikosman559
@abdimalikosman559 5 жыл бұрын
Not all heroes wear capes. Hats off dude, you are just amazing.
@zaynbaig3157
@zaynbaig3157 4 жыл бұрын
I have probably seen this ten times, and I find it so intriguing every time. Thanks, Dan!
@UweEichel
@UweEichel 5 жыл бұрын
This was an amazing video! Thanks Dave. I enjoy having these kind of videos which are a bit longer than usual.
@Dgiulian
@Dgiulian 5 жыл бұрын
I subscribed to the channel a long time ago because of the nature of code series. This is was a really nice reminder of that topic
@sayanghosh6996
@sayanghosh6996 5 жыл бұрын
19:53 Dan.exe has stopped working
@kushagra64
@kushagra64 3 жыл бұрын
100 th like :D Edit: wow someone like it :)
@lopa8519
@lopa8519 4 жыл бұрын
The editing in this video was great. Loved this one, really interesting ! You're amazing for doing all of this for free. :D
@ezequiel9055
@ezequiel9055 2 жыл бұрын
This channel is GOLD! Dan is SO GOOD at explaining everything specially the most complex things.
@Ron-op8es
@Ron-op8es 10 ай бұрын
This is such a good video, I attempted this a year ago but wasn't able to figure out all the logic on my own. I just didn't understand how 2 numbers could represent a dot moving in any direction. We only need to change the x and y values, but how you would maintain the overall speed was a mystery. Finally, after 2 full watch throughs and a full year later I was finally able to code this just using vanilla js and html canvas. I created Vector class for the math bits, and now it actually makes sense why we would need to divide the velocity vector by its own magnitude, because it gives you a unit vector with magnitude 1 but with the same direction as before so you can scale it by whatever factor you want!!!
@BulletProofCupid7
@BulletProofCupid7 2 жыл бұрын
You should advertise your book more often. It took me way too many videos of yours to discover that you've written a book! A book with all you're teaching in videos! I would've gotten it way sooner, it's a really nice read so far :)!
@gid9551
@gid9551 5 жыл бұрын
Hey Dan! Greetz from Denmark. Just here to say thanks. Been following your coding challenges for a while now and amazed at the great content and enticing questions you pose to your viewers (like in this video - SO many things I could think of, if only I had the time). In the phrase 'standing on the shoulders of giants' I very much consider you a (teaching) giant. Keep on choo-ing!
@TheCodingTrain
@TheCodingTrain 5 жыл бұрын
Thank you!
@MrPeloseco
@MrPeloseco 5 жыл бұрын
Dan is here to introduce us and guide us through p5 and or processing. But I have to thank processing for introduce me to Dan and his videos/books. A total blast!!
@echoes6092
@echoes6092 5 жыл бұрын
Thank you so much for doing subtitles :) it really helps
@andrasfauszt1693
@andrasfauszt1693 5 жыл бұрын
Different mass would be fun. Few agile small boid run circles around a the heavy ones. And its also easy to implement and visualize with different radiuses/sizes.
@Eman6240
@Eman6240 3 жыл бұрын
Love your videos. Love the way you work towards the solution.
@RedHair651
@RedHair651 Жыл бұрын
This is flocking awesome. Cracking a cold one with the boids.
@BorisJensen
@BorisJensen 7 ай бұрын
20:25 The reason everything slows down is that the average velocity of all boids is close to zero. All the random starting velocities even out, and the average velocity of neighbours for a given boid will also be close to zero, and then we try to modify that boid's velocity towards that average
@tylerhorton9807
@tylerhorton9807 3 жыл бұрын
I've always wrote letters to my teachers.. always, so I can appreciate their brilliant mind helping me discover new ways to channel my genius, even if it's not so genius at times. I would just like to say, I've became an intermediate developer on many other platforms other than java due to your tutorials. I have even made realistic gears mechanics to attempt at making computer engineering into a sandbox style, so far it's going good.. and this is one of many features I am implementing into it, but beings as I have no developer team, and I'm still getting online to find tutorials for certain algoritms I've no knowledge of yet, because I'm self-taught moreorless, it'll most likely be 2025 before beta is even a thing..
@toastrecon
@toastrecon 5 жыл бұрын
So cool! Some other ideas: give the birds "momentum" and have them controlled by some kind of PID controller algorithm to make them seem more natural. You could also adjust the perception radius to be anisotropic and stochastic, so simulate a bird's vision. So, they could only "see" birds right in front of them, and sometimes they wouldn't see ones to their sides and never behind them.
@yannmassard3970
@yannmassard3970 Жыл бұрын
bird vision that flock (like pigeons) is almost 300d horizontally and 150 v
@tomd8168
@tomd8168 5 жыл бұрын
That was really awesome, thanks!
@LucaPrn
@LucaPrn 5 жыл бұрын
I couldnt be more lucky..a video from the god of processing exactly when I was planning to do a project on flocking :) thanks....god....of processing :)
@Chayat0freak
@Chayat0freak 5 жыл бұрын
an easier dispersal algorithm is simply to get the CoM calculated for cohesion and move away from that point if the Boid is already too close. Make sure this force is strong enough to counter the force of attraction. but is inversely proportional to the distance. Also you can speed this up by using distance calculations that avoid square root calculations
@pyrookil480
@pyrookil480 5 жыл бұрын
4:53 "There it is, there's by BOIIII"
@ArnoldsKtm
@ArnoldsKtm 5 жыл бұрын
One round boi
@Wherrimy
@Wherrimy 5 жыл бұрын
Mah BOI !!
@zanejohns777
@zanejohns777 5 жыл бұрын
Your teachings are very inspiring, sir. I have learned so much, and I am very thankful for what you contribute. I was thinking about the section in which there is a division by distance. As the distance approaches zero, the result will become very large, potentially causing a less smooth behavior, though maybe only for a couple of frames or so. It may be good to consider a minimum distance before any division occurs. That is my two cents, anyhow.
@atrumluminarium
@atrumluminarium 5 жыл бұрын
An addition to the "optimisation" discussion, rather than the inverse distance, use the inverse square. Then write an alternative distance function with just d=∆x²+∆y² to get rid of messy square roots
@TheFinagle
@TheFinagle 2 жыл бұрын
20:20 when your adjusting the direction your allowing the oppsing vectors to cancel out. across 100 random vectors the average is close to 0. you want to change the direction to match without adjusting the length of individual vectors (unless that boid intends to speed up or slow down toward a goal).
@emanouda9797
@emanouda9797 3 жыл бұрын
That was the best explanation I've ever heard or seen! are you planning to code challenge the pedestrian behavior soon!
@SliveryMoonsmile
@SliveryMoonsmile 5 жыл бұрын
"We're just going to start with a blank, pre-stretched canvas with a thin coat of liquid black pixels"
@retrodragon2249
@retrodragon2249 4 жыл бұрын
of liquid crystal.
@nirmalmattmusic3993
@nirmalmattmusic3993 3 жыл бұрын
He's Bob Ross but of coding
@fedos
@fedos 3 жыл бұрын
There are no bugs, only happy accidents.
@TiffanyNg100
@TiffanyNg100 2 жыл бұрын
your energy never disaapoiint me
@XiSparks
@XiSparks 5 жыл бұрын
This is flocking awesome.
@sindreeidskrem2137
@sindreeidskrem2137 5 жыл бұрын
🎵 My vector brings all the local boids to my local position!🎶
@kodefattern
@kodefattern 5 жыл бұрын
I am still in shock and awe that you feature my little "Refactor" song :) I still love your videos, and get inspired by them :)
@kodefattern
@kodefattern 5 жыл бұрын
Actually, not trying to hijack your video or anything like that, but I've have gotten a lot of emails from people wanting me to create a full length song of the "Refactor" song. I may do that, but just to put the record straight, it was only meant as a jingleish thing for you to use in a video if you wanted to. I hope you don't mind if I extend it and make it into a real song.
@TheCodingTrain
@TheCodingTrain 5 жыл бұрын
Yes, and then we should collaborate on a music video!!! 🎵❤
@kodefattern
@kodefattern 5 жыл бұрын
@@TheCodingTrain deal!
@GodofChookie
@GodofChookie 5 жыл бұрын
@@TheCodingTrain you know you have to do it know, don't you
@twinpotatoes5513
@twinpotatoes5513 5 жыл бұрын
This is so cool! Anything to do with AI is fascinating because you can control them yet you let them be free. Do more AI related videos please :D
@ghghvfugx_gh35
@ghghvfugx_gh35 Жыл бұрын
this man is fantastic
@6Pope9
@6Pope9 3 ай бұрын
20:40 It slows them down because of the same reason why they make a parfect circle at the start (6:06). They each have a different direction so when they're aligning they take a average velocity so it cancels out.
@monocore
@monocore 8 ай бұрын
Ive learnt VEX with your tutorials. Youre amazing.
@pythonexploits8364
@pythonexploits8364 5 жыл бұрын
I would love to see an actual game development series of him, just watching him develop a larger game.
@TonyZhang01
@TonyZhang01 4 жыл бұрын
adding this strokeWeight(2); line(this.position.x, this.position.y,this.position.x+this.velocity.x*3,this.position.y+this.velocity.y*3); to the bottom of the show function let you see the direction. also, when the flock teleports when it touch the edge, the forces are not teleported which means the flocks can split in two when half teleports and half doesn't
@en3sis
@en3sis 5 жыл бұрын
Hey Dan, if you need to rename a variable just select it once then press command + f2 or command + D and rename it =)
@TheCodingTrain
@TheCodingTrain 5 жыл бұрын
Oh, great tip!
@badunius_code
@badunius_code 2 жыл бұрын
37:20 just run flock() and update() in two consecutive cycles; no need to snapshot.
@kevnar
@kevnar 4 жыл бұрын
When you're at 30 minutes in, and he says Separation is difficult, but the video ends in 12 more minutes... it's not that difficult, for Dan.
@alexlindsay9002
@alexlindsay9002 5 жыл бұрын
Something else one could do is add a little bit of randomness.... Boids aren't perfect in how they follow the rules ... so maybe they vary within a range of the ideal adjustment. Also could add some background noise...maybe perlin noise which creates a 'wind' force which acts on boids based on their position...
@lizabeti3457
@lizabeti3457 5 жыл бұрын
Awee making that without lunch You’re crazy 😂
@michaelwalker1013
@michaelwalker1013 5 жыл бұрын
Hey Dan, Just wanted to sincerely thank you! I'm still in my first year of teaching myself coding and your style of videos, "I'm gonna show you my way and explain how it works YOU (the audience) refactor it". Has helped me tremendously! I made my own Javascript "WAR" card game from scratch, No tutorials! Quick Question, at around the 19:50 mark when you copy/paste, it automatically formats all the variables for you and changes the width to height etc.. Is that a setting? is that an extension?
@TheCodingTrain
@TheCodingTrain 5 жыл бұрын
I think I am just using the default "format on save" behavior in VSCode, for more: kzbin.info/aero/PLRqwX-V7Uu6Zu_uqEA6NqhLzKLACwU74X
@michazajczyk1554
@michazajczyk1554 Жыл бұрын
It was not automatically changed while saving, but only postprocessed, and sped up by Dan. He removes repeatable things in post production so it looked like it was changed with some kind of tool/linter but it wasn't :)
@user-xr9lx9ui7l
@user-xr9lx9ui7l 7 күн бұрын
you are a gem
@gameglitcher
@gameglitcher 4 жыл бұрын
Every time your points roll over the edge they fall out of the perception. This was most noticeable during separation and your slider testing at the end, when you have some boids cross the edge and then just yeet off on their own.
@ThankYouESM
@ThankYouESM 3 жыл бұрын
Flocking Awesome!
@MrNosugarcoating
@MrNosugarcoating 5 жыл бұрын
Yesss. Back to fun stuff. Lol
@peoplethesedaysberetarded
@peoplethesedaysberetarded 5 жыл бұрын
Ah, Craig Reynolds finally shows up in the channel.
@arcadegamer6271
@arcadegamer6271 5 жыл бұрын
you is good in prgramming!!!
@prashanthkumar5987
@prashanthkumar5987 5 жыл бұрын
cool video 😁
@OonHan
@OonHan 5 жыл бұрын
Do simulations of whirlpools!
@alexanderbuk-swienty7334
@alexanderbuk-swienty7334 Жыл бұрын
Hi Dan. First of all thanks for all your stuff. I've learned tons from following along. I've improved several things on my own but am kind of stumped on the state/snapshot idea. Anyone have any pointers? Much appreciated.
@bluetype8223
@bluetype8223 5 жыл бұрын
Imagine how cool a 3d flocking environment with drones would be!
@baderalfaydi9865
@baderalfaydi9865 5 жыл бұрын
Intel already did it.
@TidBitOf
@TidBitOf 4 жыл бұрын
I don’t program in JavaScript or Processing. I program in Python,C, and R. I still learn so much from these videos though.
@KenzoArts
@KenzoArts 4 жыл бұрын
Hello Daniel! I am a big fan of you, and i would like to know if you have a code challenge where you explain in details the pixels and more specifally on how to do pixel measurement like px to inches.Thanks so much for insight content.
@gytiskau1865
@gytiskau1865 5 жыл бұрын
This was very interesting, no matter that I understood less then 1/3 of this *programing*. Not complaining or anything. Still loved it!
@OonHan
@OonHan 5 жыл бұрын
Amazing! I am your biggest fan!!!
@Thoracius
@Thoracius 4 жыл бұрын
seems like separation should act more strongly on closer boids, not uniformly across the detection radius.
@VictorRivarola007
@VictorRivarola007 3 жыл бұрын
38:40 It would be interesting if you covered how to integrate a glade GUI generator with Processing and/or with P5.js (it is available for both Java and Javascript, along with a whole different bunch of other programming languages on any platform).
@WildAnimalChannel
@WildAnimalChannel 5 жыл бұрын
Boid is New York speak for bird. Have you hoid about the boid?
@johnjackson9767
@johnjackson9767 5 жыл бұрын
Wild Animal Channel 'You sing like a boid'.
@shinershawn6053
@shinershawn6053 2 жыл бұрын
I like how he always says "from scratch" and then proceeds to use some game and graphics engine
@PritamDavis
@PritamDavis 2 жыл бұрын
@Justin batchelar agreed
@zenglingyuan6439
@zenglingyuan6439 5 жыл бұрын
Hi Daniel... can you talk about some mechanics of how computer works? I'm totally a non-cs student, but sometimes I get confusing when a sketch become slow, why our computer can run a big program but it will get slow for our little sketch?
@NatetheAceOfficial
@NatetheAceOfficial 5 жыл бұрын
Oh man, Boids is fun! I only know it from running simulations in Blender. Very cool to see it in 2D. Could we get this in P3D please!?
@TheCodingTrain
@TheCodingTrain 5 жыл бұрын
I'm hoping a viewer will implement this! (But sure I am happy to make an example.)
@kazian5453
@kazian5453 3 жыл бұрын
Hey Dan, I added an avoid collisions method to avoid all the boids from gathering into the same single point, by basically reversing the cohesion method, and only checking and applying the force if any boid is within 8 of any other boid (where distance between boids is less than 8, than apply opposite force). However, I'm running into an interesting bug with the simulation. The edge of the canvas, which we've defined behavior in the edges() method teleports the boid to the other side of the canvas. It is no longer part of the flock group that it was originally in, since this teleportation overwrites it's position, and puts it out of the perception radius of the flock group it was originally in. So if a flock group gets too close to the edge, and just skims it, the edge will cut it into two groups. Those that stayed on the one side, and those that were teleported to the opposite side. Any suggestions on how to fix this?
@TheCodingTrain
@TheCodingTrain 3 жыл бұрын
Wow, super interesting question! The discord is a great place for this discussion! thecodingtrain.com/discord
@WildAnimalChannel
@WildAnimalChannel 5 жыл бұрын
One problem is the perception radius is not wrapped around the edges.
@akhs001
@akhs001 4 жыл бұрын
This dot , this dot :) :) Nice song
@malpaca
@malpaca 5 жыл бұрын
when I do create vector with some parameter passed into it the console say that position is not a number
@SirBartolomew
@SirBartolomew 5 жыл бұрын
They're slowing down because you're averaging the speed vectors. Since they start out random, by the law of large numbers with many boids it tends to 0.
@exnerhimself
@exnerhimself 3 жыл бұрын
attaboid!
@JohnSlipper
@JohnSlipper Жыл бұрын
Great video! You could catch your syntax errors earlier in VS Code (like you do in Processing) using a JavaScript linter. I thought VS Code did it by default but you might need something like the ESLint extension installed. ...ok now I'm just realising this video is 3 years old so probably not the most useful comment now 😆
@TheCodingTrain
@TheCodingTrain Жыл бұрын
All comments are appreciated!!
@Diotallevi73
@Diotallevi73 11 ай бұрын
7:03 "How, how, how!" I don't get the reference. Back to Life by Soul 2 Soul?
@oxrock2k1
@oxrock2k1 5 жыл бұрын
I created a 3d implementation with bluejays in Unity that allows you to adjust relevant values in real time. You can play it here: oxrock.itch.io/flocking-boids I also posted the scripts on github for any who are interested: github.com/oxrock/BOIDS In my simulation the boids are incentivised to turn back when they reach the edges instead of teleporting around to the other side. This eliminates the need to monitor for boids that have already teleported but should theoretically still be in range while adding to the number of group maneuvers emerging from being constrained to a predetermined space.
@TheCodingTrain
@TheCodingTrain 5 жыл бұрын
Wow! This is amazing and thrilling to see! Would you mind adding a link here? github.com/CodingTrain/website/wiki/Community-Contributions-Guide
@oxrock2k1
@oxrock2k1 5 жыл бұрын
@@TheCodingTrain Created the pull request. Glad you like it. :)
@djsyntic
@djsyntic 2 жыл бұрын
Pausing at 13:03, personally, I would have left align generic and aligned to all boids in the list that it receives, and had perception be it's own thing that returns a list of boids. So then you would call it with align(perception(AllBoids)) This for 2 reasons: 1) Other things you might want the boid to do may also require the boids to respond to the boids it sees around it, so it saves you from need to recode the perception for each one. And 2) If you want to later change how the boids see the world, it's all in one nice area to be adjusted.
@niranjannidhi
@niranjannidhi 5 жыл бұрын
19:54 nice
@disdis6127
@disdis6127 3 жыл бұрын
How about making a video on steering bhaviours with path finding?
@rnilu86
@rnilu86 5 жыл бұрын
Fourier series wave coding? ;)
@A_Lesser_Man
@A_Lesser_Man 4 жыл бұрын
@Dan - I am really liking this boid stuff! super neat. love that i can do rudimentary AI. Your style is definitely comical and makes this learning thing much more fun, for sure! Hopefully with your teachings I can build a Boid sim worthy of discussion. On another note, I've got a small problem with something. My boids go offscreen, and won't come back (unless i do a if (this.position.x < -200) { this.position.x = width/2; } etc.). Would you, or anyone, really be willing to take a peak and perhaps point out where i've gone wrong? i'm not asking for a solution to the issue, but a pointer in the right direction, really. all of the code lives at night-stand.ca/boids/ the code in question is in the "boid.js" file. i hope it's easy to read (i try to comment, and make it easy). The three main functions are there - separation, alignment, and cohesion. added in "boundaries()" function (to replace wraparound function) ... and now boids will sometimes just fly off ... and not return without a check. Please help... press 3 to get the Boids moving fast. The issue happens fastest that way. Boids will "die" and will be "born", too. Press B then A to see LOADS of info about each and every Boid, perhaps that can help us...?
@jacobbeitner8796
@jacobbeitner8796 4 жыл бұрын
You should have let them see everyone else in the flock, but be attracted to closer ones more than ones that are farther away
@ccoodduu
@ccoodduu 3 жыл бұрын
That would be really slow
@doctorgraphics
@doctorgraphics Жыл бұрын
Could this happen on the Apple 2? That would be awesome!
@ricardoespinoza5821
@ricardoespinoza5821 5 жыл бұрын
Does someone know what the line
@JGTooooah
@JGTooooah 5 жыл бұрын
At 20:45 they're all slowing down because the average of an array of pseudorandomly generated vectors is about zero.
@Robber7
@Robber7 5 жыл бұрын
Been following your channel for years and I really enjoy it. I do have a question for you, or rather, a request. Could you make a simple 2D grid of squares and implement scaling/zooming? I've done so for a game im working on, but whenever I scale small pixel gaps appear between the tiles probably because when scaling the tiles are rendered at non integer values, thus leaving gaps. I havent figured out how to solve this, so if you could, I would love that so much! Keep up the great work my man
@wheeler2137
@wheeler2137 5 жыл бұрын
Elo tibijski wariacie
@danthiel8623
@danthiel8623 2 жыл бұрын
Interesting
@angelcaru
@angelcaru 4 жыл бұрын
Visual Studio Code auto-completes this. IFF you press TAB
@sujals7108
@sujals7108 5 жыл бұрын
Please do vortex simulation
@zohaibshafique940
@zohaibshafique940 3 жыл бұрын
why in update method , we first update position, then velocity, not other way around
@matiasvlevi6647
@matiasvlevi6647 5 жыл бұрын
Iv'e been trying to make them rotate (made them in arrows) towards where they want to go but I don't know which vector is their direction.
@jonasnicolaysen8019
@jonasnicolaysen8019 4 жыл бұрын
the speedvektor.heading() I think is the way
@nickbrighty8882
@nickbrighty8882 4 жыл бұрын
Love the videos, Dan! But I'm new to all this coding stuff, and even though I follow your coding and it all seems to work (using Sublime and Firefox), I have the grey canvas and the white boid... but it doesn't move! I check over the code and it's exactly as in the video, but nothing happens... Where might I be going wrong?!
@TheCodingTrain
@TheCodingTrain 4 жыл бұрын
Would you mind asking at discourse.processing.org/! It's a better platform for Processing and p5.js related code questions. You can share code there easily! Feel free to link from here to your post.
@nickbrighty8882
@nickbrighty8882 4 жыл бұрын
@@TheCodingTrain Thank you, I'll give it a try...
@denniskwan1906
@denniskwan1906 5 жыл бұрын
@The Coding Train just to ask what editor you're using for this project (as well as all the previous ones) Thanks in advance
@TheCodingTrain
@TheCodingTrain 5 жыл бұрын
This workflow series might help! kzbin.info/aero/PLRqwX-V7Uu6Zu_uqEA6NqhLzKLACwU74X
@denniskwan1906
@denniskwan1906 5 жыл бұрын
@@TheCodingTrain thanks
@kevnar
@kevnar 4 жыл бұрын
float GetDistWrap(float x1, float y1, float x2, float y2) { // A distance function for simulations with toroidal screen wrapping // Places a simulated x2, y2 beyond the edge of the screen // returns the distance taking into account screen wrapping // if on-screen distance is lower, returns that float fxDiff; float fyDiff; float xDiff1 = Math.abs(x1 - x2); float xDiff2 = Math.abs(x1 - (x2 - width)); if (xDiff1 < xDiff2) { fxDiff = xDiff1; } else { fxDiff = xDiff2; } float yDiff1 = Math.abs(y1 - y2); float yDiff2 = Math.abs(y1 - (y2 - height)); if (yDiff1 < yDiff2) { fyDiff = yDiff1; } else { fyDiff = yDiff2; } return (float)Math.sqrt((fxDiff * fxDiff) + (fyDiff * fyDiff)); }
@adamwisniewski5898
@adamwisniewski5898 4 жыл бұрын
thank you so very much, wiseman
@lilboat8421
@lilboat8421 5 жыл бұрын
Hi, I’m a graphic design student trying to expand my knowledge and these videos are an amazing help! Is there a way you can implement the accelerometer for cool interactions with moving your phone to move a 3D object for example in p5 js? That would be awesome
@TheCodingTrain
@TheCodingTrain 5 жыл бұрын
Yes, check out the acceleration events! p5js.org/reference/
@lilboat8421
@lilboat8421 5 жыл бұрын
The Coding Train i still came away a bit confused.. could you make a video about how to include the physics engine with the accelerometer? Thanks
@yannmassard3970
@yannmassard3970 Жыл бұрын
hello, you can do the flocking alg by referencing the 7 closets boids around, wich allows avoidance to feel more natural. According to reseaches, birds and fishes follow 3 rules to flock : 1 align speed with 7 boids- 2 If one of the 7 changes direction, you change -3 avoid getting too close to any boid among those 7. I saw a documentary about it 17 years ago and reproduced it back then. Works like a charm, and can handle a shit ton of birds. In order to optimize the distance calculation between boids (to avoid square loops ) you can use a grid space alg. Oh and something else, bird vision that flock (like pigeons) is almost 300d horizontally and 150 v, so making a spherical perception isnt going to cause any non natural behavior, the V shape has nothing to do with vision, they fly in V shape because of motivation, they always follow someone, like cyclists in line, that s why they actually rotate their positions over time.
@Flackon
@Flackon 5 жыл бұрын
IT would have been nice to draw them as pointed triagles though. I wonder what the logic for that looks like
@TheCodingTrain
@TheCodingTrain 5 жыл бұрын
You can find that code at github.com/shiffman/The-Nature-of-Code-Examples-p5.js/tree/master/chp06_agents/NOC_6_09_Flocking
@vixtorocha
@vixtorocha 5 жыл бұрын
Dan!!! How do you find these code challeges? I want to attempt them myself!
@TheCodingTrain
@TheCodingTrain 5 жыл бұрын
They get suggested here! github.com/CodingTrain/Rainbow-Topics/issues
@Xnichfrytk
@Xnichfrytk 5 жыл бұрын
33:56 are we really not addressing how the flock formed a crystal/lattice ????
@Sccarlettex
@Sccarlettex 5 жыл бұрын
I promise there will come a time in which I’ll understand half of what he did in this video
@TheCodingTrain
@TheCodingTrain 5 жыл бұрын
I believe in you!
@Sccarlettex
@Sccarlettex 5 жыл бұрын
The Coding Train aw thank you!
@mattshnoop
@mattshnoop 5 жыл бұрын
Hey Dan, I asked this question before, but got no answer. Why do you use VS Code now, instead of Atom? What does it offer that Atom does not? I've just gotten comfortable with Atom thanks to you, but now I'm wondering if I should be using VS Code instead!
@TheCodingTrain
@TheCodingTrain 5 жыл бұрын
To be honest I don't see a clear advantage of one over the other. I do see more of my students using VSCode these days so I wanted to try to learn and get used to it. I don't think you need to worry too much, both will work for all the things I do in my videos!
@mattshnoop
@mattshnoop 5 жыл бұрын
@@TheCodingTrain Thanks :) I was worried that maybe there was one 'killer feature' of Code I was missing out on. Incidentally, this was one of my favourite Coding Challenges so far! Thanks for the great content.
@shivamchhetri9893
@shivamchhetri9893 5 жыл бұрын
my reason is integrated terminal in VS code.
Coding Challenge 125: Fourier Series
28:47
The Coding Train
Рет қаралды 580 М.
Simulation Coding Challenge #1: Waves
27:37
Mr. P Solver
Рет қаралды 15 М.
Glow Stick Secret 😱 #shorts
00:37
Mr DegrEE
Рет қаралды 119 МЛН
原来小女孩在求救#海贼王  #路飞
00:32
路飞与唐舞桐
Рет қаралды 65 МЛН
ISSEI funny story😂😂😂Strange World | Pink with inoCat
00:36
ISSEI / いっせい
Рет қаралды 25 МЛН
I learned to code from scratch in 1 year. Here's how.
41:55
Thomas Frank
Рет қаралды 307 М.
Coding Challenge 180: Falling Sand
23:00
The Coding Train
Рет қаралды 707 М.
Coding Adventure: Boids
8:35
Sebastian Lague
Рет қаралды 1,5 МЛН
Neat AI does Predator Boids
8:16
Neat AI
Рет қаралды 132 М.
Coding Challenge #59: Steering Behaviors
30:56
The Coding Train
Рет қаралды 186 М.
Just Boids | Useless Game Dev
12:10
Useless Game Dev
Рет қаралды 54 М.
Coding Challenge 183: Paper Marbling Algorithm
32:10
The Coding Train
Рет қаралды 53 М.
Coding Challenge #64.2: Inverse Kinematics
36:45
The Coding Train
Рет қаралды 397 М.
Flocking Simulation: Boids (JavaScript)
10:42
SimonDev
Рет қаралды 20 М.
Glow Stick Secret 😱 #shorts
00:37
Mr DegrEE
Рет қаралды 119 МЛН