A Flash Developer Resource Site

Page 2 of 2 FirstFirst 12
Results 21 to 35 of 35

Thread: simple physics

  1. #21
    When in doubt ask Eager Beaver's Avatar
    Join Date
    Feb 2007
    Location
    Planet Earth
    Posts
    911
    The problem may not be with the formulas used or the code.
    The problem I think is with the iterations, during which the ball bouncing height slightly increases with each bounce.
    Hope some FLASH expert can give a better explanation, and suggest a solution.

  2. #22
    Senior Member
    Join Date
    Sep 2006
    Posts
    130
    yes that would be nice, any experts out there?

  3. #23
    FK'n_dog a_modified_dog's Avatar
    Join Date
    Apr 2003
    Location
    "aaarf"
    Posts
    9,176
    i have this working in a 3 frame movie, running at 30FPS
    movieclip instance - ball - across all 3 frames
    button instance - start_btn - across all 3 frames
    button instance - reset_btn - across all 3 frames

    Code:
    // frame#1 -
    var t:Number = 0;
    var d:Number = 0.6;
    var g:Number = 9.8; // Acceleration of gravity
    var e:Number = 0.1; // Energy absorption
    var y:Number = ball._y;
    var gr:Number = 300; // ground
    
    btn_start.onRelease = function() {
    gotoAndPlay(2);
    };
    
    btn_reset.onRelease = function() {
    y = ball._y = 70;
    gotoAndStop(1);
    };
    
    stop();
    Code:
    // frame#2 -
    ball._y = Math.min(y+(g/2)*t*t, gr+2);
        if (ball._y >= gr) {
          d = -d;
          y += e*(gr+2-y);
          t = Math.sqrt((gr-y)*2/g);
          if (y >= gr) {
            gotoAndStop(1);
          }
        }
    Code:
    // frame#3 -
    t += d;
    gotoAndPlay(2);
    can you make use of the logic and apply to your onClipEvent method ?

    mx2004 fla
    Last edited by a_modified_dog; 02-20-2007 at 04:54 PM. Reason: fla file version added

  4. #24
    Senior Member
    Join Date
    Sep 2006
    Posts
    130
    I'm not really understanding what's happening. You're setting the ball's position to be the minimum of what it would be according to free fall or the height of the ground + 2. That makes sense except for the +2. Then if the ball hits the ground you're setting d=-d, i'm assuming d's the velocity, and y becomes it self +some energy times what you have. I don't understand what you're doing with time with sqrts or adding d. If you could, could you look at my code and see what I'm doing thats not right?

    // frame#2 -
    ball._y = Math.min(y+(g/2)*t*t, gr+2);
    if (ball._y >= gr) {
    d = -d;
    y += e*(gr+2-y);
    t = Math.sqrt((gr-y)*2/g);
    if (y >= gr) {
    gotoAndStop(1);
    }
    }

  5. #25
    Senior Member
    Join Date
    Sep 2006
    Posts
    130
    I'm going to attach my .fla so you guys can see what my code does. Also note that theres a problem where, if I place the ground at (0,460) ground._y will return 475 for some reason I think. See for yourself
    Attached Files Attached Files

  6. #26
    When in doubt ask Eager Beaver's Avatar
    Join Date
    Feb 2007
    Location
    Planet Earth
    Posts
    911
    Hey! This code seems to be working OK!
    //__________________________________________________ _________
    onClipEvent(load)
    {
    aY = 9.8;
    t = 0;
    v0 = 0;
    v = 0;
    ground=350;
    yb=40;
    yt=40;
    this._y=yb;

    }
    onClipEvent(enterFrame)
    {
    trace(this._y);
    if(this._y<yt){//Stops the ball moving to a height <yt.
    yb=yt;
    t=0;
    v0=0;
    }

    if(this._y > ground)
    {
    t=0;
    v0 = -v;
    yb = ground;
    }

    this._y = yb+v0*t + 1/2 * aY * t*t;
    v = v0 + aY * t;
    t+=0.1;

    }

  7. #27
    Senior Member
    Join Date
    Sep 2006
    Posts
    130
    Thanks, it works but its sloppy. The ball is forced to stop so it doesn't look real. Also, I'm still not sure why its telling me the ground is at 475 when its not.

  8. #28
    When in doubt ask Eager Beaver's Avatar
    Join Date
    Feb 2007
    Location
    Planet Earth
    Posts
    911
    In the .FLA you have the ground._y according to the property box as 475.3
    Please check this up.

  9. #29
    Senior Member
    Join Date
    Sep 2006
    Posts
    130
    When I click on the movieclip which is the ground, the property box shows Y: 460.0. I don't see 475.3 anywhere except for the console window that pops up

  10. #30
    When in doubt ask Eager Beaver's Avatar
    Join Date
    Feb 2007
    Location
    Planet Earth
    Posts
    911
    In scene 1, Layer 1, world
    Properties:
    W:500.0 X: 235.6
    H: 40.0 Y: 475.3
    __________________________
    I have again checked up.
    The above data is indeed shown!

  11. #31
    FK'n_dog a_modified_dog's Avatar
    Join Date
    Apr 2003
    Location
    "aaarf"
    Posts
    9,176
    movieclip - ground is at y:460 on stage
    but if you open the clip, the centre point is set at - y:-15.3

    460 + 15.3 = 475.3

    sloppy huh

    set the clip at zero and use -

    _global.floor = ground._y - ground._height;
    trace(floor); // 420
    trace(ground._y) // 460
    Last edited by a_modified_dog; 02-21-2007 at 03:49 PM.

  12. #32
    Senior Member
    Join Date
    Sep 2006
    Posts
    130
    why would I want the floor to be at 420 if the top of the ground is at 460?

    ps, thanks for catching that, I forgot about where the shape coordinates are

    pps, im starting to not like physics!
    Last edited by jcafaro10; 02-21-2007 at 05:54 PM.

  13. #33
    Senior Member
    Join Date
    Sep 2006
    Posts
    130
    I think i see where the problem lies. If you watch the animation, the ball doesn't hit the ground at the same spot everytime so therefore there will be times when it will fall a greater distance than others. I'm not exactly sure though how to correct that. The ball is accelerating so its distance is not linearly increasing

  14. #34
    When in doubt ask Eager Beaver's Avatar
    Join Date
    Feb 2007
    Location
    Planet Earth
    Posts
    911
    Quote Originally Posted by jcafaro10
    Thanks, it works but it’s sloppy. The ball is forced to stop so it doesn't look real. Also, I'm still not sure why its telling me the ground is at 475 when its not.
    This is my analysis:
    When we make a program combining physics and animation I think, we should remember that during animation the movement is controlled by iterations of time (t), which gets added or subtracted during each iteration. Since the computer uses binary method of calculation, this sometimes leads to errors due to lose of significant digits.
    _______________________________
    //See the following example:
    var a=1.23456E20;
    var b=1E5;
    var c;
    c=a-b;
    trace(c);
    _______________________________
    The trace statement shows 1.23456E20, which is obviously not correct!
    This has happened because the computer stores only about 15 significant digits!
    Anyhow this is what I think. Let other computer experts offer their comments.
    Don't hate physics. Understand how the binary system works.
    To overcome this problem to some extent, we have to fix the boundary conditions for the ball at the top, and bottom positions.

  15. #35
    Senior Member
    Join Date
    Sep 2006
    Posts
    130
    ah ok well how do we do this

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