Electric Arcs

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
Zamaster
Posts: 1025
Joined: Jun 20, 2005 21:40
Contact:

Electric Arcs

Post by Zamaster »

Been a while! So, I have never seen a decent, animated, real-time, single spine electric arc CGI. From what I've found online they're all kinda weak with some arguably better than others (no shots at any .bas boiz who've done one). My weekend project was to try and make one that was passable and somewhat believably morphed over time. I'll say again, I don't think this is great, but I think its better than most and could maybe inspire someone to extend it and make it look better.

First a screeny:
Image

And a link to the .exe and code:
http://www.filedropper.com/arcsimulation

Just to preemptively show usage of the ElectricArc object, here's the main source file:

Code: Select all

#include "electricarc.bi"
#include "vector2d.bi"

dim as ElectricArc arc
dim as integer ptr screenBuf
dim as integer arcs(0 to 20)
dim as integer i, mx, my
dim as Vector2D ptA
         
randomize 13                        
screenres 640,480,32

screenbuf = imagecreate(640,480)                

arc.init(640,480)
                     
for i = 0 to 9
    arcs(i) = arc.create()
    arc.setPoints(arcs(i), Vector2D(20 + i*60, 40), Vector2D(60 + i*60, 40))
next i

for i = 10 to 19
    arcs(i) = arc.create()
    arc.setPoints(arcs(i), Vector2D(20 + (i-10)*60, 300), Vector2D(60 + (i-10)*60, 400))
next i

arcs(20) = arc.create()
ptA = Vector2D(320, 200)

do
    getmouse mx, my
    line screenbuf, (0,0)-(639,479), 0, BF
    
    
    arc.setPoints(arcs(20), ptA, Vector2D(mx, my))
    
    
    arc.stepArcs(1.0 / 60.0)
    arc.drawArcs(screenbuf)
        
    put (0,0), screenbuf, pset
    sleep 20
loop until multikey(1)
Zamaster
Posts: 1025
Joined: Jun 20, 2005 21:40
Contact:

Re: Electric Arcs

Post by Zamaster »

Seems I didn't clean all the garbage code out of electricarc.bas because I was in a hurry so forgive me :S
anonymous1337
Posts: 5494
Joined: Sep 12, 2005 20:06
Location: California

Re: Electric Arcs

Post by anonymous1337 »

Zamaster, three things:

1) I'm submitting this to my FB User Contribs GitHub repository under the "Zamaster" folder if that's OK with you.

2) FileDropper, really? :P Aren't you hip to DropBox, Google Drive or some other modern and free host by now?

3) I've modified the arc4 file to use #include once instead of #include, and to #include once the .bas files to simplify the compilation process. I hope no purists shun me.

How are your graduate studies?
Zamaster
Posts: 1025
Joined: Jun 20, 2005 21:40
Contact:

Re: Electric Arcs

Post by Zamaster »

I'm glad I have a folder!

I extensively use both, but turn into a grandma when it comes to sharing off of them and go find something more "public" when I want to share to the greater interwebz.

Shunnnn, just throw together a little make.bat and put it in the folder:

Code: Select all

del "arc4.exe"
fbc -g -s gui -x arc4.exe -m arc4 *.bas
Thanks for asking, I'm actually writing my master's thesis atm. It's cool, I'm proposing a set of algorithms for continuous collision detection and restitution solving that lead to interpenetration-less physics implementations. Kinda like... "swept" collisions, but fast, and also the part about bodies never occupying the same space. I'm getting strong armed into writing the methods to utilize the GPU because buhnz. I'm mostly doing it because with me, all roads lead to games haha and I think there are many issues with the freeware fizix engines everybody's using these days.

How u doin ;)?
anonymous1337
Posts: 5494
Joined: Sep 12, 2005 20:06
Location: California

Re: Electric Arcs

Post by anonymous1337 »

Zamaster wrote:Shunnnn, just throw together a little make.bat and put it in the folder:

Code: Select all

del "arc4.exe"
fbc -g -s gui -x arc4.exe -m arc4 *.bas
As you command :|
Zamaster wrote:Thanks for asking, I'm actually writing my master's thesis atm. It's cool, I'm proposing a set of algorithms for continuous collision detection and restitution solving that lead to interpenetration-less physics implementations. Kinda like... "swept" collisions, but fast, and also the part about bodies never occupying the same space. I'm getting strong armed into writing the methods to utilize the GPU because buhnz. I'm mostly doing it because with me, all roads lead to games haha and I think there are many issues with the freeware fizix engines everybody's using these days.
Do your parents know that's how you waste your degree? Jk, that sounds cool. 3D algorithms or...?
Zamaster wrote:How u doin ;)?
Fine. I thank you for asking lel.
BasicCoder2
Posts: 3917
Joined: Jan 01, 2009 7:03
Location: Australia

Re: Electric Arcs

Post by BasicCoder2 »

Here are some clever simulations if anyone is clever enough to write FreeBasic versions:
http://krazydad.com/bestiary/bestiary_lightning.html

.
dodicat
Posts: 7987
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Electric Arcs

Post by dodicat »

I think it's C with some graphics library or other.
But it is effective, here is a first draft with hardly any frills:
Curdetail (in the sub) and displace (passed) can be tweaked.

Code: Select all

 

#macro Original_Ccode(This_is_a_comment)
function drawLightning(x1,y1,x2,y2,displace)
{
  if (displace < curDetail) {
    graf.moveTo(x1,y1);
    graf.lineTo(x2,y2);
  }
  else {
    var mid_x = (x2+x1)/2;
    var mid_y = (y2+y1)/2;
    mid_x += (Math.random()-.5)*displace;
    mid_y += (Math.random()-.5)*displace;
    drawLightning(x1,y1,mid_x,mid_y,displace/2);
    drawLightning(x2,y2,mid_x,mid_y,displace/2);
  }
}
#endmacro



'Freebasic
function drawLightning(x1 as integer ,y1 as integer,x2 as integer,y2 as integer,displace as integer) as integer
dim as integer curdetail=5
  if (displace < curDetail) then 
  line(x1,y1)-(x2,y2)
  else 
    var mid_x = (x2+x1)/2
    var mid_y = (y2+y1)/2
    mid_x += (rnd-.5)*displace
    mid_y += (rnd-.5)*displace
    drawLightning(x1,y1,mid_x,mid_y,displace/2)
    drawLightning(x2,y2,mid_x,mid_y,displace/2)
  end if
  return 0
end function

screen 19,32,,64
do
    screenlock
    line(0,0)-(799,599),rgba(0,0,0,150),bf
    circle(25,300),25,rgb(100,100,100),,,,f
    circle(775,300),25,rgb(100,100,100),,,,f
drawlightning(50,300,750,300,400)
screenunlock
sleep 100
loop until len(inkey)
sleep
 
BasicCoder2
Posts: 3917
Joined: Jan 01, 2009 7:03
Location: Australia

Re: Electric Arcs

Post by BasicCoder2 »

I was hoping you might have a go at it. I did translate it to FreeBasic as well but made a silly coding mistake, so silly I won't say what it was, and my attempt kept crashing.

This is another algorithm that looks really cool.
http://drilian.com/2009/02/25/lightning-bolts/

added thickness to your example:

Code: Select all

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


'Freebasic
function drawLightning(x1 as integer ,y1 as integer,x2 as integer,y2 as integer,displace as integer) as integer
  dim as integer curdetail=5
  if (displace < curDetail) then 
  thickline(x1,y1,x2,y2,2,rgb(200,200,255))
  else 
    var mid_x = (x2+x1)/2
    var mid_y = (y2+y1)/2
    mid_x += (rnd-.5)*displace
    mid_y += (rnd-.5)*displace
    drawLightning(x1,y1,mid_x,mid_y,displace/2)
    drawLightning(x2,y2,mid_x,mid_y,displace/2)
  end if
  return 0
end function

screen 19,32,,64
do
    screenlock
    line(0,0)-(799,599),rgba(0,0,0,150),bf
    circle(25,300),25,rgb(100,100,100),,,,f
    circle(775,300),25,rgb(100,100,100),,,,f
    drawlightning(50,300,750,300,400)
    screenunlock
    sleep 100
loop until len(inkey)
sleep
anonymous1337
Posts: 5494
Joined: Sep 12, 2005 20:06
Location: California

Re: Electric Arcs

Post by anonymous1337 »

Started a little experiment: http://freebasic.net/forum/viewtopic.php?f=15&t=23518

Anyone up for joining in? I get that this Tips & Tricks thread works just fine, but I'm trying to make a point per this thread: http://freebasic.net/forum/viewtopic.php?f=17&t=23476

Of course, it's up to other people to support the idea or not. I could also (in theory) scrape specific threads and place all lightning-related code demos in a GitHub repository, although it would be MUCH easier to do that from within a single thread.
Dr_D
Posts: 2452
Joined: May 27, 2005 4:59
Contact:

Re: Electric Arcs

Post by Dr_D »

This is good man. I wish it would have been around when sir_mud and I made that Dr. Mudball game. It would have looked a lot better I think.
Stonemonkey
Posts: 649
Joined: Jun 09, 2005 0:08

Re: Electric Arcs

Post by Stonemonkey »

This is something I want to try in stereoscopic.
Dr_D
Posts: 2452
Joined: May 27, 2005 4:59
Contact:

Re: Electric Arcs

Post by Dr_D »

Stonemonkey wrote:This is something I want to try in stereoscopic.

Now we're talking dude... :)
Stonemonkey
Posts: 649
Joined: Jun 09, 2005 0:08

Re: Electric Arcs

Post by Stonemonkey »

OK, so now I need to code one of these in 3 dimensions first. Are there any rules or ways you're supposed to generate the points or do you just make it up as you go along?
Dr_D
Posts: 2452
Joined: May 27, 2005 4:59
Contact:

Re: Electric Arcs

Post by Dr_D »

Stonemonkey wrote:OK, so now I need to code one of these in 3 dimensions first. Are there any rules or ways you're supposed to generate the points or do you just make it up as you go along?
I just made mine up man. It isn't any kind of real simulation or anything.
BasicCoder2
Posts: 3917
Joined: Jan 01, 2009 7:03
Location: Australia

Re: Electric Arcs

Post by BasicCoder2 »

Dr_D wrote:I just made mine up man. It isn't any kind of real simulation or anything.
One way is a random walk although it becomes a problem to make the random walk end up at the same place each time if that is required.
The method below uses a random walk to the right halfway (white) and a reverse version of the walk in the other direction (yellow). The last segment joins them together (red).

Code: Select all

screenres 640,480,32

dim as integer x1,x2,y1,y2
dim as integer x3,x4,y3,y4

do
    x1 = 40
    y1 = 240
    x3 = 600
    y3 = 240
    cls
    locate 2,2
    print "TAP SPACE BAR FOR NEW EXAMPLE"
    locate 4,2
    print "TAP ESC KEY TO EXIT"
    circle (40,240),5,rgb(0,0,255),,,,f
    circle (600,240),5,rgb(0,0,255),,,,f
    do
        y2 = y1 + int(rnd(1)*30)-15
        x2 = x1 + 5
        line (x1,y1)-(x2,y2),rgb(255,255,255)
        x1 = x2
        y1 = y2
        
        y4 = y1 + int(rnd(1)*30)-15
        x4 = x3 - 5
        line (x3,y3)-(x4,y4),rgb(255,255,0)
        x3 = x4
        y3 = y4
    loop until x2 >= 315
    line (x2,y2)-(x4,y4),rgb(255,0,0)
    sleep
loop until multikey(&H01)
Post Reply