r/Python • u/Norn9 • Jan 17 '20
I made metaballs. Works quite fast. https://github.com/Norne9/metaballs
13
u/bionicdna Jan 17 '20
This would be an interesting thing to port over to Julia, since it looks like you're trying to optimize with JIT through numba. Not sure how one would do the graphics yet.
11
u/Norn9 Jan 17 '20 edited Jan 17 '20
I think it will work better. If you remove jit decorator it will take about a minute to render single frame.
9
u/bionicdna Jan 17 '20
If I find a good visualization alternative I'd like to fork it, since this is really neat :)
1
9
u/MattR0se Jan 17 '20
Can you explain how you utilize CORES = os.cpu_count() in the draw_balls function? Does numba.prange automatically invoke multiprocessing?
9
u/Norn9 Jan 17 '20
Yes. If you set
parallel=Truethennumba.prangeautomatically execute you loop in parallel. Ifparallel=Falsethen it works exactly likerange
3
u/Miss_pechorat Jan 17 '20
How do the colours mix?, Is it a combo of two rgb's ?
2
u/Norn9 Jan 18 '20
I just add up the colors, normalized if some component bigger than 255.
For example take some pixel affected by 2 metaballs. Say one metaball have color (200, 130, 15) in this place, and other (100, 30, 250). I need to sum them (300, 160, 265) and use next formula
color = color * 255 / maxResult will be (255, 136, 225)1
2
u/LovesGettingRandomPm Jan 18 '20
Does python lack object property names? Why are those abstracted by numbers, if that comment wasn't there I'd have no clue.
2
u/Norn9 Jan 18 '20
No, you can use classes in python. It's just not easy to use array of classes with numba.
1
2
u/appsplaah Jan 18 '20
Pyhon Beginner: Awesome! Mind telling the framework used? Thanks for the Hope:))
2
2
u/kurti256 Jan 18 '20
If I use pyinstaller I think I can make it in to an exe no python download required to use it
1
u/Norn9 Jan 18 '20
It is possible, but since I used numpy, its size will be more than 200 mb. Or am I doing something wrong?
1
1
u/kurti256 Jan 18 '20
I'm not sure I'm a novice that knows a few stuff but not how to do it or what it entails for the most part Edit: maths I'm good at I'll have a look let me boot up the wiki and the pc 😊
1
u/kurti256 Jan 18 '20
It opens and instantly closes (when it is an exe) I don't know what I messed up
1
u/kurti256 Jan 18 '20
I edited the code to allow custom width and height
print ("recommended height is 800 by 600") userhight = input("Height?: ") WIDTH = int(userwidth) HEIGHT = int(userhight)
67
u/DanGee1705 Jan 17 '20
https://github.com/Norne9/metaballs for those who are lazy like me
16
u/Smobey Jan 18 '20
Considering you went through all the trouble to copy paste it from the title and post it here for all of us... I don't think you're lazy at all! You big cheat!
1
5
111
u/averageJoe576 Jan 17 '20
Shoulda called them meetballs
62
u/infinit_e Jan 18 '20
I read the title as “meatballs” and was really confused. I should probably go get dinner...
16
1
u/I_BA1LEY_I Jan 18 '20
Same here. I wondered how you would code a computer to cook dinner though so clicked the link xD
1
33
u/fiskfisk Jan 17 '20
Instead of making them bounce of each other just make them flow over the screen, affecting each other as they flow past. It'll look (even) better :-)
11
1
u/UltraBoss123 Jan 18 '20
How did you get the balls to glow?
2
u/Norn9 Jan 18 '20
It's just metaballs algorithm. If you don't clip values less than 1, the balls will glow. But it will be too bright, I reduced glow using next formula
value = value * value / 2
1
1
u/stef13013 Jan 18 '20
Nice work... Why using numpy instead of a "good old class" for a ball ?
1
6
3
u/fm75 Jan 18 '20
Nice! Brings back a lot of memories :)
In the '90s this kind of effects were pretty popular in several productions made by demo-sceners... Metaballs (and vector balls, and plasma clouds...) were pretty abused and at the time you could only rely on integer math of 386 processors running at 16Mhz with 1MB of RAM - single core of course.
There are plenty of effects that you can try to replicate (at the time they were spectacular given the limitations of the hardware. See https://www.youtube.com/watch?v=17Bl4xf1BAg )
Today, thanks to the heavy lifting done by GPUs, things have changed a bit https://www.youtube.com/watch?v=lkhDR91TH9M but still, the way they managed to create those little pieces of art is still impressive even after 30 years.
Of course Python didn't exist at the time and everything was a huge land of C and Assembly code kept jealously secret - like real magician. Eventually the code for Second Reality was released after 20 years if you're curious: https://github.com/mtuomi/SecondReality)
Happy coding!
1
Jan 17 '20
I'm on a Mac. Is there anything I need to do after the window appears? Nothing seems to happen.
2
u/Norn9 Jan 17 '20
No, it should work after start. It takes some time to load. Did it print something to console?
2
3
u/socialistvegan Jan 19 '20 edited Jan 19 '20
Edit: Figured it out, looks like pygame 1.9.6 is incompatible with Python 3.8.1, had to run
pip install pygame==2.0.0.dev6Everything now works!
I'm also on a Mac, and I don't even get a window to appear. This is all I see in console:
% python3 metaballs.py pygame 1.9.6 Hello from the pygame community. https://www.pygame.org/contribute.html metaballs.py:120: DeprecationWarning: an integer is required (got type float). Implicit conversion to integers using __int__ is deprecated, and may be removed in a future version of Python. screen.blit(text, (WIDTH / 2 - text.get_width() / 2, HEIGHT / 2 - text.get_height() / 2))
4
2
u/truespirit89 Jan 18 '20
I've seen this post since it's been up and have read it as meatballs every single time. Holy shit.
1
1
1
1
1
1
u/stef13013 Jan 19 '20
What is the purpose of : # calculate value dx, dy = x - bx, y - by light = radius * radius / (dx * dx + dy * dy)
1
1
1
0
33
u/[deleted] Jan 17 '20
What is the purpose of line 124?
If you wanted the fps you could just use
clock.get_fps().