Author Topic: Electricity Effects . . . please bear with me!!!!  (Read 12699 times)

0 Members and 1 Guest are viewing this topic.

Offline DrewPee

  • I Toast Therefore I am
  • Pentium
  • *****
  • Posts: 563
  • Karma: 25
  • Eat Cheese - It's good for you!
    • View Profile
    • Retro Computer Museum
Hello guys
Ive come up with some code to generate electricity like effects but the code is shite!  :-\
It shows you what i am trying to do though . . . I want this sort of effect on top of a logo that I am designing.
Don't laugh too hard at my code please?!?!?  ???
I know I need to use something like tinyptc but not sure where to go from here!
Can anybody help me write something better or point me in the right direction?

DrewPee
DrewPee
aka Falcon of The Lost Boyz (Amiga)
Ex-Amiga Coder and Graphic Designer
Administrator of > www.retrocomputermuseum.co.uk

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: Electricity Effects . . . please bear with me!!!!
« Reply #1 on: September 06, 2006 »
tbh mate i really like that and thought it would be worse by the way you were dissing it,

tinyptc isnt a must but if youd like i can port your lightning routine over with good commentation.

btw when i do lightning i like to use garoud shaded triangles to have such things as flashing.
Challenge Trophies Won:

Offline DrewPee

  • I Toast Therefore I am
  • Pentium
  • *****
  • Posts: 563
  • Karma: 25
  • Eat Cheese - It's good for you!
    • View Profile
    • Retro Computer Museum
Re: Electricity Effects . . . please bear with me!!!!
« Reply #2 on: September 06, 2006 »
Any help would be greatly appreciated Ninogenio!
Thanks!

DrewPee
DrewPee
aka Falcon of The Lost Boyz (Amiga)
Ex-Amiga Coder and Graphic Designer
Administrator of > www.retrocomputermuseum.co.uk

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: Electricity Effects . . . please bear with me!!!!
« Reply #3 on: September 07, 2006 »
no worries mate.

ill port your lightning over tommorow for you and hopefully get you on your way into tinyptc.
Challenge Trophies Won:

Offline MrP

  • Atari ST
  • ***
  • Posts: 176
  • Karma: 18
    • View Profile
Re: Electricity Effects . . . please bear with me!!!!
« Reply #4 on: September 07, 2006 »
Its a good start, and has the potential to be a good effect. I'm sure Nino will do it justice with his TinyPTC Version... Although I think the code should change a bit if your thinking of using it in real time with other stuff happening......

Offline DrewPee

  • I Toast Therefore I am
  • Pentium
  • *****
  • Posts: 563
  • Karma: 25
  • Eat Cheese - It's good for you!
    • View Profile
    • Retro Computer Museum
Re: Electricity Effects . . . please bear with me!!!!
« Reply #5 on: September 07, 2006 »
Thanks MrP - Its something ive always wanted to do and it came to me whilst designing a logo for a customer yesterday morning.
Not sure if anybody would laugh because the code is pretty crappy - the idea is there but badly implemented.

Again thanks for the encouragement, and thanks Nino for taking a bit of time out for little ol' me!

Regards

DrewPee
DrewPee
aka Falcon of The Lost Boyz (Amiga)
Ex-Amiga Coder and Graphic Designer
Administrator of > www.retrocomputermuseum.co.uk

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17414
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: Electricity Effects . . . please bear with me!!!!
« Reply #6 on: September 07, 2006 »
Nino will do a great version of this for you I am sure!
The best electricity effects are in my opinion those that arc slowly between two points and flicker on and off with tendrils of electricity branching off them. If you wanted to similate something like that though you'd need to study nature and I think you'd find that a curved approach would even be better.

You could still generate the points randomly, apply sinus to them and re-generate the position of the points randomly, every say 25 frames. I believe that the electricity would look even better if you used some sort of cubic equation like a Bezier (which should be fast enough) to interpolate and face it, you're going to have to do that anyway if you're writing thins in tinyptc.

After all that, a few passes through an anti-alias filter (don't clear your screen buffer!) and you'll have a lovely arcing, flickering, blurry electric effect.
Shockwave ^ Codigos
Challenge Trophies Won:

Offline DrewPee

  • I Toast Therefore I am
  • Pentium
  • *****
  • Posts: 563
  • Karma: 25
  • Eat Cheese - It's good for you!
    • View Profile
    • Retro Computer Museum
Re: Electricity Effects . . . please bear with me!!!!
« Reply #7 on: September 07, 2006 »
@Shockwave,
Thanks for your input there shockwave, I know what you mean but I have not got to grips with SIN and COS in FreeBasic yet.
The code came to me fairly quickly and I was pleased with my result but it don't look very professional.
This is something I have wanted to do for a long time and looks like it is finally coming to fruition.   ;)
Regards
DrewPee
DrewPee
aka Falcon of The Lost Boyz (Amiga)
Ex-Amiga Coder and Graphic Designer
Administrator of > www.retrocomputermuseum.co.uk

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: Electricity Effects . . . please bear with me!!!!
« Reply #8 on: September 07, 2006 »
heres some code i bashed up really quickly this avo its just a port of your code that hopefully will let you get to grips with tiny.
now what id like to see is the lightning moving acrross the screen not to slowly and mabey flashing which you can do to the lines and also the background using the setrgb function then when youve got that sorted well see about blurring the lines.

Code: [Select]
'THE MAIN DIFFRENCE IN USING TINYPTC I CAN THINK OF IS THAT YOU GET NO GRAPHICS FUNCTIONS
'ALL YOU GET IS A 1D FRAME BUFFER IE INSTED OF BUFFER(0 TO 640, 0 TO 480) YOU GET BUFFER(0 TO 640*480)
'NOW TO PLOT A POINT AT SAY 10,100 YOU NEED AN AGORITHM HER IT IS Y*SCR_WIDTH+X SO THE LINE OF CODE
'WOULD BE BUFFER(100*640+10)=COLOR_VALUE THATS BASICALLY THE FUNDEMENTALLS TO IT NOW TO DRAW DIFFRENT
'SHAPES AND EFFECTS YOU USE THAT FORMULA WITH YOUR OWN CODE AND VWALLA

defint a-z

'$include: 'tinyptc.bi'

dim shared SCR_WIDTH as integer
dim shared SCR_HEIGHT as integer
dim shared SCR_SIZE as integer

'HERE I JUST DECLARE THE FUNCTIONS IM GOINGTO USE
declare function flip_buffer()
declare function clear_buffer()
declare function setrgb(byval index as double,byval red as double,byval green as double,byval blue as double)
declare function my_circle(byval x as double, byval y as double, byval r as double)
declare function my_line(byval x1 as double,byval y1 as double,byval x2 as double,byval y2 as double)
declare function open_window(byval xres as double,byval yres as double)

'i use option dynamic on the buffer array so i can resize it in the open window
'function to the size of the screen
'$DYNAMIC
dim shared buffer(0) as integer
dim shared forground_pallette_color as integer
dim shared background_pallette_color as integer

'these two lines just set the background to black and forground to white by default
forground_pallette_color = rgb( 255 , 255 , 255 )
background_pallette_color = rgb( 0 , 0 , 0 )

'here we open the ptc window with my own function thats not really needed but i find
'its a bit cleaner
open_window(640,480)

dim a as integer
dim r as integer
dim r2 as integer
dim i as integer
dim o as integer

randomize

'heres our main loop
do
   
    r = INT(RND(25)*75)+1
    r2 = INT(RND(25)*75)+1
   
    i=15
   
    o = int(rnd(1)*4)
    if o = 0 then r2 =- r2
    if o = 2 then r=-r
   
    'HERES THE MAIN DIFFRENCE FROM MY TINY CODE TO THE FB CODE
    'SETRGB SETS EITHER THE BACKGROUND COLOR OR FORGROUND COLOR AS SO
    'SETRGB INDEX(1 OR 0) 1 = FORGROUND 0 = BACKGROUND , RED(0 TO 255) , GREEN(0 TO 255) , BLUE(0 TO 255)
    setrgb 1,0,0,255
   
    'MY CIRCLE DIFFRERS IN THAT ALL YOU NEED SEING AS YOUVE JUST SET THE COLOR IS
    'MY CIRCLE X1,Y1,RAD
    my_circle 0, 240 , 6
    my_circle 639,240 , 6
   
    'AGAIN THE SAME APPLIES TO MY LINE
    'MY LINE X1,Y1,X2,Y2
    my_line 0 , 240 , 100+r , 200+r2
    my_line 100+r , 200+r2 , 225+r , 295+r2
    my_line 225+r , 295+r2 , 265+r , 220+r2
    my_line 265+r , 220+r2 , 305+r , 315+r2
    my_line 305+r , 315+r2 , 450+r , 150+r2
    my_line 450+r , 150+r2 , 550+r , 300+r2
    my_line 550+r , 300+r2 , 585+r , 200+r2
    my_line 585+r , 200+r2 , 639 , 240
   
    'NOW TO SIMULATE DOUBLE BUFFERING WY PUSH THE BUFFER ARRAY UP TO THE ACTUALLY SCREEN WITH FLIP_BUFFER
    flip_buffer
    'THEN CLEAR IT READY FOR THE NEXT FRAME
    clear_buffer
   
loop until( inkey$ = chr$(27) )
'a bit of house keeping
'empty the keboard buffer
while INKEY$ <> "": wend
'after code execution you have to close ptc
ptc_close
'and then clear the buffer
erase buffer



function my_circle(byval x as double, byval y as double, byval r as double)
   
          dim r2 as double
          dim z as double
          dim w as double
          if r<0 then exit function
          r2 = (r*r)
          for z=-r to r
               w=sqr(r2-z*z)
               my_line x-w,z+y,x+w,z+y
          next
         
          return 0
         
end function




''''''''''''''''''''''''''''''''''''''''''''''''''
' my_line named so because line was already taken
' Bresenham's infamous line algorithm
''''''''''''''''''''''''''''''''''''''''''''''''''
function my_line(byval x1 as double ,byval y1 as double ,byval x2 as double ,byval y2 as double)
         
         dim i, deltax, deltay, numpixels as integer
         dim d, dinc1, dinc2 as integer
         dim x, xinc1, xinc2 as integer
         dim y, yinc1, yinc2 as integer
         
         'sort points
         if x1>x2 then
             
              tmp = x1
              x1 = x2
              x2 = tmp
             
         endif
         
         if y1<0 then
             
            tmp = y1
            y1 = y2
            y2 = tmp
           
         endif
         
         'a spot of clipping
         if x1<0 then x1 = 0
         if x2>SCR_WIDTH then x2 = SCR_WIDTH - 1
         if y1<0 then y1 = 0
         if y2>SCR_WIDTH then y2 = SCR_HEIGHT - 1
         
         'calculate deltaX and deltaY
         deltax = abs(int(x2) - int(x1))
         deltay = abs(int(y2) - int(y1))
         
         'initialize
         if(deltax >= deltay) then
                  'If x is independent variable
                  numpixels = deltax + 1
                  d = (2 * deltay) - deltax
                  dinc1 = deltay shl 1
                  dinc2 = (deltay - deltax) shl 1
                  xinc1 = 1
                  xinc2 = 1
                  yinc1 = 0
                  yinc2 = 1
         else
                  'if y is independent variable
                  numpixels = deltay + 1
                  d = ( 2 * deltax ) - deltay
                  dinc1 = deltax shl 1
                  dinc2 = ( deltax - deltay ) shl 1
                  xinc1 = 0
                  xinc2 = 1
                  yinc1 = 1
                  yinc2 = 1
         endif
         
         'move the right direction
         if ( int(x1) > int(x2) ) then
                  xinc1 = -xinc1
                  xinc2 = -xinc2
         endif
         
         if ( int(y1) > int(y2) ) then
                  yinc1 = -yinc1
                  yinc2 = -yinc2
         endif
         
         x = int(x1)
         y = int(y1)
         
         'draw the pixels
         for i = 1 to numpixels
                  buffer( y*SCR_WIDTH+x ) = forground_pallette_color
                  if ( d < 0 ) then
                          d = d + dinc1
                          x = x + xinc1
                          y = y + yinc1
                  else
                          d = d + dinc2
                          x = x + xinc2
                          y = y + yinc2
                  endif
         next
         
         return 0
         
end function


'this function sets your background or forground color to what ever you specify
function setrgb(byval index as double,byval red as double,byval green as double,byval blue as double)
         if index = 0 then background_pallette_color = rgb( red , green , blue )
         if index = 1 then forground_pallette_color = rgb( red , green , blue )

         return 0
         
end function



'this function will open a tinyptc window what ever valid size you specify
function open_window(byval xres as double, byval yres as double)
   
         SCR_WIDTH=xres
         SCR_HEIGHT=yres
         SCR_SIZE=xres*yres
         redim buffer( 0 to SCR_SIZE - 1) as integer
         ptc_open( "ELECTRICITY", SCR_WIDTH, SCR_HEIGHT )
         
         return 0
         
end function



'this pushes the back buffer up to the display through tinyptc
function flip_buffer()
         
         ptc_update @buffer(0)
         
         return 0
         
end function



' this function clears our screen or what i like to think of the back buffer
function clear_buffer()
         
         for x=0 to SCR_SIZE-1 'scr_size is 640*480 because remember tiny uses 1d arrays
                 buffer(x) = background_pallette_color 'flushes all the elements in our screen refrence array i.e sets the color of each screen pixel to the color specifyed by setrgb
         next
         
         return 0
         
end function

<edited a small typo that stoped the background changing color>
« Last Edit: September 07, 2006 by ninogenio »
Challenge Trophies Won:

Offline DrewPee

  • I Toast Therefore I am
  • Pentium
  • *****
  • Posts: 563
  • Karma: 25
  • Eat Cheese - It's good for you!
    • View Profile
    • Retro Computer Museum
Re: Electricity Effects . . . please bear with me!!!!
« Reply #9 on: September 07, 2006 »
Thanks Nino, just been looking at the code - thanks for the remarks throughout the code.

Very much appreciate your help. Its over me now to have a mess about.

Regards
DrewPee
DrewPee
aka Falcon of The Lost Boyz (Amiga)
Ex-Amiga Coder and Graphic Designer
Administrator of > www.retrocomputermuseum.co.uk

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: Electricity Effects . . . please bear with me!!!!
« Reply #10 on: September 07, 2006 »
no probs mate if theres anything your not sure about just give a shout.
Challenge Trophies Won:

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17414
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: Electricity Effects . . . please bear with me!!!!
« Reply #11 on: September 07, 2006 »
Well done Nino :) A perfect port now in blue!
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Rbz

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 2757
  • Karma: 493
    • View Profile
    • https://www.rbraz.com/
Re: Electricity Effects . . . please bear with me!!!!
« Reply #12 on: September 07, 2006 »
Nice one DrewPee  8)  Keep it up!
Challenge Trophies Won:

Offline psygate

  • Completly Insane.
  • Atari ST
  • ***
  • Posts: 173
  • Karma: 7
  • That boy needs therapy.
    • View Profile
Re: Electricity Effects . . . please bear with me!!!!
« Reply #13 on: September 09, 2006 »
I don't know if you can use it, but I wrote a little electricity simulating program, based upon ninogenio's.
If you would like to have a comment on the source (I think its very self explaining...), then yu'r  :hi:.

Code: [Select]
option explicit
randomize timer
'I'll code it completely in freebasic, because im not so familiar with libraries.

dim as integer xmax,ymax,steps,x,y,px,py,px1,py1
declare sub electric(px as integer,py as integer,px1 as integer,py1 as integer,c as integer,stps as integer,dev as integer)
xmax=640
ymax=480

px=0
py=ymax\2
px1=xmax-1
py1=ymax\2

screenres xmax,ymax,24,,

circle (px,py),20,&hff,,,,f
circle (px1,py1),20,&hff,,,,f

while inkey$=""
    screenlock
    cls
    circle (px,py),20,&hff,,,,f
    circle (px1,py1),20,&hff,,,,f
    electric(px,py,px1,py1,&hff,int(rnd*256+1),int(rnd*20))
    screenunlock
wend
end

sub electric(px as integer,py as integer,px1 as integer,py1 as integer,c as integer,stps as integer,dev as integer)
    dim as integer stepx,stepy,cntr,nx,ny,ox,oy
    stepx=px1-px
    stepy=py1-py
    stepx\=stps
    stepy\=stps
    nx=px
    ny=py
    for cntr=1 to stps-1
        ox=nx
        oy=ny
        nx=px+stepx*cntr
        ny=py+stepy*cntr+int(Rnd*dev)*(-1)^int(Rnd*2)
        line (ox,oy)-(nx,ny),c
    next
    line (nx,ny)-(px1,py1),c
end sub

It shouldn't be any problem to port it to tinyptc. (I'll update it in an hour, wrote a new version)

Hope I could help!
« Last Edit: September 09, 2006 by psygate »
He who controlles the minds commands the many.
He who commands the many, conqueres the minds.

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17414
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: Electricity Effects . . . please bear with me!!!!
« Reply #14 on: September 10, 2006 »
If anything Psy, I think that one looks slightly more authentic :) You could improve it even more by having the colour of the electricity flash white and light purple occasionally, then it would own ! Nice work.
Shockwave ^ Codigos
Challenge Trophies Won:

Offline DrewPee

  • I Toast Therefore I am
  • Pentium
  • *****
  • Posts: 563
  • Karma: 25
  • Eat Cheese - It's good for you!
    • View Profile
    • Retro Computer Museum
Re: Electricity Effects . . . please bear with me!!!!
« Reply #15 on: September 10, 2006 »
I know I have said this before but you guys on here are something else - thanks for the code Psygate!
DrewPee
DrewPee
aka Falcon of The Lost Boyz (Amiga)
Ex-Amiga Coder and Graphic Designer
Administrator of > www.retrocomputermuseum.co.uk

Offline DrewPee

  • I Toast Therefore I am
  • Pentium
  • *****
  • Posts: 563
  • Karma: 25
  • Eat Cheese - It's good for you!
    • View Profile
    • Retro Computer Museum
Re: Electricity Effects . . . please bear with me!!!!
« Reply #16 on: September 10, 2006 »
Psygate - erm - that is soooo fast, I wish my code worked as fast as that - excellent!
Drew
DrewPee
aka Falcon of The Lost Boyz (Amiga)
Ex-Amiga Coder and Graphic Designer
Administrator of > www.retrocomputermuseum.co.uk

Offline psygate

  • Completly Insane.
  • Atari ST
  • ***
  • Posts: 173
  • Karma: 7
  • That boy needs therapy.
    • View Profile
Re: Electricity Effects . . . please bear with me!!!!
« Reply #17 on: September 10, 2006 »
I improved it a bit. At the moment im working on lightning like sparks, splitting off the main channel.

Code: [Select]
option explicit
randomize timer
'I'll code it completely in freebasic, because im not so familiar with libraries.

dim as integer xmax,ymax,steps,x,y,px,py,px1,py1,eleccol,bufc
declare sub electric(px as integer,py as integer,px1 as integer,py1 as integer,c as integer,stps as integer,dev as integer)
xmax=640
ymax=480

px=0
py=ymax\2
px1=xmax-1
py1=ymax\2

screenres xmax,ymax,24,,

circle (px,py),20,&hff,,,,f
circle (px1,py1),20,&hff,,,,f

while inkey$=""
    screenlock
    cls
    bufc=int(rnd*3)
    if bufc=0 then eleccol=&HFFFF00
    if bufc=1 then eleccol=&H00FFFF
    if bufc=2 then eleccol=&HFF
    electric(px,py,px1,py1,eleccol,int(rnd*256+1),int(rnd*20))
        circle (px,py),20,&hff,,,,f
    circle (px1,py1),20,&hff,,,,f
    screenunlock
wend
end

sub electric(px as integer,py as integer,px1 as integer,py1 as integer,c as integer,stps as integer,dev as integer)
    dim as integer stepx,stepy,cntr,nx,ny,ox,oy,spark,buf,cbuf
    stepx=px1-px
    stepy=py1-py
    stepx\=stps
    stepy\=stps
    nx=px
    ny=py
    for cntr=1 to stps-1
        ox=nx
        oy=ny
        nx=px+stepx*cntr
        ny=py+stepy*cntr+int(Rnd*dev)*(-1)^int(Rnd*2)
        buf=int(rnd*3)
        if buf=0 then spark=&HFFFF00
        if buf=1 then spark=int(Rnd*256)
        if buf=2 then cbuf=int(rnd*256):spark=rgb(0,cbuf,cbuf)
        line (ox+(-1)^int(Rnd*2),oy+(-1)^int(Rnd*2))-(nx+(-1)^int(Rnd*2),ny+(-1)^int(Rnd*2)),spark
        line (ox,oy)-(nx,ny),c
    next
    line (nx,ny)-(px1,py1),c
end sub
He who controlles the minds commands the many.
He who commands the many, conqueres the minds.

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17414
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: Electricity Effects . . . please bear with me!!!!
« Reply #18 on: September 11, 2006 »
Looks pretty cool to me Psy! Well done :)
Shockwave ^ Codigos
Challenge Trophies Won:

Offline DrewPee

  • I Toast Therefore I am
  • Pentium
  • *****
  • Posts: 563
  • Karma: 25
  • Eat Cheese - It's good for you!
    • View Profile
    • Retro Computer Museum
Re: Electricity Effects . . . please bear with me!!!!
« Reply #19 on: September 11, 2006 »
Looks good to me too - never realised I would get this much feedback for this one - Fantastic!

Drew
DrewPee
aka Falcon of The Lost Boyz (Amiga)
Ex-Amiga Coder and Graphic Designer
Administrator of > www.retrocomputermuseum.co.uk