January 26, 2021, 06:03:30 AM

### Author Topic: [bmx] Fast 2D Blobby Objects/Metaballs in Max2D by ImaginaryHuman [ 1+ years ago ]  (Read 524 times)

#### BlitzBot

• Jr. Member
• Posts: 1
##### [bmx] Fast 2D Blobby Objects/Metaballs in Max2D by ImaginaryHuman [ 1+ years ago ]
« on: June 29, 2017, 12:28:38 AM »
Title : Fast 2D Blobby Objects/Metaballs in Max2D
Author : ImaginaryHuman
Posted : 1+ years ago

Description : This program shows how to generate an energy field image which can then be used to render blobby objects on the screen. Since it works using images it is highly optimized by hardware acceleration of your graphics card, so is very fast. It also allows any number of blobby objects to be added and still produces the same results. This example shows the basic technique of drawing metaballs. See my other submission for how to turn the final image into something more useful.

Code :
Code: BlitzMax
1. 'Blobby objects with BlitzMax using Max2D only
2.
3. 'Some special numbers
4. Local ballsize:Int=512
5. Local ballsizehalf:Int=ballsize/2
6.
7. 'Set up the display
8. Graphics 800,600,0
9. Cls
10.
11. 'Work out what the dividers needs to be
12. Local balldivider:Float
13. If ballsize=128 Then balldivider=64 '8x8
14. If ballsize=256 Then balldivider=256 '16x16
15. If ballsize=512 Then balldivider=1024 '32x32
16. Local lineardivider:Float
17. If ballsize=128 Then lineardivider=0.5
18. If ballsize=256 Then lineardivider=1
19. If ballsize=512 Then lineardivider=2
20.
22. For Local r:Float=1 To ballsize-1 Step 0.5
23.         Local level:Float=r
24.         level:*level
25.         level=level/balldivider
26.         SetColor level,level,level 'For blobby gradient shape
27.         'SetColor r/lineardivider,r/lineardivider,r/lineardivider 'For linear gradients
28.         DrawOval r/2,r/2,ballsize-r,ballsize-r
29. Next
30.
31. 'Turn it into an image
32. AutoMidHandle True
33. Local img:TImage=CreateImage(ballsize,ballsize,1,FILTEREDIMAGE)
34. GrabImage(img,0,0,0)
35.
36. 'Set the drawing mode
37. SetBlend LIGHTBLEND
38.
39. 'Keep drawing the image until you press Escape
40. Repeat
41.         Cls
42.         DrawImage img,400,300
43.         DrawImage img,MouseX(),MouseY()
44.         Flip
45. Until KeyHit(KEY_ESCAPE)