Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411528 Posts in 69377 Topics- by 58433 Members - Latest Member: Bohdan_Zoshchenko

April 29, 2024, 12:39:37 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)lightning graphic generated by code?
Pages: [1]
Print
Author Topic: lightning graphic generated by code?  (Read 2758 times)
st33d
Guest
« on: January 20, 2010, 01:24:34 PM »

I'm embarking on creating a lightning effect. I'm in Flash here so I'll probably be doing it all with lines. It's going to dance along the walls and hit enemies of course, but it's the basic look of the thing I'm more worried about, the rest I can fudge.

Anybody tackled this before or seen it code about for it before?

I've tried Googling but no dice.
Logged
John Nesky
Level 10
*****


aka shaktool


View Profile WWW
« Reply #1 on: January 20, 2010, 01:35:32 PM »

Here are some examples to inspire you:

http://wonderfl.net/code/04585ddf94a40f2793910bad68218f9122a80326
http://wonderfl.net/code/088749d796b33e4ab1892c346bfb7e95b82dbe9e
http://wonderfl.net/code/5cf192f22076b7c4e7e883ea06a3166abe02392d
Logged
st33d
Guest
« Reply #2 on: January 20, 2010, 02:06:20 PM »

Cool stuff!

The second example, although a bit cheesy was more what I was going for, but I knocked up a quick version myself which is more directional:

Code:
package {
import com.robotacid.geom.Dot;
import com.robotacid.geom.Line;
import flash.display.Graphics;
import flash.display.Sprite;
import flash.events.Event;

[SWF(width = "640", height = "480", frameRate="30", backgroundColor = "#000000")]

/**
* ...
* @author Aaron Steed, robotacid.com
*/
public class Main extends Sprite {

public function Main():void {
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}

private function init(e:Event = null):void {
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
scaleX = scaleY = 2;
addEventListener(Event.ENTER_FRAME, loop);
}

private function loop(e:Event = null):void{
graphics.clear();
graphics.lineStyle(1, 0xFFFFFF);
drawLightning(graphics, 160, 120, mouseX, mouseY);
}

private function drawLightning(gfx:Graphics, start_x:Number, start_y:Number, finish_x:Number, finish_y:Number, step:Number = 10):void{
var line:Line = new Line(new Dot(start_x, start_y), new Dot(finish_x, finish_y));
line.b.x = line.a.x + (line.dx + Math.random()) * line.lx + (Math.random() * line.rx) * Math.random() * step;
line.b.y = line.a.y + (line.dy + Math.random()) * line.ly + (Math.random() * line.ry) * Math.random() * step;
line.draw(gfx);
var i:int = 0;
while(line.b.x != finish_x && line.b.y != finish_y){
line.a.x = line.b.x;
line.a.y = line.b.y;
line.b.x = finish_x;
line.b.y = finish_y;
line.updateLine();
line.b.x = line.a.x + (line.dx + (Math.random() * line.lx) + (Math.random() * line.rx)) * Math.random() * step;
line.b.y = line.a.y + (line.dy + (Math.random() * line.ly) + (Math.random() * line.ry)) * Math.random() * step;
if(line.b.dist(new Dot(finish_x, finish_y)) < step){
line.b.x = finish_x;
line.b.y = finish_y;
}
line.draw(gfx);
if(i++ > 200) break;
}
}

}

}

The lightning will be random, but it will also target nearby monsters (think of it as a smart bomb that looks like the "quickening" in the Highlander films).

Rather than jumping about wildly around the next node, I've gone with stepping forwards but throwing the vector off randomly towards the right or left normal.

I need to optimise this a lot though. I'll upload when I can isolate this in one class rather than relying on my own geom package. I doubt I'll be able to afford to indulge in filters either.

My major concern is getting the forking effect. With a Tesla bolt it arcs towards a specific target, but sends off the odd little tendril, as this video shows:



Logged
minasss
Level 0
***



View Profile WWW
« Reply #3 on: January 21, 2010, 03:57:49 AM »

WOW the third example is really cool
Logged
st33d
Guest
« Reply #4 on: January 29, 2010, 02:02:09 AM »

I think what I really need is this:

http://gamma.cs.unc.edu/LIGHTNING/applet/index.html

Because it uses a grid to generate the lightning, I can get it to react to the walls in my platformer engine and also get it to seek out a target.

I've currently got a path-finding line that randomly deviates towards the target, like what I just did above. But I'm still trying to figure out how I would tackle branching.

I asked this question on the Processing board and someone linked this:

http://www.openprocessing.org/visuals/?visualID=2924
Logged
st33d
Guest
« Reply #5 on: January 30, 2010, 06:50:38 AM »

I came up with something this afternoon:

http://www.robotacid.com/flash/red_rogue/lightning/

Source code on the page. Could be refined a bit more I think, but most important is that there's game logic going on it making it that can be used to get it to strike monsters.
Logged
John Nesky
Level 10
*****


aka shaktool


View Profile WWW
« Reply #6 on: January 30, 2010, 09:55:01 AM »

That is fantastic. Shocked
Logged
Desert Dog
Level 4
****



View Profile
« Reply #7 on: January 30, 2010, 04:18:34 PM »

That is fantastic. Shocked
Agreed! That is nice work indeed.
Logged

Glaiel-Gamer
Guest
« Reply #8 on: January 30, 2010, 04:50:41 PM »

Whenever I've done code lightning, I usually structure it somewhat like this:

Step 1 is to get the lightning with no branching. Pass it in a start point, and an optional end point. If no end point is passed in, then it does not try to hit any specific target. Optionally also pass it in a direction, and a size is important too (usually specified as a "thickness".

Then, you have a direction and a size. Every iteration, vary the direction by a random amount (usually by rotating it +-random(chaoticness), can add a weight in if you need to get to a target), and decrease the thickness by a small bit. Go until you run out of "thickness".

Once you get this looking right, adding branching is as simple as spawning new bolts (as a random chance) every couple iterations. Cut the thickness in half, and spawn a new bolt with that thickness to "split" it into 2.
Logged
st33d
Guest
« Reply #9 on: January 31, 2010, 04:12:29 AM »

Yah, the branching is definitely a recursive function that is similar to the core bolt, but the core bolt needs to be targeted, whereas the branching needs to spread out randomly.

I went for a core bolt algorithm and then wrote a separate recursive branch algorithm that I would start calling whilst walking the path. That seemed to work well.

Although it would be nice to get all of the logic in one function.
Logged
Will Vale
Level 4
****



View Profile WWW
« Reply #10 on: January 31, 2010, 01:33:35 PM »

[quote
I came up with something this afternoon:
Looks great! I like the stylised look coming (I presume) from the underlying grid.

can add a weight in if you need to get to a target
I've implemented lightning along the lines you suggest, except that I replaced the above step by having the lightning travel along a straight line (or arc) between the source and target and fading out the contribution from the random walk at the start and end. It's a bit easier and you know it won't wander off the target unless you want it to Smiley
Logged
Glaiel-Gamer
Guest
« Reply #11 on: January 31, 2010, 02:12:00 PM »

Yah, the branching is definitely a recursive function that is similar to the core bolt, but the core bolt needs to be targeted, whereas the branching needs to spread out randomly.

I went for a core bolt algorithm and then wrote a separate recursive branch algorithm that I would start calling whilst walking the path. That seemed to work well.

Although it would be nice to get all of the logic in one function.

I just have a thing where i set a target point, and if it's not there then it doesnt add in the weight

can add a weight in if you need to get to a target
I've implemented lightning along the lines you suggest, except that I replaced the above step by having the lightning travel along a straight line (or arc) between the source and target and fading out the contribution from the random walk at the start and end. It's a bit easier and you know it won't wander off the target unless you want it to Smiley

http://spamtheweb.com/ul/upload/310110/51034_nerdsmite.swf

What I did here was have the random weight, which gets the bolt CLOSE to where it needs to be, but then the last iteration just goes right to the target. You never really notice when it misses then
Logged
Will Vale
Level 4
****



View Profile WWW
« Reply #12 on: February 01, 2010, 03:05:25 PM »

That works well - I never had an instance of it not targetting accurately - thanks for the demo Smiley One visual nitpick is that it's maybe a bit smooth - to me it looks more like veins than lightning?
Logged
Draknek
Level 6
*


"Alan Hazelden" for short


View Profile WWW
« Reply #13 on: February 02, 2010, 05:04:53 AM »

This is very cool: http://imgbit.com/images/2868c9c9331249519453.gif

If not particularly relevant to procedurally generating the effect.
Logged

Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic