Code archives/Graphics/Fast 2D Blobby Objects/Metaballs in Max2D

This code has been declared by its author to be Public Domain code.

Download source code

Fast 2D Blobby Objects/Metaballs in Max2D by ImaginaryHuman2005
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.
'Blobby objects with BlitzMax using Max2D only

'Some special numbers
Local ballsize:Int=512
Local ballsizehalf:Int=ballsize/2

'Set up the display
Graphics 800,600,0
Cls

'Work out what the dividers needs to be
Local balldivider:Float
If ballsize=128 Then balldivider=64 '8x8
If ballsize=256 Then balldivider=256 '16x16
If ballsize=512 Then balldivider=1024 '32x32
Local lineardivider:Float
If ballsize=128 Then lineardivider=0.5
If ballsize=256 Then lineardivider=1
If ballsize=512 Then lineardivider=2

'Render the gradient image
For Local r:Float=1 To ballsize-1 Step 0.5
	Local level:Float=r
	level:*level
	level=level/balldivider
	SetColor level,level,level 'For blobby gradient shape
	'SetColor r/lineardivider,r/lineardivider,r/lineardivider 'For linear gradients
	DrawOval r/2,r/2,ballsize-r,ballsize-r
Next

'Turn it into an image
AutoMidHandle True
Local img:TImage=CreateImage(ballsize,ballsize,1,FILTEREDIMAGE)
GrabImage(img,0,0,0)

'Set the drawing mode
SetBlend LIGHTBLEND

'Keep drawing the image until you press Escape
Repeat
	Cls
	DrawImage img,400,300
	DrawImage img,MouseX(),MouseY()
	Flip
Until KeyHit(KEY_ESCAPE)

Comments

drnmr2005
you can create green blobs by changing the line SetColor level,level,level to SetColor level,r/lineardivider,level.


Code Archives Forum