103
u/koenigsbier May 08 '22
And this is your first Rust program? I feel ashamed of myself now
35
u/racnanCode May 08 '22
Haha thanks. I finished the book and was immediately drawn to make this
22
u/koenigsbier May 08 '22
Congratulations for this work. I just finished chapter 12 but for sure my first Rust program will be something much easier than yours
3
u/enokeenu May 08 '22
Which book?
46
u/koenigsbier May 08 '22
3
u/enokeenu May 08 '22
I did not see GUI stuff in the book.
8
u/koenigsbier May 09 '22
The book won't hold your hand forever, at some point you should walk by yourself like a grown up 😉
24
u/roberte777 May 08 '22
Once you’ve done the same in another language, it’s not such a crazy feat to do something like this as your first. Don’t feel ashamed, I bet OP has a lot of experience! You’ll get there!
4
u/koenigsbier May 08 '22
Thanks for your support. I do hope one day this kind of exercise becomes a cup of tea for me 😛
4
May 08 '22
You just made us all feel like absolute losers
That aside, very well done
9
u/racnanCode May 08 '22
You just made us all feel like absolute losers
Hey, if you look at the code, it pretty simple. You can also make same from scratch.
Pseudo code http://www.kfish.org/boids/pseudocode.html
Coding train video https://youtu.be/mhjuuHl6qHM
This guy implements in javascript, you can follow along and do the same in rust.
That aside, very well done
Thanks.
3
u/bitmejster May 09 '22
I’m disappointed you didn’t let us see the entire zombie apocalypse play out
1
u/racnanCode May 09 '22
Haha, sorry wanted to keep the video short and concise.Feel free to clone the repo and try it out on your own.
3
u/ke43az May 08 '22
Hi! I am amazed, I just have a few questions, also I am a beginner in programming so I am sorry if I offend anyone.
First of all, I just started learning programming a few months ago. I started with solidity and got the basics of it, but now I have switched to rust as I was offered an opportunity to learn it the following months to work for someone as a junior.
How would a beginner learn to do something like this? I went through the code, and while I could understand some of it, it boggles my mind how would someone start something like this from scratch. Obviously, if I just copy the code in my editor, it won't work, another question is, why is that?
I am learning Rust in order to develop smart contracts on it, I would really appreciate some insights, as seeing people doing these programs motivates me, but I also fall prey to the fear of not being good enough. Sorry if I offended anyone, just looking for some advice.
6
u/racnanCode May 08 '22
How would a beginner learn to do something like this?
Tutorials and practice. I watched a lot of coding train videos.
Obviously, if I just copy the code in my editor, it won't work, another question is, why is that?
It should work. What errors are you getting?
In general, my two cents would be to watch some follow along videos and actually follow along. Then try to implement something on your own.
4
May 08 '22
It should work. What errors are you getting?
Probably didn’t add the dependencies to the Cargo.toml
9
u/JoJoJet- May 08 '22
Ooh this is such a good idea for a first rush program! Not trivial but not super hard either, and once it's done it feels like you've made something really cool. I'm gonna steal this next time a friend asks for help learning Rust.
9
4
12
3
3
3
u/drewsiferr May 08 '22
This is really cool!
One nit-pick, starting when you add predators, your mouse pointer is in the frame.
3
u/Dimitri_3gg May 09 '22
Now do 3d >:)
Fr tho, gj. I've wanted to do the same since watching coding train and Neat AI.
2
2
1
1
u/Xatraxalian May 09 '22
This is cool. I've watched many Coding Train video's because of the Nature of Code stuff. I implemented Smart Rockets a few years ago and wrapped it into an Electron app when looking into making web-apps run on the desktop.
I've always wanted to re-implement this in Rust but never found a framework to do the graphics with. Nannou seems to be good for this. I'll give it a shot some day :)
(I've implemented my own chess engine in Rust as my first Rust program and wrote some other command-line stuff, but never did anything with graphics or GUI's yet; which is what I would actually need if I wanted to write a GUI for said engine and not use something heavy like Electron.)
-10
u/Eorika May 08 '22
Neat. Some observations: the Predator/Prey ought to be a boolean since there are only two states. You don't appear to make any use of match syntax so the enum doesn't add any value.
5
34
u/abstractionsauce May 08 '22
Is there any benefit to doing so? Enum is much clearer, there is no clear true/false relationship with predator/prey.
15
u/Eorika May 08 '22 edited May 08 '22
It's a better decision to use an
enumin terms of design since it can be built upon later, and yeah, you might add, idk,Observerto the list. But from what I could see in this code, it was used inifstatements without the use ofmatch, so it's as if there's a contradiction and it's not being used with the intention of being expanded later. So a bool attribute ofprey, withif x.prey { ... }being used as opposed toif x.type == Type::Preymakes more sense.5
u/abstractionsauce May 08 '22
A convincing argument, thanks for taking the time. Boolean more concise and a well named variable makes it pretty clear what’s what.
7
u/Eorika May 08 '22
Hadn't seen all the downvotes haha - that was all the feedback I could come up with after a short read, bit pedantic I suppose but I appreciate feedback after sharing some code.
2
u/robin-m May 08 '22
However the reverse is much more obtuse.
if x.type == Type::Predatoris clearer thanif !x.prey.1
u/IceSentry May 08 '22
They mentioned this being their first rust project, so it's possible they didn't think of using match.
2
6
May 08 '22
Eh idk. An enum makes it explicit what is what, which, with the frequent use of newtypes and "microtypes" in rust seems to be the preferred style. I guess having some field called "prey" makes it more concise, but it also makes it easier to pass the wrong thing into a function
61
u/racnanCode May 08 '22
https://github.com/racnan/rust-flocking-simulation