Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Advice
Index -> Programming, Turing -> Turing Submissions
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
TheOneTrueGod




PostPosted: Fri Mar 03, 2006 4:01 pm   Post subject: Advice

Allright, i'm programming an RPG type thing, with real time battles, and I want to make it entirely in turing (I.E. no outside graphics, meaning the game won't look too great but itll play well [I hate skeptics who only focus on graphics]) That being said, I'm trying to craft a lightning spell, and to do that I wanted to make it look fancier than just a yellow line from point A to point B. I came up with the following procedure:

code:
function Angle (x1, y1, x2, y2 : real) : real
    var theta : real
    if x1 = x2 then
        if y2 > y1 then
            theta := 90
        else
            theta := 270
        end if
    else
        theta := arctand ((y1 - y2) / (x1 - x2))
        if x2 > x1 then
            if y2 > y1 then
            else
                theta := 360 + theta
            end if
        else
            theta := 180 + theta
        end if
    end if
    result theta
end Angle

function Dist (x1, y1, x2, y2 : real) : real
    result sqrt ((x2 - x1) ** 2 + (y2 - y1) ** 2)
end Dist

procedure DrawLightning (x, y, tx, ty : real, numforks : int)
    var lx, ly, oldlx, oldly : real := x
    var tempcx, tempcy : real
    var angle : real := Angle (x, y, tx, ty)
    var r : int
    ly := y
    oldly := y
    var c : int := 0
    loop
        c += 1
        lx += cosd (angle) * Dist (x, y, tx, ty) / numforks
        ly += sind (angle) * Dist (x, y, tx, ty) / numforks
        tempcx := lx
        tempcy := ly
        r := Rand.Int (-1, 1)
        tempcx += cosd (angle + r * 90) * Dist (x, y, tx, ty) / numforks
        tempcy += sind (angle + r * 90) * Dist (x, y, tx, ty) / numforks
        exit when c > numforks

        Draw.ThickLine (round (oldlx), round (oldly), round (tempcx), round (tempcy), 3, yellow)
        oldlx := tempcx
        oldly := tempcy

    end loop
end DrawLightning
View.Set ('offscreenonly')
loop
    for i : -1 .. 1
        for j : -1 .. 1
            DrawLightning (maxx div 2, maxy div 2, maxx div 2 + 200 * i, maxy div 2 + 200 * j, 70)
        end for
    end for
    View.Update
    delay (30)
    cls
end loop



This will just draw several lightning bolts originating from the centre. I need to know some ways to improve it, because while it looks okay, it could still use some improvement.

The algorithm works basically by dividing it up into "NumForks" different sections, and just randomizing if it goes up or down on that section. Any ideas on how to make it look better, without sacrificing too much efficiency? (Also, a bit of efficiency help may be nice, but the main thing i'm looking for is graphical improvement. I know I should have made it a for loop, for example.)
Sponsor
Sponsor
Sponsor
sponsor
batman




PostPosted: Fri Mar 03, 2006 4:43 pm   Post subject: Program

Cool program!
One thing you could do to make your code better is using arrays, it will make your code shorter. You could also add music to your program! Maybe a lightningnoice, that be cool. IF you need help doing that then send me a message.
Delos




PostPosted: Fri Mar 03, 2006 6:05 pm   Post subject: Re: Program

batman wrote:
Cool program!
One thing you could do to make your code better is using arrays, it will make your code shorter. You could also add music to your program! Maybe a lightningnoice, that be cool. IF you need help doing that then send me a message.


Intersting concept. Now where exactly would he be using said arrays? Perhaps in lx, ly...but that would just be pushing it.

As for improvement:
I like the look of it when few forks are used. To make things a little more interesting, use a couple of other 'think lines' drawn with a larger radius behind the main one. This will add a border, and you can achieve some enticing effects.
My own intuition tells me that your parameter setup:
code:

DrawLightning (x, y, tx, ty : real, numforks : int)

would translate as positionX, positionY, sizeX, sizeY, forks. This is apparently not true though Very Happy. But that's just me. Perhaps having a variable size would be advantageous?

Now, the most important critique:
Go here!. zylum's tut on recursion will open up a whole new level of leetness for you. You'll want to focus on fractal generation, since this will allow you to create more realistic lightning.

Good work so far, and keep us updated.
Clayton




PostPosted: Fri Mar 03, 2006 8:14 pm   Post subject: (No subject)

very nice, how long did that take you to figure out, your obviously a math genius, however, whenever you finish your RPG post it, id like to give it a try, if this is a good sample of it, keep up the good work
md




PostPosted: Fri Mar 03, 2006 9:59 pm   Post subject: (No subject)

The math looks interesting; but as I don't have turing someone want to post a screenshot?

Batman: Arrays? I can't see any use for arrays at all...
SuperFreak82: Good with math yes, math genius perhaps not. This code just demonstrates an ability to use math (one which to be fair many people seem to lack...) but I don't think it qualifies for genius level code.

One thing that might be cool; and uses recursion in psudo (Pascal/C) code:

code:
Function Lightening(s, d:point; fork_length, fork_chance, fork_angle:real; fork_count:integer)
{
    if( Distance(s, d) <= fork_length )
            LineTo(d);
        else
        {
            point n;
            real angle = AngleTo(s, d) - (fork_angle/2);
            for( int i = 1 to fork_count )
            {
                        n.x += s.x + cos(angle) * fork_length;
         n.y += s.y + sin(angle) * fork_length;
                        angle += fork_angle / fork_count;
                        if( Rand(0,1) <= fork_chance)
                        {
                                Line(s, n);
                                Lightening(n, d, fork_length * 0.7, fork_chance * 0.7, fork_angle *0.7, fork_count);
                        }
            }
        }
}


The 0.7's are to make it less likely to fork, and less likely to go away from the point the closer it gets.

**Edit, damned tabs. I'd fix it... but I'm lazy.
** Edit 2: It'd look better with a really wide fork angle, ~50% fork chance, high fork count, and shortish fork length.
Cervantes




PostPosted: Sat Mar 04, 2006 11:30 am   Post subject: (No subject)

Accidently posted in a new thread:
TheOneTrueGod wrote:
Thanks for the help. I attempted to put in recursion for it, and this is what I got. I only found one problem that I don't know how to fix. At higher "divisions" and "angle", i.e. (60 and 120 respectively), the final branch is much longer than the rest. It adds for an ok "homing in" effect, but aside from that, it looks like crap.

@Batman, if you feel like creating, or finding, or whatever a bolt sound, go for it, I will implement it into the RPG.

code:

function Angle (x1, y1, x2, y2 : real) : real
    var theta : real
    if x1 = x2 then
        if y2 > y1 then
            theta := 90
        else
            theta := 270
        end if
    else
        theta := arctand ((y1 - y2) / (x1 - x2))
        if x2 > x1 then
            if y2 > y1 then
            else
                theta := 360 + theta
            end if
        else
            theta := 180 + theta
        end if
    end if
    result theta
end Angle

function Dist (x1, y1, x2, y2 : real) : real
    result sqrt ((x2 - x1) ** 2 + (y2 - y1) ** 2)
end Dist

procedure DrawLightning (startx, starty, x, y, exitx, exity, angle : real, exitcondition, divs, forkanglemax : int)
    var x2, y2 : real
    var r : int := Rand.Int (-forkanglemax, forkanglemax)
    if exitcondition not= 1 then
        x2 := x + cosd (angle + r) * Dist (startx, starty, exitx, exity) / divs
        y2 := y + sind (angle + r) * Dist (startx, starty, exitx, exity) / divs
        Draw.ThickLine (round (x), round (y), round (x2), round (y2), 3, yellow)
        DrawLightning (startx, starty, x2, y2, exitx, exity, Angle (x2, y2, exitx, exity), exitcondition - 1, divs, round (forkanglemax - (forkanglemax / divs)))
    else
        Draw.ThickLine (round (x), round (y), round (exitx), round (exity), 3, yellow)
    end if
end DrawLightning
procedure Lightning (x1, y1, x2, y2 : real, fork_count, fangle : int)
    DrawLightning (x1, y1, x1, y1, x2, y2, Angle (x1, y1, x2, y2), fork_count, fork_count, fangle)
end Lightning

loop
    locate (1, 1)
    Lightning (100, 100, 300, 100, 25, 40)

    delay (30)
    cls
end loop


Oh, and the header for the procedure is as follows:
Lightning(starting_x_coordinate,starting_y_coordinate,ending_x_coordinate,ending_y_coordinate,number_of_forks,max_fork_starting_angle)


Taking care of business
[Gandalf]




PostPosted: Sat Mar 04, 2006 12:10 pm   Post subject: (No subject)

Pictures of Lightning slightly edited by me:
http://img486.imageshack.us/img486/179/lightning1ih.jpg
http://img96.imageshack.us/img96/9669/lightning28so.jpg

My attempt at converting Cornflake's code, which doesn't quite work:

code:
proc Lightning (sX, sY, dX, dY : real, fork_length, fork_chance, fork_angle : real, fork_count : int)
    if Math.Distance (sX, sY, dX, dY) <= fork_length then
        Draw.Line (round (sX), round (sY), round (dX), round (dY), black)
    else
        var nX, nY : real := 0
        var angle : real := Angle (sX, sY, dX, dY) - (fork_angle / 2)
        for i : 1 .. fork_count
            nX += sX + cos (angle) * fork_length
            nY += sY + sin (angle) * fork_length
            angle += fork_angle / fork_count
            if Rand.Real <= fork_chance then
                Draw.Line (round (sX), round (sY), round (nX), round (nY), black)
                Lightning (nX, nY, dX, dY, fork_length * 0.7, fork_chance * 0.7, fork_angle * 0.7, fork_count)
            end if
        end for
    end if
end Lightning

Lightning (50, 100, maxx - 50, 100, 5, 0.5, 40, 50)

Not sure what I got wrong... Don't have much time to spend this minute. Think I might have gotten a int/real mixed up.
batman




PostPosted: Sat Mar 04, 2006 1:27 pm   Post subject: Bolt sound

I will begin on working on some sort of bolt sound and when im done i'll send it to you. You know what would make your code cooler is if you added falling rain in the backround. Its a suggestion. You could use blue asterisks that scroll down the screen as rain, I guess.
Very Happy
Sponsor
Sponsor
Sponsor
sponsor
Mazer




PostPosted: Sat Mar 04, 2006 1:53 pm   Post subject: (No subject)

The lightning effect would definitely benefit from a dark background. I know you said you'll be using it in a game, but consider a sort of checkerboard of black dots across the screen to make it look darker just before you start with the lightning.

The lightning itself looks nice (from the screenshots; I haven't got Turing here to see if it's animated or anything). A few things to consider here:
- try using the RGB module to get yourself a small pallete of colours, bright yellows that are close to white (or if you'd prefer instead of yellow, blue)
- thinner bolts. If it doesn't take too much time while running, try drawing the bolts as thick as they are now with one of those slightly darker colours from before, and then again thinner with a really bright colour. Just try to make the differences subtle
- fewer bolts: screenshot2 was pretty crazy, but again, I don't know if this is animated...

Keep it up!
Clayton




PostPosted: Sat Mar 04, 2006 6:46 pm   Post subject: (No subject)

Quote:

SuperFreak82: Good with math yes, math genius perhaps not. This code just demonstrates an ability to use math (one which to be fair many people seem to lack...) but I don't think it qualifies for genius level code.

i was just stating that he obviously knew something about math, and that he is good with it, i was exagerating, and in answer to some of the above questions yes this is animated
MysticVegeta




PostPosted: Sat Mar 04, 2006 8:01 pm   Post subject: Re: Program

Delos wrote:
Now, the most important critique:
Go here!. zylum's tut on recursion will open up a whole new level of leetness for you. You'll want to focus on fractal generation, since this will allow you to create more realistic lightning.

Interesting, recursive fractals of lightening.. now thats what I am talking about!!! heh
[Gandalf]




PostPosted: Sat Mar 04, 2006 8:28 pm   Post subject: (No subject)

Coutsos, hehe. I made it so that there would be 100 lightning bolts in the 2nd screenshot, only my "demo". I also changed the lightning bolts to be various shades of yellow. As for animation... It's only animated as far as:
loop
draw i x lightning at random spots
clear screen
end loop
md




PostPosted: Sun Mar 05, 2006 8:18 am   Post subject: (No subject)

[Gandalf] wrote:
My attempt at converting Cornflake's code, which doesn't quite work:

code:
proc Lightning (sX, sY, dX, dY : real, fork_length, fork_chance, fork_angle : real, fork_count : int)
    if Math.Distance (sX, sY, dX, dY) <= fork_length then
        Draw.Line (round (sX), round (sY), round (dX), round (dY), black)
    else
        var nX, nY : real := 0
        var angle : real := Angle (sX, sY, dX, dY) - (fork_angle / 2)
        for i : 1 .. fork_count
            nX += sX + cos (angle) * fork_length
            nY += sY + sin (angle) * fork_length
            angle += fork_angle / fork_count
            if Rand.Real <= fork_chance then
                Draw.Line (round (sX), round (sY), round (nX), round (nY), black)
                Lightning (nX, nY, dX, dY, fork_length * 0.7, fork_chance * 0.7, fork_angle * 0.7, fork_count)
            end if
        end for
    end if
end Lightning

Lightning (50, 100, maxx - 50, 100, 5, 0.5, 40, 50)

Not sure what I got wrong... Don't have much time to spend this minute. Think I might have gotten a int/real mixed up.

Couple of things I've thought about since: angles must be modulo 360; or 2PI. And the random bit towards the end means that lightening is not guarunteed to propagate all the way to the target; that needs to be fixed somehow. One way would be to count the number of forks generated; and if there weren't any then just draw a line along any random angle in the range.
MysticVegeta




PostPosted: Sun Mar 05, 2006 12:32 pm   Post subject: (No subject)

SuperFreak82 wrote:
very nice, how long did that take you to figure out, your obviously a math genius, however, whenever you finish your RPG post it, id like to give it a try, if this is a good sample of it, keep up the good work


if you need to see a math genius, search for catalyst's old posts and his submissions.
Clayton




PostPosted: Sun Mar 05, 2006 5:23 pm   Post subject: (No subject)

MysticVegeta wrote:
SuperFreak82 wrote:
very nice, how long did that take you to figure out, your obviously a math genius, however, whenever you finish your RPG post it, id like to give it a try, if this is a good sample of it, keep up the good work


if you need to see a math genius, search for catalyst's old posts and his submissions.


ive seen them dont you worry about that, that guy has skillz
Display posts from previous:   
   Index -> Programming, Turing -> Turing Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 15 Posts ]
Jump to:   


Style:  
Search: