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

Username:   Password: 
 RegisterRegister   
 Ball bouncing effect
Index -> Programming, Turing -> Turing Submissions
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
NikG




PostPosted: Mon May 15, 2006 6:57 pm   Post subject: Ball bouncing effect

Something I was just playing with... a ball falling and bouncing off the bottom of the screen. I tried to simulate friction and gravity.
code:
View.Set ("offscreenonly")

var BallX := 50.0
var BallY := 200.0 %starting ball height
var maxBallY := BallY
var newVelMplr := 0.069 %0.068 to 0.075

var VelX := 2.5
var VelY := 0.0
var Gravity := -0.6 %good values from -0.49 to -0.71
var Friction := -0.09

loop
    BallX += VelX
    BallY += VelY
    if BallY > maxBallY then
        maxBallY := BallY
    end if
    VelY += Gravity
    if round (BallY) <= 12 then
        VelY := maxBallY * newVelMplr
        maxBallY := 0
        if VelX > 0 then
            VelX += Friction
        end if
    end if

    drawfilloval (round (BallX), round (BallY), 10, 10, green)
    View.Update

    exit when VelX <= 0
    delay (20)
    drawfilloval (round (BallX), round (BallY), 10, 10, white)
end loop

Anything wrong with this approach?
Sponsor
Sponsor
Sponsor
sponsor
blaster009




PostPosted: Mon May 15, 2006 7:21 pm   Post subject: (No subject)

Looks fine to me! Smile
upthescale




PostPosted: Mon May 15, 2006 7:24 pm   Post subject: (No subject)

Cool man nice effect
Darkmantis




PostPosted: Wed May 24, 2006 12:15 pm   Post subject: (No subject)

This is awesome! I've been trying to figure this out for so long, Yay my search is over Very Happy!
Remm




PostPosted: Wed May 24, 2006 3:38 pm   Post subject: (No subject)

thats awsome; you should put somthing in so you can drag it up 'n drop it.
Aziz




PostPosted: Wed May 24, 2006 8:49 pm   Post subject: (No subject)

Good. Gravity is 9.8 though, in physics Razz Just playing, it's nice. Shouldn't be hard to convert to drag-and-drop.
TheOneTrueGod




PostPosted: Wed May 24, 2006 9:30 pm   Post subject: (No subject)

Yarr, what people don't realise is that gravity is 9.8 metres / second Code doesn't work in metres per second, it works in pixels per loop execution. You could do some calculations to convert it, but for a simple effect like this, it isn't worth it Razz

Also, the diminishing returns (I believe thats what its called) of the ball's movement doesn't work the way you have it. It should be something more along the lines of

code:

%Pseudo Code
if the ball hits the ground then
    ball's yspeed *= -0.9 %Or some other value that is approximately = 1
end if

This way, the ball's speed is adjusted properly.

Other than that, nice work.  This is the simplest application of vector movement, but its a good example to refer back to when the more complicated stuff fails ya. :P
Aziz




PostPosted: Thu May 25, 2006 6:26 am   Post subject: (No subject)

Yeah, I realized that. Figured that. And tried it. It just goes flump. You'd have to make a scale (1 metre = 50 px?) and then could would work Very Happy
Sponsor
Sponsor
Sponsor
sponsor
TheOneTrueGod




PostPosted: Thu May 25, 2006 7:03 am   Post subject: (No subject)

1 metre for 50 pixels? wow Shocked dude, my monitor is 1200 pixels across, and its not even half a metre. Depending on your resolution, the scale would have to be different, so that it would work on all computers. You'd have to set the screensize to max/max, you would have to KNOW the dimensions of their monitor, and not to mention the fact that g is not a constant, and it changes as your distance from the earth changes. Also, just converting it from metres to pixels doesn't work either, because you still have to convert time to a relative one. You would have to know the frames per second that your program is operating at, then you would have to factor that into the conversion. Theres a lot more to think about than just plugging in the value of 9.8 and assuming itll work Razz. In my experiences, (using my formulae, though) the best g to work with is one of 0.3-0.5.
Aziz




PostPosted: Thu May 25, 2006 7:10 am   Post subject: (No subject)

Lol, no, sorry if I didn't clarify, I meant using a scale to represent 1 metre as 50 pixels (like you would do on a graph. Such as 'let 1cm represent 1m'). And the value of g isn't constant, you're right, but it's basically irrelevant to include to difference for this purpose Razz Then again, it's irrelevant to put this into terms of real physics, too Razz When it's so much easier to just do it so it works out Razz. I was just experimenting, as yours/his method is *muchos* more effecient. But hey, >_> I always think of those "hey I wonder how we could do that..." thinks. Then I get worked up and can't sleep (generally I think about these things while lying in bed lol)
sylvester-27




PostPosted: Thu May 25, 2006 7:30 am   Post subject: (No subject)

actually the maximum velocity for objects falling to earth is 9.81 m/s. Very Happy ya the ball falling thing is pretty good
Aziz




PostPosted: Thu May 25, 2006 2:21 pm   Post subject: (No subject)

sylvester-27 wrote:
actually the maximum velocity for objects falling to earth is 9.81 m/s. Very Happy ya the ball falling thing is pretty good


You mean acceleration Razz Acceleration due to gravity is 9.8m/s^2. Velocity for a falling object at any given time is HEIGHT - 9.8 * TIME (given the object started at rest) Wow, why are we debating physics?
NikG




PostPosted: Fri May 26, 2006 11:40 pm   Post subject: (No subject)

I tried adding the Mouse drag code and it works for the most part:
code:
View.Set ("offscreenonly")

var x, y, b, b2 : int
var BallInHand := false
var BallInMotion := true

var BallX := 50.0
var BallY := 200.0 %starting ball height
var maxBallY := BallY
var newVelMplr := 0.068 %0.068 to 0.075

var VelX := 2.0
var VelY := 0.0
var Gravity := -0.6 %good values from -0.49 to -0.71
var Friction := -0.11

loop
    Mouse.Where (x, y, b)

    if Mouse.ButtonMoved ("down") and Math.Distance (x, y, BallX, BallY) <= 10 then
        Mouse.ButtonWait ("down", x, y, b, b2)
        BallInHand := true
    elsif Mouse.ButtonMoved ("up") and BallInHand then
        Mouse.ButtonWait ("up", x, y, b, b2)
        BallInHand := false
        BallInMotion := true
        maxBallY := y
        VelX := 2
        VelY := 0
    elsif Mouse.ButtonMoved ("downup") then
        Mouse.ButtonWait ("downup", x, y, b, b2)
    end if

    if BallInHand then
        BallX := x
        BallY := y
    else
        if BallInMotion then
            VelY += Gravity
            BallX += VelX
            BallY += VelY
            if BallY > maxBallY then
                maxBallY := BallY
            end if
            if round (BallY) <= 30 then
                if VelX > 0 then
                    VelY := maxBallY * newVelMplr
                    maxBallY := 0
                    VelX += Friction
                else
                    BallInMotion := false
                end if
            end if
        end if
    end if

    drawline (0, 20, maxx, 20, black)
    drawfilloval (round (BallX), round (BallY), 10, 10, green)
    View.Update
    delay (35)
    drawfilloval (round (BallX), round (BallY), 10, 10, white)
end loop

There are a few problems though... for some reason, the ball seems to jump higher if you drop it from too high, and I still haven't gotten a good feel for friction.
_justin_




PostPosted: Mon May 29, 2006 7:35 pm   Post subject: (No subject)

wow man thats pretty awesome real nice work keep it up
sylvester-27




PostPosted: Wed May 31, 2006 7:34 am   Post subject: (No subject)

dunno, we just finished the Motion unit of gr 10 science. i can't believe i said maximum velocity. i think like the max v for a human is like 200 km/h.
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 2  [ 17 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: