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

Username:   Password: 
 RegisterRegister   
 Bouncing ball
Index -> Programming, Turing -> Turing Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Token




PostPosted: Wed Sep 21, 2005 8:36 pm   Post subject: Bouncing ball

okay, heres my problem, I'm working on a little program that has a ball bouncing. I'm trying to make it realistic so that the ball falls to rest after loosing momentum, take a look at the code, and see if you can find out how to make it eventually stop bouncing. **you can drag the ball higher to drop it from higher.

code:
setscreen ("Graphics:max;max,offscreenonly")

var x, y, acc, gravity : real
var mx, my, mb, rad, dir : int


rad := 10
acc := 0
dir := -1
gravity := 4.9
x := maxx div 2
y := maxy - rad - 500

drawfillbox (0, 0, maxx, 40, black)



loop
    mousewhere (mx, my, mb)


    if mb = 1 and whatdotcolor (mx, my) = red then        %Draging the ball

        loop
            mousewhere (mx, my, mb)
            exit when mb = 0
            x := mx
            y := my
            cls
            drawfillbox (0, 0, maxx, 40, black)
            drawfilloval (round (x), round (y), rad, rad, red)
            View.Update
        end loop
        dir := -1
        acc := 0

    elsif dir = 1 and acc <= 0 then                      %When ball is going up and starts to fall
        dir := -1
        acc := 0

    elsif dir = -1 and y - rad - acc <= 40 then            %when ball bounces

        y := 40 + rad
        dir := 1

    else                                                %ball falling
        y += acc * dir
        acc += gravity * (-1 * dir)
    end if

    cls
    put "y: ", y
    put "acc: ", acc
    put "dir: ", dir
    drawfillbox (0, 0, maxx, 40, black)
    drawfilloval (round (x), round (y), rad, rad, red)

    View.Update
    delay (50)
end loop


Thanks in advance
Sponsor
Sponsor
Sponsor
sponsor
[Gandalf]




PostPosted: Wed Sep 21, 2005 9:14 pm   Post subject: (No subject)

I don't know what about half your code is supposed to do - use meaningful var names, etc.

Your gravity is currently not doing anything. Outside of your if structure, you need something of this sort:
code:
acc -= gravity + dir

Then you need to make the gravity something below 1.

Lastly, your variables are not of the type I would use for this kind of program.
beard0




PostPosted: Wed Sep 21, 2005 9:28 pm   Post subject: (No subject)

Here you go. I Took your code and added in a bit more 'realism' of the physical world.

code:
setscreen ("Graphics:max;max,offscreenonly")

var x, y, vy, gravity, airResistance, bounceNonElasticity : real
var mx, my, mb, rad : int


rad := 10
vy := 0
airResistance := 0.5
bounceNonElasticity := 0.5
gravity := 4.9
x := maxx div 2
y := maxy - rad - 200

drawfillbox (0, 0, maxx, 40, black)


proc stats
    locate (1, 1)
    put "y: ", y
    put "vy: ", vy
end stats
loop
    mousewhere (mx, my, mb)
    if mb = 1 and whatdotcolor (mx, my) = red then        %Draging the ball
        loop
            mousewhere (mx, my, mb)
            exit when mb = 0
            x := mx
            y := my
            cls
            drawfillbox (0, 0, maxx, 40, black)
            drawfilloval (round (x), round (y), rad, rad, red)
            View.Update
        end loop
        vy := 0
    end if
    if y - rad + vy <= 40 then            %when ball bounces
        vy := max (abs (vy) - bounceNonElasticity, 0)
        y := 40 + rad
    else
        vy := vy - gravity
        if vy > 0 then
            vy := max (0, vy - airResistance)
        elsif vy < 0 then
            vy := min (0, vy + airResistance)
        end if
        y := max (y + vy, 40 + rad)
    end if
    cls
    stats
    drawfillbox (0, 0, maxx, 40, black)
    drawfilloval (round (x), round (y), rad, rad, red)

    View.Update
    delay (50)
    exit when vy <= 0 and y = 40 + rad
end loop
Token




PostPosted: Wed Sep 21, 2005 9:32 pm   Post subject: (No subject)

hmmmn, doesent work for me, it just falls and stops
[Gandalf]




PostPosted: Wed Sep 21, 2005 9:34 pm   Post subject: (No subject)

It works fine for me, it bounces, then stops when there is no more velocity+.
beard0




PostPosted: Wed Sep 21, 2005 9:34 pm   Post subject: (No subject)

You mean that for you it doesn't even bounce once? I get 6 bounces before it comes to rest. Did you copy my code exactly?
Token




PostPosted: Wed Sep 21, 2005 9:36 pm   Post subject: (No subject)

yes 100% exactly, a couple of times, into new windows
beard0




PostPosted: Wed Sep 21, 2005 9:39 pm   Post subject: (No subject)

Likely your Turing is screwed up then, because the code works perfectly for me, and for [Gandalf] (apparently).
Sponsor
Sponsor
Sponsor
sponsor
Mr. T




PostPosted: Wed Sep 21, 2005 9:52 pm   Post subject: Alex's Opinion

It also just stops after one bounce at the beginning of the program for me. The only way I can get it to start bouncing is by catching and letting it go before it falls the first time.
EDIT The reason is because the original y velocity is set to 0. Change it to 10.
EDIT 2 Yay, 20 bits for me. Smile
beard0




PostPosted: Wed Sep 21, 2005 9:55 pm   Post subject: (No subject)

This is just weird. I tried closing Turing, getting back in and copying the program from my post, and I still get 6 bounces. 20 bits to whoever figures this out.
StarGateSG-1




PostPosted: Thu Sep 22, 2005 10:05 am   Post subject: (No subject)

What do you mean you only got 6 bounces, every time, that isn't good, balls bounce based on there height and a portion of there height is reduced every bounce if the screen was bigger you should be able to get more bounces I tryed it form higher and I got 8 but it is hard to see the last few. Also balls drop in a straight line but after the first bounce they don't come down straight, more in a parabola.

Diable the exit when for testing.
beard0




PostPosted: Thu Sep 22, 2005 11:45 am   Post subject: (No subject)

I mean 6 bounces with the original height. And actually, as the "ground" is perfectly flat, it will bounce straight up and then back down. Only if the ground were on a slight angle would we have to worry about horizontal motion, creating your parabolic path. This is not a perfect model, either, and I know that. I am majoring in physics-math at U of Ottawa. The air resistance should change with velocity (this is why you get terminal velocity, when you get to the point that you are going so fast that air resistance equals gravity).
StarGateSG-1




PostPosted: Thu Sep 22, 2005 12:55 pm   Post subject: (No subject)

The ground isn't perfectly flat, the world isn't perfectly flat, even if it looks flat. It isn't! I know you know the model isn't perfect, but that is why it bounces 6 times. becasue it isn't perfect!
codemage




PostPosted: Thu Sep 22, 2005 1:03 pm   Post subject: (No subject)

A decayed height of a ball bounding forms a parabola if you're charting it as height(y) and time(x)... if that's what was meant.

Re: the curvature of the earth, the planet can be considered flat, for all intents and purposes. Because the ball is being dropped towards the very centre of the Earth (that's where gravity is pulling it), they hit dead on - parallel to the gravity vector, so there shouldn't be any roll.

(In the ideal model).
beard0




PostPosted: Thu Sep 22, 2005 3:31 pm   Post subject: (No subject)

Thanks, codemage.

On top of which, I never claimed that the ground was flat, but merely that the "ground" was flat, which most people would take to mean the surface from which the ball is bouncing in the model. It is clearly flat as it is drawn with a drawfillbox.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 17 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: