Computer Science Canada

[source] water

Author:  zylum [ Wed Jan 18, 2006 10:27 pm ]
Post subject:  [source] water

ok here's another program that involves spring forces. Its water! Surprised

code:
setscreen ("graphics:max;400,offscreenonly")

type Node :
    record
        x, y, vx, vy, px, py : real
    end record

const g := 0
const wind := -0.0
const N := 100
const d := 0
const k := 0.5
const decay := 0.99
const wallFriction := 0.9
const radius := maxx div (N * 2)

var F : real
var mx, my, md, mxl, myl : int
var holding : int := -1
var x, y : array 1 .. N + 2 of int

var connected : array 1 .. N, 1 .. N of boolean
var nodes : array 1 .. N of Node

for i : 1 .. N
    for j : 1 .. N
        connected (i, j) := abs (i - j) = 1
    end for
    nodes (i).x := (maxx / (N - 1)) * (i - 1)
    nodes (i).px := (maxx / (N - 1)) * (i - 1)
    nodes (i).y := maxy / 2
    nodes (i).py := maxy / 2
    nodes (i).vx := 0
    nodes (i).vy := 0
end for


function whatAngle (dx, dy : real) : real
    var ratio, angle : real
    if abs (dx) > 0.0000001 then
        ratio := dy / dx
    else
        ratio := dy / 0.000001
    end if
    angle := arctand (abs (ratio))
    if dx < 0 then
        angle := 180 - angle
    end if
    if dy < 0 then
        angle := 360 - angle
    end if
    result angle
end whatAngle

loop
    mousewhere (mx, my, md)

    for i : 1 .. N
        nodes (i).vx := nodes (i).vx * decay + wind
        nodes (i).vy := nodes (i).vy * decay + g
        for j : -1 .. 1 by 2
            if i + j <= N & i + j >= 1 & connected (i, i + j) then
                F := -k * (Math.Distance (nodes (i + j).x, nodes (i + j).y, nodes (i).x, nodes (i).y) - d)
                nodes (i).vx += cosd (whatAngle (nodes (i).x - nodes (i + j).x, nodes (i).y - nodes (i + j).y)) * F
                nodes (i).vy += sind (whatAngle (nodes (i).x - nodes (i + j).x, nodes (i).y - nodes (i + j).y)) * F
            end if
        end for
        F := -k * (Math.Distance (nodes (i).px, nodes (i).py, nodes (i).x, nodes (i).y) - d) / 50
        nodes (i).vx += cosd (whatAngle (nodes (i).x - nodes (i).px, nodes (i).y - nodes (i).py)) * F
        nodes (i).vy += sind (whatAngle (nodes (i).x - nodes (i).px, nodes (i).y - nodes (i).py)) * F
    end for

    for i : 1 .. N
        x (i) := nodes (i).x div 1
        y (i) := nodes (i).y div 1
        if i ~= holding then
            if i ~= 1 & i ~= N then
                nodes (i).x += nodes (i).vx
            end if
            nodes (i).y += nodes (i).vy
            if (nodes (i).x < radius| nodes (i).x > maxx - radius) then
                nodes (i).vx *= -1
            end if
            if (nodes (i).y < radius| nodes (i).y > maxy - radius) then
                nodes (i).vy *= -1
            end if
            if (nodes (i).x < radius| nodes (i).x > maxx - radius)| (nodes (i).y < radius| nodes (i).y > maxy - radius) then
                nodes (i).vx *= wallFriction
                nodes (i).vy *= wallFriction
            end if
            nodes (i).x := min (max (radius, nodes (i).x), maxx - radius)
            nodes (i).y := min (max (radius, nodes (i).y), maxy - radius)
        end if
    end for
    x (N + 1) := maxx
    x (N + 2) := 0
    y (N + 1) := 0
    y (N + 2) := 0
    drawfillpolygon (x, y, N + 2, blue)

    if md > 0 then
        if holding = -1 then
            for i : 1 .. N
                if Math.Distance (mx, my, nodes (i).x, nodes (i).y) <= radius & i ~= 1 & i ~= N then
                    holding := i
                    exit
                end if
            end for
        end if
    else
        if holding ~= -1 then
            nodes (holding).vx := mx - mxl
            nodes (holding).vy := my - myl
        end if
        holding := -1
    end if
    if holding ~= -1 then
        nodes (holding).x := mx
        nodes (holding).y := my
    end if
    mxl := mx
    myl := my

    Time.DelaySinceLast (10)
    View.Update
    cls
end loop

Author:  GregLP [ Wed Jan 18, 2006 11:41 pm ]
Post subject: 

Wow!

This is cool! Add some crash detection and you could have a cool water effect!

It would be nice to here a brief explanation of the general programming but this is awesome!

Im gonna give ya bits but I don't have too much 2 give

Author:  Delos [ Thu Jan 19, 2006 8:43 am ]
Post subject: 

Ah zylum, you creative hijinks! Ok, that one was a little buggy, especially when one pulls a wave from the right all the way to the left, then lets go...

Oh, and GregLP - it was a nice thought for you to give bits to zylum, but it will make no difference. He has mod-like status, hence bits are like cotton candy to him - the thought appreciated, but ultimately will get stuck in his teeth.

Author:  codemage [ Thu Jan 19, 2006 1:12 pm ]
Post subject: 

Delos wrote:
a little buggy, especially when one pulls a wave from the right all the way to the left, then lets go...


Or pulls "air" down into the water. Wink

Still - a good effect. It's amusing to see all these variations of the same general chunk of code.

Author:  iker [ Thu Jan 19, 2006 4:29 pm ]
Post subject: 

I found it to be quite amusing, but very buggy as well.. but who cares, anything that good looking on turing is fine be me. Would have been cool if you could get a clear, multi coloured water, but since its using Draw.Polygonfill, I guess that would be hard... anyways, its still great.

Author:  Rasta Fella [ Thu Jan 19, 2006 4:56 pm ]
Post subject: 

Still quite good, despite the fact that the white spaces appear in the water. If you could figure a way to make the movement smooth that would be a good hence the word "wave" effect. Overall Great Job. +Bits

Author:  sylvester-27 [ Thu Jan 19, 2006 5:37 pm ]
Post subject: 

delos just said above that giving bits won't make a difference. anyway wicked code. u could (if you felt like it) put like a pirate ship on it and see how long it would take for the user to sink it. but still awsome code

Author:  Delos [ Thu Jan 19, 2006 6:51 pm ]
Post subject: 

Rasta Fella wrote:
+Bits


Rolling Eyes

Now, now Rasta Fella. You've been here long enough to figure out what was wrong with what you just did... Laughing

Author:  MovieTheatre [ Thu Jan 19, 2006 8:34 pm ]
Post subject: 

Thanks A Billion

You Guyz R DoinG VerY GooD JoB

Author:  Delos [ Thu Jan 19, 2006 11:21 pm ]
Post subject: 

What do you mean by 'Thanks'? That sounded very suspicious. [shifty eyes].

Stealing is bad. Don't do it.

Author:  codemage [ Fri Jan 20, 2006 10:36 am ]
Post subject: 

Thanks for clearing up the bits issue, delos.

As a reward for your good moderation,
+bits

Wink

Author:  Rasta Fella [ Fri Jan 20, 2006 3:27 pm ]
Post subject: 

Delos wrote:
Rasta Fella wrote:
+Bits


Rolling Eyes

Now, now Rasta Fella. You've been here long enough to figure out what was wrong with what you just did... Laughing


I know he already has a set bit amount..duh...but I still appreciate what he did..no matter if the bits count or not. It's not much of a loss to me because bits=fake money.

Author:  chrispminis [ Sat Jan 21, 2006 12:17 am ]
Post subject: 

No, no, bits can buy you a lot here at compsci, say like my soul, or my body or jujubes. You want jujubes don't you?

Yeah, well so this reply has a point, zylum is a god. I don't even know where you get some of your ideas, and why you decide to do them in Turing.

Author:  Mr. T [ Sun Jan 22, 2006 12:58 am ]
Post subject:  Alex's Opinion

chrispminis wrote:
and why you decide to do them in Turing.

easy syntax

Author:  Delos [ Sun Jan 22, 2006 9:30 am ]
Post subject: 

chrispminis wrote:
and why you decide to do them in Turing.


So others know that even with crappy tools, masterpieces may be forged.


: