The Lightning Thread (cont. from Electric Arcs)

Game development specific discussions.
Post Reply
anonymous1337
Posts: 5494
Joined: Sep 12, 2005 20:06
Location: California

The Lightning Thread (cont. from Electric Arcs)

Post by anonymous1337 »

Cont'd from: http://freebasic.net/forum/viewtopic.php?f=7&t=23502

Purpose: Place for people to store lightning, electricity, spark, etc.-related demos and algorithms. Unlike "temporary" Tips & Tricks or General forum threads, posts in this sort of forum would be effectively "permanently stickied" and related to a single topic. Anything that branches off of that topic would also be "permanently stickied" and get its own thread. (Ex: A lightning thread may discourse into a magnetism thread.)

Motive: To demonstrate the kind of thread that could exist in an algo/demo-specific sub-forum.

Anyone feel like joining in?
DeNivra
Posts: 33
Joined: Jan 23, 2015 17:25

Re: The Lightning Thread (cont. from Electric Arcs)

Post by DeNivra »

@ anonymous1337
this I a great idea. Been wanting something like this for so long
However instead of lightning threads may I suggest a more general thread such as SFX code or game physics which will include sub topics like lightning, explosions, collisions, smoke effects, laser effects, bullet physics, blah, blah.
anonymous1337
Posts: 5494
Joined: Sep 12, 2005 20:06
Location: California

Re: The Lightning Thread (cont. from Electric Arcs)

Post by anonymous1337 »

DeNivra wrote:@ anonymous1337
this I a great idea. Been wanting something like this for so long
However instead of lightning threads may I suggest a more general thread such as SFX code or game physics which will include sub topics like lightning, explosions, collisions, smoke effects, laser effects, bullet physics, blah, blah.
Do you want a thread that encompasses the entire idea of this thread, instead?: http://freebasic.net/forum/viewtopic.php?f=17&t=23476

That's what I'm going for, but I don't know what everyone else wants yet.

I do understand wanting to discuss all of those programming topics, and it's disappointing we don't have a subforum for it. I do think a single thread would become quickly cluttered and difficult to manage, but perhaps it would spur creativity? I'll create one to see if anyone posts in it.

Personally, I think a subforum would be better because it provides a valid reason to have that instead of just posting in Tips & Tricks.
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: The Lightning Thread (cont. from Electric Arcs)

Post by dodicat »

anonymous1337 wrote :
Purpose: Place for people to store lightning, electricity, spark,

Do you mean some sort of capacitor for static (not UDT arrays) of course, there is a separate place for them.

Code: Select all


#define Intrange(f,l) int(Rnd*((l+1)-(f))+(f))
#define da rnd*90-rnd*90
#define dl rnd*20
sub drawline(x as integer,y as integer,angle as double,length as double,col as uinteger,byref x2 as integer=0,byref y2 as integer=0)
    angle=angle*.0174532925199433 
     x2=x+length*cos(angle)
     y2=y-length*sin(angle)
     for k as integer=-2 to 2
     line(x,y+k)-(x2,y2+k),col
     next k
end sub

sub wire(x1 as integer,y1 as integer,x2 as integer)
    dim as single r=10,pi=4*atn(1),count,min,max
    for z as single=x1 to x2-2*r step 2*r
        count+=1
        if count mod 2 then min=0:max=pi else min=pi:max=2*pi
        circle(z,y1),r,rgb(200,200,200),min,max
        next z
    end sub

sub spark
    static as single inc
    inc+=.1
    dim as integer x2=200,y2=300+100*sin(inc),x,y
        do
drawline(x2,y2,da,dl,rgb(200,200,255),x,y)
x2=x
y2=y
loop until x>570
end sub

screen 19,32,,64

do

screenlock
line(0,0)-(799,599),rgba(0,0,0,150),bf
'===============================
wire(0,300,200)
line(190,50)-(200,550),rgb(255,255,255),b
spark
line(580,50)-(590,550),rgb(255,255,255),b
wire(600,300,800)
screenunlock
sleep 50,1
loop until len(inkey)
sleep
 
anonymous1337
Posts: 5494
Joined: Sep 12, 2005 20:06
Location: California

Re: The Lightning Thread (cont. from Electric Arcs)

Post by anonymous1337 »

Haha, dodicat. Good one. No, I meant literally extending Zamaster's thread into a new, dedicated thread on lightning effects :)
DeNivra
Posts: 33
Joined: Jan 23, 2015 17:25

Re: The Lightning Thread (cont. from Electric Arcs)

Post by DeNivra »

@ dodicat
Do you mean some sort of capacitor .....
Using Capacitors is imaginary. It makes things complex. I would prefer it real
Dr_D
Posts: 2451
Joined: May 27, 2005 4:59
Contact:

Re: The Lightning Thread (cont. from Electric Arcs)

Post by Dr_D »

Hey guys. Here's something. It's kind of lengthy for what it actually does though. I made if for that Dr. Mudball game.
Anyway, its kind of like a plasma ball, if you've ever seen one. You can click/hold in a certain spot and the arcs will gravitate towards that area. ;)

Code: Select all

#include "fbgfx.bi"

const pi=3.1415926, pi2 = pi*2


type vec2f
    x as single
    y as single
    
    declare constructor () 
    declare constructor ( byval x as single, byval y as single )
    
    declare function distance( byref v as vec2f ) as single
    declare function dot ( byref v as vec2f ) as single
end type

constructor vec2f ()
end constructor


constructor vec2f ( byval x as single, byval y as single )
    
	this.x = x
	this.y = y
    
end constructor


function vec2f.distance( byref v as vec2f ) as single
    
	return sqr((v.x - this.x)^2 + (v.y - this.y)^2)
    
end function

function vec2f.dot ( byref v as vec2f ) as single
    
    return  this.x * v.x + this.y * v.y
    
end function


operator + ( byref lhs as vec2f, byref rhs as vec2f ) as vec2f
    
	return type<vec2f>( lhs.x + rhs.x, lhs.y + rhs.y )
    
end operator


operator - ( byref lhs as vec2f, byref rhs as vec2f ) as vec2f
    
	return type<vec2f>( lhs.x - rhs.x, lhs.y - rhs.y )
    
end operator


operator * ( byref lhs as vec2f, byref rhs as single ) as vec2f
    
	return type<vec2f>( lhs.x * rhs, lhs.y * rhs )
    
end operator 


operator / ( byref lhs as vec2f, byref rhs as single ) as vec2f
    
    return type<vec2f>(lhs.x / rhs, lhs.y / rhs)
    
end operator


screenres 640,480,32,,FB.GFX_ALPHA_PRIMITIVES

type lstruct
    a as single
    p as vec2f
    c as integer = 1
end type

dim as integer mx, my, mb
dim as integer steps = 32
dim as integer bolts = 32
dim as single radius = 128
dim as vec2f posit = vec2f(320f,240f)


dim as double this_time

dim as lstruct rpoint(1 to bolts, 1 to steps)

do
    this_time = timer
    getmouse(mx,my,,mb)
    dim as vec2f ms = vec2f(mx,my)

    screenlock
    cls
    
    for i as integer = 1 to bolts
        
        dim as single rangle = rnd*pi2
        rpoint(i,1).a = rangle
        rpoint(i,1).p = posit + vec2f(radius/5*sin(rangle), radius/5*cos(rangle) )
        
        dim as integer modify

        if mb and FB.BUTTON_LEFT and mx>-1 then
            'if (rpoint(i,1).p-posit).dot(ms-posit)>0 then
                modify = -1
            'end if
        end if

        for r as integer = 2 to steps
            dim as single nr = radius/steps
            rangle = rpoint(i,r-1).a -.75+rnd*1.5
            rpoint(i,r).a = rangle
            rpoint(i,r).p = rpoint(i,r-1).p + vec2f(nr*sin(rangle), nr*cos(rangle) )
            
            if modify then 
                dim as single dist = rpoint(i,r).p.distance(ms)
                dim as vec2f dvec = (ms-rpoint(i,r).p)/dist
                rpoint(i,r).p+= dvec*2f
            end if

            dim as single bcheck = ((rpoint(i,r).p.y - posit.y)^2) + ((rpoint(i,r).p.x - posit.x)^2)
            if bcheck>radius^2 or r = steps then
                bcheck =  sqr(bcheck)
                rpoint(i,r).p = posit + ( rpoint(i,r).p - posit ) / bcheck * radius
            end if
        next
        
    next
    
    for i as integer = radius to radius/5 step -1
        circle(posit.x, posit.y), i, rgb(0,0,255-(255*((i-1)/radius))),,,,f
    next

    dim as integer c
    for i as integer = 1 to bolts
        for r as integer = 2 to steps
            c = 255-(255*(r/steps))
            line(rpoint(i,r-1).p.x, rpoint(i,r-1).p.y)-(rpoint(i,r).p.x, rpoint(i,r).p.y),rgba(255,255,255,c)
        next
    next
    
    circle(posit.x, posit.y),radius/5,rgba(255,255,255,200),,,,f

    screensync
    screenunlock
    
    sleep 3,1

loop until multikey(fb.sc_escape)

sleep
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: The Lightning Thread (cont. from Electric Arcs)

Post by BasicCoder2 »

Had a bit of a play with your code. Added a thickLine. Dodicat has a shorter thickLine. Because you create and store each bolt before display you can redraw it more than once at reducing sizes to produce a glow effect.

Code: Select all

#include "fbgfx.bi"

const pi=3.1415926, pi2 = pi*2
sub thickLine(x1 As Integer,y1 As Integer,x2 As Integer,y2 As Integer,size As Integer,c As UInteger)
    dim as integer x,y
  if x1 = x2 and y1 = y2 then
      circle (x1, y1), size, c, , , , f        
  elseif abs(x2 - x1) >= abs(y2 - y1) then
    dim K as Single = (y2 - y1) / (x2 - x1)
    for I as Integer = x1 To x2 step sgn(x2 - x1)
        x = I
        y = K * (I - x1) + y1
        circle (x,y), size, c, , , , f
    next I
  else
    dim L as Single = (x2 - x1) / (y2 - y1)
    for J as Integer = y1 To y2 step sgn(y2 - y1)
        x = L * (J - y1) + x1
        y = J
        circle (x,y), size,c,,,,f
    next J
  end if
end sub

type vec2f
    x as single
    y as single
    
    declare constructor () 
    declare constructor ( byval x as single, byval y as single )
    
    declare function distance( byref v as vec2f ) as single
    declare function dot ( byref v as vec2f ) as single
end type

constructor vec2f ()
end constructor


constructor vec2f ( byval x as single, byval y as single )
    
    this.x = x
    this.y = y
    
end constructor


function vec2f.distance( byref v as vec2f ) as single
    
    return sqr((v.x - this.x)^2 + (v.y - this.y)^2)
    
end function

function vec2f.dot ( byref v as vec2f ) as single
    
    return  this.x * v.x + this.y * v.y
    
end function


operator + ( byref lhs as vec2f, byref rhs as vec2f ) as vec2f
    
    return type<vec2f>( lhs.x + rhs.x, lhs.y + rhs.y )
    
end operator


operator - ( byref lhs as vec2f, byref rhs as vec2f ) as vec2f
    
    return type<vec2f>( lhs.x - rhs.x, lhs.y - rhs.y )
    
end operator


operator * ( byref lhs as vec2f, byref rhs as single ) as vec2f
    
    return type<vec2f>( lhs.x * rhs, lhs.y * rhs )
    
end operator 


operator / ( byref lhs as vec2f, byref rhs as single ) as vec2f
    
    return type<vec2f>(lhs.x / rhs, lhs.y / rhs)
    
end operator


screenres 640,480,32,,FB.GFX_ALPHA_PRIMITIVES

type lstruct
    a as single
    p as vec2f
    c as integer = 1
end type

dim as integer mx, my, mb
dim as integer steps = 32
dim as integer bolts = 10
dim as single radius = 128
dim as vec2f posit = vec2f(320f,240f)


dim as double this_time

dim as lstruct rpoint(1 to bolts, 1 to steps)

do
    this_time = timer
    getmouse(mx,my,,mb)
    dim as vec2f ms = vec2f(mx,my)

    screenlock
    color rgb(0,0,0),rgb(200,200,200)
    cls
    
    for i as integer = 1 to bolts
        
        dim as single rangle = rnd*pi2
        rpoint(i,1).a = rangle
        rpoint(i,1).p = posit + vec2f(radius/5*sin(rangle), radius/5*cos(rangle) )
        
        dim as integer modify

        if mb and FB.BUTTON_LEFT and mx>-1 then
            'if (rpoint(i,1).p-posit).dot(ms-posit)>0 then
                modify = -1
            'end if
        end if

        for r as integer = 2 to steps
            dim as single nr = radius/steps
            rangle = rpoint(i,r-1).a -.75+rnd*1.5
            rpoint(i,r).a = rangle
            rpoint(i,r).p = rpoint(i,r-1).p + vec2f(nr*sin(rangle), nr*cos(rangle) )
            
            if modify then 
                dim as single dist = rpoint(i,r).p.distance(ms)
                dim as vec2f dvec = (ms-rpoint(i,r).p)/dist
                rpoint(i,r).p+= dvec*2f
            end if

            dim as single bcheck = ((rpoint(i,r).p.y - posit.y)^2) + ((rpoint(i,r).p.x - posit.x)^2)
            if bcheck>radius^2 or r = steps then
                bcheck =  sqr(bcheck)
                rpoint(i,r).p = posit + ( rpoint(i,r).p - posit ) / bcheck * radius
            end if
        next
        
    next
    
    for i as integer = radius to radius/5 step -1
        circle(posit.x, posit.y), i, rgb(i,i,255-(255*((i-1)/radius))),,,,f
    next

    dim as integer c
    'draw thick line
    for i as integer = 1 to bolts
        for r as integer = 2 to steps
            c = 255-(255*(r/steps))
            thickLine(rpoint(i,r-1).p.x, rpoint(i,r-1).p.y,rpoint(i,r).p.x, rpoint(i,r).p.y,5,rgba(100,100,255,c))
        next
    next
    'draw thinner line
    for i as integer = 1 to bolts
        for r as integer = 2 to steps
            c = 255-(255*(r/steps))
            thickLine(rpoint(i,r-1).p.x, rpoint(i,r-1).p.y,rpoint(i,r).p.x, rpoint(i,r).p.y,1,rgba(255,255,255,c))
        next
    next    
    'circle(posit.x, posit.y),radius/5,rgba(255,255,255,200),,,,f

    screensync
    screenunlock
    
    sleep 3,1

loop until multikey(fb.sc_escape)

sleep
Dr_D
Posts: 2451
Joined: May 27, 2005 4:59
Contact:

Re: The Lightning Thread (cont. from Electric Arcs)

Post by Dr_D »

BasicCoder2 wrote:Had a bit of a play with your code. Added a thickLine. Dodicat has a shorter thickLine. Because you create and store each bolt before display you can redraw it more than once at reducing sizes to produce a glow effect.

Code: Select all

 'Code trimmed to keep thread tidy. :)
That's cool man. :) Gives me some ideas...
Zamaster
Posts: 1025
Joined: Jun 20, 2005 21:40
Contact:

Re: The Lightning Thread (cont. from Electric Arcs)

Post by Zamaster »

Dr_D I like the spine of the arcs in the plasma ball, is that done by meandering in a direction by slowly changing the walk angle?
Dr_D
Posts: 2451
Joined: May 27, 2005 4:59
Contact:

Re: The Lightning Thread (cont. from Electric Arcs)

Post by Dr_D »

Thanks man. Yeah, it just picks a random angle within a certain range.
Post Reply