Computer Science Canada

Elasticity in rope simulation

Author:  TheOneTrueGod [ Sat Apr 22, 2006 10:23 am ]
Post subject:  Elasticity in rope simulation

A rope simulator. Simulates very elastic rope. Astonishing Wink . the rope is tied to the mouse and there is a weight on the end. If the weight sinks below the length of the rope, tension pulls it back up, and if its above, gravity pulls it down. Use w/a/s/d to apply a force on the rope.

code:

const Tension := 1.3
const Length := 50
const maxspeed := 10
const gravity := 0.7
const loss := 1.1

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


var mx, my, button : int
var x, y, xs, ys : real := 0
x := 100
y := 100

var keys : array char of boolean

View.Set ('offscreenonly')

loop

    mousewhere (mx, my, button)
    Input.KeyDown (keys)

    x += xs
    y += ys

    if keys ('d') then
        xs += 1
    elsif keys ('a') then
        xs -= 1
    end if

    if keys ('s') then
        ys -= 1
    elsif keys ('w') then
        ys += gravity * 0.9
    end if

    ys -= gravity

    drawline (mx, my, round (x), round (y), black)
    drawfilloval (round (x), round (y), 5, 5, black)

    xs /= loss
    ys /= loss

    if Dist (x, y, mx, my) > Length then
        xs += cosd (Angle (x, y, mx, my)) * Tension * Dist (x, y, mx, my) / Length
        ys += sind (Angle (x, y, mx, my)) * Tension * Dist (x, y, mx, my) / Length
    end if
    View.Update
    Time.DelaySinceLast (30)
    cls
end loop

Author:  Kactus [ Sat Apr 22, 2006 2:53 pm ]
Post subject: 

wow this is cool! i played with it for a good 30 mins! lol

Author:  upthescale [ Sat Apr 22, 2006 3:44 pm ]
Post subject: 

wow do u have a life man i looked at that code with aw

[mod:0f446bb8df="Cervantes"]
Nice comment. You have been warned a great many times about breaking [the rules]. In this case, you are breaking the flaming rule. In other words, you are flaming. That's not allowed.

As I was saying, you have been warned a great many times. Continue and you will be banned.

Furthermore, what is "aw"?
[/mod:0f446bb8df]

Author:  MysticVegeta [ Sun Apr 23, 2006 10:38 am ]
Post subject: 

I think he forgot an e? but anyways, nice code man, as soon as I finish trig and start grade 11 physcis LOL, I would try to make similar programs, really cool, the concept could be extended for the use of AI and missile tracker? lol Laughing

Author:  TheOneTrueGod [ Sun Apr 23, 2006 1:54 pm ]
Post subject: 

Thanks for the praise. Yes, the concepts shown here areused in all of my AI / tracking. This specific example, I was planning on using in a spiderman-esque game.

sin(theta) and cos(theta) are really fun concepts (I find), as they can even apply to 3-D systems (As is shown in my RPG Shameless Plug Very Happy )

Author:  Clayton [ Mon Apr 24, 2006 6:35 pm ]
Post subject: 

very cool man, i cant wait until i learn the physics and stuff to be able to do that, then ill be able to do stuff like that, very cool, although i think you should be able to like sling the elastic across the screen with w|a|s|d keys, but meh, gj

Author:  rownale [ Fri Apr 28, 2006 3:42 pm ]
Post subject: 

cool game/program
sort of like this one
http://www.teagames.com/games/chuck/play.php

wish i knew how to do that stuff

Author:  BenLi [ Sun Apr 30, 2006 4:48 pm ]
Post subject: 

very nice, any games planned form this?


: