
-----------------------------------
baozilla
Tue May 16, 2006 8:53 am

Help with bounce
-----------------------------------
i found this code and i want some help with it



setscreen ("offscreenonly") 

type coords : 
    record 
        x : real 
        y : real 
    end record 

const xdecay := 0.99 
const ydecay := 0.99 
const gravity := -0.1 
const balls := 35 
const speed := 5 
const ballRadius := 15 
const elasticity := 1.6 

var ball : array 1 .. balls of coords 
for i : 1 .. balls 
    ball (i).x := maxx div 2 
    ball (i).y := maxy div 2 
end for 
var dx, dy : real := 0 
var oldx, oldy : int 
var clr : array 1 .. balls of int 
var mx, my, md : int 
var holding : boolean := false 

fcn checkIfHolding (x, y : int) : boolean 
    if Math.Distance (ball (1).x, ball (1).y, x, y) < ballRadius and md = 1 then 
        holding := true 
        result true 
    else 
        result false 
    end if 
end checkIfHolding 

proc updateBalls 
    mousewhere (mx, my, md) 
    if md = 1 then 
        if checkIfHolding (mx, my) or holding = true then 
            dx := 0 
            dy := 0 
            ball (1).x := mx 
            ball (1).y := my 
        else 
            holding := false 
            ball (1).x += dx 
            ball (1).y += dy 
            dx *= xdecay 
            if abs (dy) > 1 or ball (1).y > 1 then 
                dy += gravity 
            else 
                ball (1).y := 0 
            end if 
            dy *= ydecay 
        end if 
    else 
        if holding = true then 
            dx := mx - oldx 
            dy := my - oldy 
        end if 
        holding := false 
        ball (1).x += dx 
        ball (1).y += dy 
        dx *= xdecay 
        if abs (dy) > 1 or ball (1).y > 1 then 
            dy += gravity 
        else 
            ball (1).y := 0 
        end if 
        dy *= ydecay 
    end if 
    for i : 2 .. balls 
        ball (i).x -= (ball (i).x - ball (i - 1).x) / speed 
        ball (i).y -= (ball (i).y - ball (i - 1).y) / speed 
    end for 
    if ball (1).x < 0 or ball (1).x > maxx then 
        dx *= -xdecay 
        if ball (1).x < 0 then 
            ball (1).x := 0 
        else 
            ball (1).x := maxx 
        end if 
    end if 
    if ball (1).y < 0 then 
        dy *= -elasticity 
        if ball (1).y < 0 then 
            ball (1).y := 0 
        end if 
    end if 
    oldx := mx 
    oldy := my 
end updateBalls 

proc drawBalls 
    for decreasing i : balls .. 1 
        drawfilloval (round (ball (i).x), round (ball (i).y), ballRadius - i, ballRadius - i, clr (i)) 
    end for 
end drawBalls 

proc makeColors (clrs : int) 
var a : real := 0.6 
    for i : 1 .. clrs 
        %clr (i) := RGB.AddColor (i / clrs, i / clrs, i / clrs) 
        clr (i) := RGB.AddColor (a, a,a+ i / clrs) 
    end for 
end makeColors 

makeColors (balls) 
loop 
    updateBalls 
    drawBalls 
    View.Update 
    delay (10) 
    cls 
end loop 


it bounces but frequently staying in the same spot, is it possible to make it move from side to side without the mouse drag. please help and thnx

-----------------------------------
XeroX
Thu May 18, 2006 2:50 pm


-----------------------------------
It won't even work for me, says 'Distance' is not in the export list of 'Math'

-----------------------------------
Clayton
Thu May 18, 2006 2:59 pm


-----------------------------------
what version of Turing do you have? if it is an older version it might not have the Math.Distance function for you to use, an alternative to using Math.Distance is... making your own!! its simple here:


fcn Math_Distance(x1,y1,x2,y2:int):real
    result sqrt((x2-x1)**2+(y2-y1)**2)
end Math_Distance

see simple :D
