A lightning effect?

Blitz3D Forums/Blitz3D Beginners Area/A lightning effect?

JBR(Posted 2009) [#1]
Hi, any pointers on how to draw a lightning bolt effect between 2 points in 2D.

Ideally it would 'wrap around' after so many frames.

Thanks
Jim


Stevie G(Posted 2009) [#2]
Looks more like electricity but it's a start ...

Graphics 640,480,16,1

SetBuffer BackBuffer()

While Not KeyHit(1)

	Cls

	DRAWlightning( GraphicsWidth() *.5, GraphicsHeight() *.5, MouseX(), MouseY(), 10, 20 )
	
	Text MouseX(), MouseY(), "X", 1, 1
	
	Flip

Wend

;======================================================================
;======================================================================
;======================================================================

Function DRAWlightning( Sx, Sy, Fx, Fy , Segs, MaxWidth )

	;get distance
	Dx# = Fx - Sx
	Dy# = Fy - Sy
	Di# = Sqr( Dx * Dx + Dy * Dy )

	;normalize direction vector	
	Nx# = Dx / Di
	Ny# = Dy / Di
	
	;start point
	Lx = Sx
	Ly = Sy
	
	For l = 1 To Segs
	
		Width# = Rand( -MaxWidth , MaxWidth )
		Px = Sx + ( l * Dx ) / Segs + -Ny * Width
		Py = Sy + ( l * Dy ) / Segs + Nx * Width
		Line Lx, Ly, Px, Py
		Lx = Px
		Ly = Py
		
	Next
	
End Function



Nate the Great(Posted 2009) [#3]
if you dont want your lightning to get squished or stretched over longer or shorter distances try this... only 3 or 4 lines added/changed

Graphics 640,480

SetBuffer BackBuffer()

While Not KeyHit(1)

	Cls

	DRAWlightning( GraphicsWidth() *.5, GraphicsHeight() *.5, MouseX(), MouseY(), 1, 20 )
	
	Text MouseX(), MouseY(), "X", 1, 1
	
	Flip

Wend

;======================================================================
;======================================================================
;======================================================================

Function DRAWlightning( Sx, Sy, Fx, Fy , Segs, MaxWidth )	;segs is now segs per 10 pixels

	;get distance
	Dx# = Fx - Sx
	Dy# = Fy - Sy
	Di# = Sqr( Dx * Dx + Dy * Dy )
	
	scnt = Int(di/10)
	segs = segs * scnt
	;normalize direction vector	
	Nx# = Dx / Di
	Ny# = Dy / Di
	
	;start point
	Lx = Sx
	Ly = Sy
	
	For l = 1 To Segs
	
		Width# = Rand( -MaxWidth , MaxWidth )
		Px = Sx + ( l * Dx ) / Segs + -Ny * Width
		Py = Sy + ( l * Dy ) / Segs + Nx * Width
		Line Lx, Ly, Px, Py
		Lx = Px
		Ly = Py
		
	Next
	
End Function