r/Python • • Jan 17 '20

I made metaballs. Works quite fast. https://github.com/Norne9/metaballs

1.1k Upvotes

64 comments sorted by

33

u/[deleted] Jan 17 '20

What is the purpose of line 124?

fps = fps * 0.97 + 1000.0 / max(dt, 1) * 0.03

If you wanted the fps you could just use clock.get_fps().

37

u/Norn9 Jan 17 '20

Oh, I did not know that. That line calculates something like average fps.

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

u/kurti256 Jan 18 '20

what is JIT?

1

u/[deleted] Jan 18 '20

just in time compilation

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=True then numba.prange automatically execute you loop in parallel. If parallel=False then it works exactly like range

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 / max Result will be (255, 136, 225)

1

u/Miss_pechorat Jan 18 '20

THX! thats very handy to know, and very usefull too ;)

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

u/SV-97 Jan 18 '20

What do you mean?

2

u/appsplaah Jan 18 '20

Pyhon Beginner: Awesome! Mind telling the framework used? Thanks for the Hope:))

2

u/Norn9 Jan 18 '20

I used pygame for rendering, numpy for computing and numba to make it faster

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

u/agree-with-you Jan 18 '20

I agree, this does seem possible.

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

u/NoLongerUsableName import pythonSkills Jan 18 '20

He even made it a link!

5

u/kurti256 Jan 18 '20

Thank you

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

u/admiralspark Jan 18 '20

I did too haha.

13

u/thundercloudtemple Jan 18 '20

Samesies

8

u/ElysiumUS Jan 18 '20

I too am here to report HumanInterpretationError 'meatball'

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

u/got_outta_bed_4_this Jan 18 '20

I saw "meat-a-balls"

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

u/cube-drone Jan 17 '20

That's a spicy metaball!

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

u/[deleted] Jan 18 '20

I’m on my phone but I’m really curious what lib you used for the visualisation?

2

u/Norn9 Jan 18 '20

I used pygame

1

u/stef13013 Jan 18 '20

Nice work... Why using numpy instead of a "good old class" for a ball ?

1

u/Norn9 Jan 18 '20

Its easier than using classes with numba

1

u/stef13013 Jan 18 '20

Ok, may be faster too...

6

u/usbafchina Jan 17 '20

These don't look like meatballs.

Oh.

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

u/[deleted] 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

u/[deleted] Jan 17 '20

pygame 1.9.6

Hello from the pygame community. https://www.pygame.org/contribute.html

5

u/Norn9 Jan 17 '20

Yep, it should print only that. I don't know whats wrong.

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.dev6

Everything 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

u/LyLyV Jan 17 '20

Metaballs at a rave, going around kissing everyone, LOL XD

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

u/babar77 Jan 18 '20

Nicely done

1

u/Joker042 Jan 18 '20

Your collisions are awesome :)

1

u/pslayer89 Jan 18 '20

Good job! Now do this in 3D! 😉

1

u/zacharius_zipfelmann Jan 18 '20

Nice, but WILL THEY HIT THE CORNER OF THE SCREEN

1

u/stef13013 Jan 19 '20

How did you render the "melt" between balls ??

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

u/coderboii Jan 19 '20

reminds me of that bubbles screensaver from Windows

1

u/MiniDaftHen0 Jan 17 '20

thats pre cool

0

u/[deleted] Jan 18 '20

[deleted]

1

u/Norn9 Jan 18 '20

Explained there