A Flash Developer Resource Site

Results 1 to 16 of 16

Thread: bouncing ball

  1. #1
    Junior Member
    Join Date
    Dec 2002
    Posts
    7

    bouncing ball

    hi all

    first off i'd just like to say that i'm not very good at programming. so if my logic sounds wack, it's cuz it is

    i read twistedmind's recent post about how to determine yposition and such. i'm currently trying to use 3dfa to animate a ball bouncing up and down correctly. my goal for now is to have a ball dropped from an initial height, hit the SAME spot each time before bouncing back up, bouncing less higher each time due to gravity and eventually coming to a stop. however, i'm a little fuzzy on how to properly incorporate physics into a flash file using 3dfa.

    initially i created a list of actions that looked like this:
    get yposition
    if yposition >= 'value' (if ball value)
    yvelocity = yvelocity * 0.8 (decay ball velocity by a %)
    -yvelocity (switch direction of ball)
    if yvelocity < 10 and yvelocity > 10
    y velocity and acceleration = 0 (stop ball)

    i also set parent movie variables for the ball's yvelocity and yacceleration

    at best, the first IF statement attempts to cause the ball to bounce less and less each time. i set the action to run 50 times/sec...i'm not too sure what this does exactly but, if i set it to say 10 time/sec, the ball will overshoot the desired boundary before boucing back up again. i used the two_ball_boucing sample that came with the prog as reference, but i'm not sure if this is the most effective way to get what i want.

    the second IF statement is used to stop the ball from sliding off the screen when it stops bouncing. i do not know why the ball would slide off in the first place and it was getting on my nerves for a while.

    i'm not too sure how y velocity and y acceleration properties work in 3dfa. for instance, if i set a ball to have an intial velocity of 10...what are the units? for acceleration? assuming it's 10 pixels/sec then if i set initial velocity to 10 pix/sec and acceleration to be 2 pix/sec^2, then after 5 seconds will the ball have a new velocity of 20pix/sec? do i need to consider how many times per sec the action is running? or do i need to perform all calculations manually. if so then i guess i need to rethink my coding logic.

    i'm only concerned with the ball's position along the yaxis...for now.

    as i type this, i'm getting a few ideas in my head of which direction to take. i know i asked alot of questions...any input at all for any of the questions would be appreciated.

    sanou

  2. #2
    Senior Member kusco's Avatar
    Join Date
    Nov 2001
    Posts
    681
    Hello sanou,

    I've put together a little example of a bouncing myself. I've used acceleration and velocity. I've only used javascript and there's comments throughout to hopefully explain what I'm doing.

    I'm not 100% sure of the answer to the question you have about the velocity and acceleration but I believe that you are right in that the velocity of the ball will be 20pix/sec after 5 secs.

    I hope that my example helps. One thing to note about the ball is that I've placed the center marker (for lack of another name) at the base of the ball. Normally I put such things in the center of the object I'm moving but in this case it just made a lot more sense to move it at the bottom.
    Attached Files Attached Files
    Cheers,
    kusco
    (3DFA Support Team)

  3. #3
    hates sounds in preloaders.
    Join Date
    Nov 2002
    Location
    Sweden
    Posts
    361
    in my bouncing ball, i started to use javascript loops instead. It worked a lot better, since they go many more times per sec.
    hurg.

  4. #4
    Senior Member kusco's Avatar
    Join Date
    Nov 2001
    Posts
    681
    Hello Joel,

    Not sure what you mean by using javascript loops??

    If you use loops in javascript the entire flash movie will wait (pause) until the loop is finished. This can make your movie appear to be sluggish and/or jerky. Also, if you have an infinite loop the flash player will eventually ask you if you would like to terminate the script.
    Cheers,
    kusco
    (3DFA Support Team)

  5. #5
    hates sounds in preloaders.
    Join Date
    Nov 2002
    Location
    Sweden
    Posts
    361

    Oooops...

    Sorry... Forgot to test it in browser. But in the program it worked fine...
    hurg.

  6. #6
    Junior Member
    Join Date
    Dec 2002
    Posts
    7

    still chuggin away, trying to get bouncing ball to work

    ok i just typed an essay-like entry, but pressed back on my browser...when i pressed forward all my text was gone...i guess it's a sign from above that i should be more concise

    -thx kusco for ur java script example. i've included a file that shows how i made a list of actions that's bascially identical to what you were trying to do, however i did not decrease the ball's bounce. aside from the fact that it will never stop bouncing, i like the ball's motion...so it's a keeper for now.

    some new questions i have regarding bouncing balls...
    -is it possible to 'create' gravity? i just realized that the yacceleration values i assign to the ball...are characteristic of the ball itself...not some independent value. i'm slow to pick up on these things so...
    -how feasible would it be to implement kinematics into what i'm trying to do? like i've been trying to incorporate the following equations w/o much success...

    v(at time t) = v(initial) + at
    v(final)^2 = v(initial)^2 + 2a * (delta x)

    i thought this approach would work for sure...but then i encountered problems such as determining time (another post) and getting proper delta x's...which leads to another question...if i were to try to use kinematics, then would it be better to use frame animations instead of time based animations? cuz i was thinking i could use pixel/frame instead of pixel/second...which would make things slightly easier. just a thought.

    oh and one more thing. i was messing around with your version of the bouncing ball, kusco. i tried toying with the velocity and acceleration variables...with mixed results. sometimes the ball would go haywire, sometimes it'd pull a series of rapid bounces then slowly edge off the bottom of the screen...sometimes it'd act normal for the first few bounces then trip out completely. these problems are mirrored in my attached version as well...when i try to mess around with my own variables.

    i shy from java because quite frankly, i don't know any. i understand basic c++ at best.
    Attached Files Attached Files

  7. #7
    Senior Member kusco's Avatar
    Join Date
    Nov 2001
    Posts
    681
    Hello sanou,

    Hmmmm, kinematics...a very interesting topic indeed. Unfortunately I know next to nothing about it but am willing to put some energy into it.

    You have quite a number of questions there let's see...

    Is it possible to create gravity?
    I think that you've done a pretty good job with the use of the y.acceleration value that you've used. I'm not sure how you would go about creating a second value to control the strength of gravity.
    If you apply the same y.accelaration value to all objects then wouldn't this have the effect you're looking for?

    How feasible is kinematics...?
    I'm not sure of the formula you've posted. I'm a little on the
    v(final)^2 = v(initial)^2 + 2a * (delta x)
    In particular the bit at the beginning v(final)^2. I thought that '^' was to mean 'to the power of'??? If I understood the formula better then I might be of more help here.
    I've only looked at what kinematics is. I've wanted to use it myself in some animations but quickly fell into despair when I saw how complex the mathematics appeared to be. (notice how I said 'appeared') The mathematics may not be that complex but my understanding of it was/is limited.
    Someone-else in this forum may be able to assist perhaps?

    Pixel/frame vs. Pixel/second
    This I don't know. I've only used Pixel/second for my movies and have yet to have a go with pixel/frame.

    bouncing ball...
    I've also noticed that the ball can behave eratically when the values are changed.

    Looking at the version you supplied in your last post I thought it looked good. I've made a couple of changes to it...
    1. added a base line to see where the ball is bouncing.
    2. changed the compare from 220 to 240
    3. added another line to the actionscript to set the value of the balls y position to 239 to prevent the ball from sliding off the bottom of the screen.
    4. added a reduction to the velocity of the ball (+60) when the ball is bouncing upwards.
    5. changed the frame rate of the movie to make it appear smoother
    6. changed the times per second of the actionlist from 10 to 80 so that the checking is done more often.

    Overall, the result is (I think) nice.

    If there's some place on the web you can point me to so that I can pick up on the details for the formulas then that would be appreciated. I'd like to be able to assist where I can.

    I know that I haven't been of much help here but...
    Attached Files Attached Files
    Cheers,
    kusco
    (3DFA Support Team)

  8. #8
    Junior Member
    Join Date
    Dec 2002
    Posts
    7

    i wanna throw in the towel...

    haha, the more i think about the bouncing ball problem, the more addicting it gets...the more frustrating it gets also. guess this is what i get for sleeping thru all those physics classes in college...

    i've been worried about the proper use of vector theory...like the whole point of adding a negative sign to yvelocity when it hits yposition=240 is to change the direction of the vector. however, what puzzles me is how 3dfa's built in yacceleration properties work. When i set yacceleration to be 980, i was thinking it would only act in the positive direction (in this case, downwards). but based on the resulting bouncing ball animation, it seems like 3dfa takes it upon itself to change the direction of the yacceleration vector...

    however, it could also be the case that the sign change used in the IF statement (yvelocity = -yvelocity IF yposition >= 240...) coincidentally caused the yacceleration to appear changed. this leads to yet another question...is there an actual difference between y.acceleration and y.relative.acceleration? if i gave the ball a y.relative.acceleration property, what exactly would it be relative too?

    i've tried in the past to mess around with yacceleration properties...but it's like trying to stick your finger in a car engine while it's running...not pretty

    ...

    as for ur upate on the bouncing ball kusco, what is the purpose of the line that you drew at the bottom? just aesthetic? you might have mentioned it in your previous post and i just missed it.

    and also i don't know if it's my computer, but towards the end of ur animation, the ball jitters alot...

    ...

    i've done a little research on the web and this is what i've come up with so far

    a nice little tutorial about introductory kinematics
    http://physics.webplasma.com/physics04.html#vel

    my goal is to recreate the flash demo on this page...does anyone know what program to use to open .fla files?
    http://www.pipey.com/freeflash/gravity.asp

    this last page is just freaking nuts. whoever programmed this site is 10 million years ahead of me. check out the spring one...it's beyond trippy...just thinking of how he implemented spring properties...holy @#!&
    http://www.illogicz.com/flash5/physicsengine/

    ...

    my goal is still to keep things as simple as possible. i just realized now that by trying to implement physics equations...it might just be a failed attempt at recreating the wheel. sigh.

  9. #9
    KoolMoves Moderator blanius's Avatar
    Join Date
    Jul 2001
    Location
    Atlanta GA
    Posts
    5,244
    So info:

    The Realtive acceration is based on an objects rotation. So a Negative Realive acceration Y would move you up if the rotation is 0 but down if the rotation is 90.

    EXAMPLE OF THIS HERE

    Ironic that this came up. I was playing around with this very thing while doing
    THIS INTRO

    I tried to make use of the friction feature but didn't get that to work.
    Last edited by blanius; 12-30-2002 at 10:06 PM.

  10. #10
    Junior Member
    Join Date
    Dec 2002
    Posts
    7

    kinetic energy?

    ok so i was talking to someone and we were discussing properties of a bouncing ball...and midway through i realized that i've been going about this whole thing in the wrong manner.

    gravity doesn't cause the ball to bounce less every time. instead, the ball loses energy each time it bounces, which in turns, lowers the velocity.

    this relationship is defined as K.E. = 0.5mv^2

    haha, now it's official...i REALLY didn't learn anything in physics...

    so anyways i reworked my bouncing ball and made a few changes...first i made use of the velocity and acceleration parameters that u can assign to an image. second instead of decreasing the velocity each time the ball bounced back up, i calculated the kinetic energy and then took away some of the energy. from there i worked backward to find out the new velocity.

    the only hiccup with this new method is that i'm using an arbitrary value for the mass of the ball. however, when i try to recalculate velocity (v = sqrt(2 K.E. / m) unless i increase the mass (in the denominator) the result will still be a perpetual bouncing ball.

    i'm not the best at explaining things and i'd be glad to reword things.

  11. #11
    Senior Member kusco's Avatar
    Join Date
    Nov 2001
    Posts
    681
    Hello sanou,

    I have somehow missed this post...

    Well...I wish I had paid more attention in my maths classes. In your formula you have the kinetic energy divided by the mass to get your new velocity but there's nothing there to take into account the energy loss...unless you are recalculating the kinetic energy elsewhere??
    Cheers,
    kusco
    (3DFA Support Team)

  12. #12
    A Senior Newbie - How Odd ForumNewbie's Avatar
    Join Date
    Mar 2003
    Posts
    590
    Hope no-one minds me revisiting this thread. I noticed that you can search previous threads, and found one that caught my eye, since I like a mental challenge !

    NOTE FOR BEGINNERS - The search facility on this forum is really useful if you're reluctant to ask a question, since at least you can find out if someone else has already asked it.

    Anyway here is my contribution to the bouncing ball debate:

    Friction/Mass/Energy and Bouncing etc - Elasticity
    --------------------------------------------------
    In this example, the ball needs to lose energy each time it has a collision with the walls or floor.
    If my memory of physics is right, the amount of energy lost is dependent on elasticity of the ball. ie. A rubber ball is pretty elastic, so it loses very little energy, so it would have an elasticity of nearly 1
    A marble may have the same mass and volume and velocity, but would not bounce as high, because it has a lower elasticity, maybe 0.3
    So, in the movie, you need to add a factor into the script that reduces the velocity by an elasticity parameter each time it hits a wall.

    I don't think the complex equations are necessary because:
    The equations for energy do describe the amount of energy possessed by the ball in motion as being related to velocity squared, but I don't think these relate to the loss of energy upon collision at all.
    Also friction with the air is more dependent on surface area, smoothness etc, and should be negligible for the purposes of these examples.


    Simulated Gravity
    -----------------
    Since gravity is measure by an acceleration of ~10 metres per second per second, the problem with simulating this is that the movie has no scale, so it is hard to measure the distance part accurately.
    I'm not sure how accurate things like the 'Frames per second' and getTimer() are, but in theory, these should allow you to control the time part.
    Hence, to accurately simulate gravity, you need something to indicate the scale, and you need to make sure the time measurements are accurate.

    I had a go at reproducing the example that was mentioned earlier in the thread, but I must admit, my gravity and elasticity estimates were somewhat random.
    I've attached it anyway - in case anyone is still interested in this debate. Perhaps it'll make a useful sample for folks just starting to look at scripting?

    Happy bouncing,

    Morgan.
    Attached Files Attached Files
    Please note that my domain has changed to http://www.morganmultinational.com

  13. #13
    KoolMoves Moderator blanius's Avatar
    Join Date
    Jul 2001
    Location
    Atlanta GA
    Posts
    5,244
    You are going to make an interesting addition to the crew around here!

    Nice work. Keep it up.


    Note to all concerned: I've just taken a big job and will not be available around here for at least a month. Ya'll don't get to far ahead of me. I promise to work on the XML based guestbook and the XML news feed stuff as soon as I can.

  14. #14
    A Senior Newbie - How Odd ForumNewbie's Avatar
    Join Date
    Mar 2003
    Posts
    590

    Proud !!

    OK,

    After Brett's kind words, I decided to stick my example on a webpage, so you can compare to the original goal, without downloading the source:

    Remember, this is what we were aiming for:
    http://www.pipey.com/freeflash/gravity.asp

    This is the 3dfa version I created today:
    http://www.morgan-multinational.com/gravitydemo.html

    (See the earlier post for the source movie file in v4 beta 3)

    The original has a slightly smoother throwing action, stronger gravity, and is a bit faster, but I'll bet it was quicker for me to create in 3dfa.
    Just goes to show what can be done in 3dfa on a cold Saturday afternoon!

    Morgan.
    Please note that my domain has changed to http://www.morganmultinational.com

  15. #15
    Senior Member
    Join Date
    Mar 2003
    Posts
    136
    Add a little more decay on the last few bounces when the height < 2% or so and you have it.

    I notice a lot of bounding box sides applications suffer from an oscillatory problem when speed is almost zero and the object is hard up against the wall.

    One tip I think is worth adding. Use a small one pixel square object, invisible to the viewer to be the falling object and use it to detect the wall/floor. The ball or other viewed object can then be placed using the pixel coordinates of the hidden object.

    This also applies to larger non-uniform objects like space ships where the size of their bounding box is too big for accurate detection of impacts etc. So again, use a smaller object and its collision properties to place the space ship and detects hits from missiles etc etc.

  16. #16
    A Senior Newbie - How Odd ForumNewbie's Avatar
    Join Date
    Mar 2003
    Posts
    590
    Some good points there, and good ideas about detecting collisions on odd shaped objects.
    I think my example goes wrong because friction would become significant in this last 2% of movement, and so the bouncing would be dampened at this point.

    Regarding strange phenomena (oscillatory problems) when elements are near the edge of play: When I started to bounce object off the walls in this way, I found that they kept getting stuck to the walls at certain speeds. This was what I found. Hope this helps someone!

    I originally programmed my scripts so that if my element was going out of range, I multiplied the velocity by -1. This didn't always work, and they would get stuck on the boundaries. This is why:

    If an element is moving fast, then if your script says: 'if an element has x<5 then multiply its velocity by -1' then what happens is:

    If your element is moving really fast, and your script is not running that often, then the element's position isn't detected until the element is well out of range, say when x=-20
    Your script detects the motion and the velocity is multiplied by -1
    By the next time the script is run, the element is moving back towards the right range; the x coordinate is nearly back in range, but not quite, say at position x=3
    This still fulfills the criteria x<5, so the velocity is multiplied by -1, and the element starts to move back out of range again.
    This results in your element bouncing around right on the boundary, and never moving back into the real range.

    If you find that your attempts to bounce objects back into play, results in them getting stuck at the boundaries, look at the script for the example above. Making sure that you use the absolute (abs) velocity, multiplied by the direction you want it to go (eg. Multiply your x velocity by +1 to move your element right, and by -1 to move it left) stops your element from sticking to the wall.

    This drove me mad for a while, but made perfect sense when I thought it through. Hope this helps.

    If you're thinking Grandma, eggs, teaching your to, and suck, then that explains why I still call myself ForumNewbie !!

    Morgan.

    (They shouldn't let me log on when I get back from the pub!)


    PS - Here's a real mental challenge for someone. Can you program Newton's cradle in 3dfa? (That executive toy where you have 5 metal balls hung in a frame, and they bounce around in seemingly perpetual motion)
    I was thinking about this last night, and reckoned that this was a real test of both physics knowledge and 3dfa skills.
    I reckon you need to start with a well programmed pendulum, and the rest can be done with collisions, and the transfer of velocity from one element to another. I'm going to try this, so any help would be gratefully received! EDIT: This is the dummy's version: http://jibbering.com/directanimation/nc2.html
    Last edited by ForumNewbie; 04-20-2003 at 08:30 PM.
    Please note that my domain has changed to http://www.morganmultinational.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center