A Flash Developer Resource Site

Page 1 of 2 12 LastLast
Results 1 to 20 of 26

Thread: Incredibly Easy Physics and Gravity!!!!

  1. #1
    j00 r teh pwnz0r3d DarkMagicHacker's Avatar
    Join Date
    Feb 2001
    Location
    Your mom's room.
    Posts
    622

    Smile

    For all of you with questions pertaining to Physics and Gravity, here is a bug free solution to your problem!
    Here are the Steps:

    -> Open a new movie and set its dimensions to 550x400
    -> Make a ball movie clip.
    -> Go into actions
    -> Type this in
    Code:
    onClipEvent(enterFrame)
    {
     // this keeps track of the ball speed.
     _x = xspeed + _x;
     _y = yspeed + _y;
     // this slows down the ball
     xspeed = xspeed*0.9;
     yspeed = yspeed*0.9;
     // makes dat ball bounce
     if (_y>400)
     {
      yspeed = -yspeed;
     }
     if (_x>550)
     {
      xspeed = -xspeed;
     }
     else if (_x<0)
     {
      xspeed = -xspeed;
     }
    }
    onClipEvent(load)
    {
     xspeed = 5;
     yspeed = -7;
    }
    The solution to all physics problems!

  2. #2
    Gross Pecululatarian Ed Mack's Avatar
    Join Date
    Dec 2001
    Location
    UK
    Posts
    2,954
    There is more to physics than bouncing off walls... unless I'm not getting somthing here.

  3. #3
    Senior Member
    Join Date
    May 2001
    Posts
    1,838
    bug free solution
    I did as you suggest, and make a ball _y=5; Copy your codes into the ball. Then test movie.

    How come the ball goes off the screen straightly without bounce ?! I think there must be a bug in your code.

  4. #4
    Senior Member
    Join Date
    Jan 2002
    Posts
    368
    aaaah! all my problems are solved!

  5. #5
    Gross Pecululatarian Ed Mack's Avatar
    Join Date
    Dec 2001
    Location
    UK
    Posts
    2,954
    Darn, it seems like your site's out of business now, eh bit?

  6. #6
    Senior Member
    Join Date
    Jan 2002
    Posts
    368
    are you kidding? with this new information, i'll be able to do crazy stuff!

  7. #7
    Gross Pecululatarian Ed Mack's Avatar
    Join Date
    Dec 2001
    Location
    UK
    Posts
    2,954
    oh you make me laugh

    More seriously, DarkMagicHacker, this sort of code is unsuitable for Flashkit, due to our rating. This really could blow some of the younger 13-15yr olds.

  8. #8
    Back from Hiatus mouse111's Avatar
    Join Date
    Oct 2001
    Posts
    599
    blow what?

    how is it unsuitable

    it may not be that great cause the bounces are not true bounces they just hit something and reverse direction at a constant speed, but how is it unsuitable

  9. #9
    Junior Member
    Join Date
    Jul 2002
    Posts
    25
    Originally posted by mouse111
    blow what?

    how is it unsuitable

    it may not be that great cause the bounces are not true bounces they just hit something and reverse direction at a constant speed, but how is it unsuitable
    Swwwoooooossshhhh!


  10. #10
    Gross Pecululatarian Ed Mack's Avatar
    Join Date
    Dec 2001
    Location
    UK
    Posts
    2,954
    Originally posted by mouse111
    blow what?

    how is it unsuitable

    it may not be that great cause the bounces are not true bounces they just hit something and reverse direction at a constant speed, but how is it unsuitable
    You see, it's unfortunitly too late for some of us. Please, any parents out there, ban this thread on your computer.

  11. #11
    Banned-ed-er-ing WMLeeBo's Avatar
    Join Date
    Mar 2001
    Posts
    1,482
    Whaaa...*Tries to piece together with 10th grade Geometry knowledge. 9Th grade Algerbra Knowledge.*

    Brain Crashes....
    Hello.

  12. #12
    Back from Hiatus mouse111's Avatar
    Join Date
    Oct 2001
    Posts
    599
    Originally posted by muckyMuckMan
    Swwwoooooossshhhh!

    ugh i understand now

    its a pretty simplistic way of making a cheasy effect

    o well

  13. #13
    heres' an example, with air resistance and draggable throwable ball

    http://www.geocities.com/nohuntersweb/heheagain.html

    just the unnatural stop and the tendency to keep on bouncing, which is caused by the imperfect gravity script

    this was in the arena's actionscript battle

  14. #14
    Jordan Berry
    Join Date
    Oct 2002
    Posts
    307
    Originally posted by Ed Mack
    oh you make me laugh

    More seriously, DarkMagicHacker, this sort of code is unsuitable for Flashkit, due to our rating. This really could blow some of the younger 13-15yr olds.
    omg, im in 8th grader, heres the code:

    onClipEvent (enterFrame) {
    if (_x>300-_width/2) {
    _x = 300-_width/2-1;
    xv = xv*-0.9;
    }
    if (_x<0+_width/2) {
    _x = 0+_width/2+1;
    xv = xv*-0.9;
    }
    if (_y<0+_width/2) {
    _y = 0+_width/2+1;
    yv = yv*-0.9;
    }
    if (_y>300-_width/2) {
    _y = 300-_width/2-.1;
    yv = yv*-0.9;
    }
    if (!drag) {
    xv = xv*.99;
    yv = yv*.99 +.5;
    _x = _x += xv;
    _y = _y += yv;
    } else {
    xv = _x-x;
    yv = _y-y;
    x = _x;
    y = _y;
    }
    }

    Thats the best i can do (it should work)..

    Ed Mack, come on man stop picking on the little kids (me)
    Last edited by xDrunkCowx; 10-24-2002 at 01:43 AM.

  15. #15
    Jordan Berry
    Join Date
    Oct 2002
    Posts
    307
    Originally posted by mouse111
    blow what?

    how is it unsuitable

    it may not be that great cause the bounces are not true bounces they just hit something and reverse direction at a constant speed, but how is it unsuitable
    When something bounces off of a wall does it come back off the ball at the same speed as it hit the wall? no you instead of * the velocity by -1 multiply it by -0.9 that way it slows the ball down a little... making a more relistic script..

  16. #16
    Gross Pecululatarian Ed Mack's Avatar
    Join Date
    Dec 2001
    Location
    UK
    Posts
    2,954
    Originally posted by xDrunkCowx

    Ed Mack, come on man stop picking on the little kids (me)
    I think you completly missed the jokes.... they were with Bit101 mainly.

    Anyway, I'm not quite sure of how the school system works in other countries, but I'm probably about the same age as you.

  17. #17
    Jordan Berry
    Join Date
    Oct 2002
    Posts
    307
    well im 14 and in 8th grader... i dont get what u ment by they are with bit101??
    I'm back.

  18. #18
    Jordan Berry
    Join Date
    Oct 2002
    Posts
    307
    Originally posted by mouse111
    ugh i understand now

    its a pretty simplistic way of making a cheasy effect

    o well
    cool i guess.. but its wierd how when the ball starts moving at a slow speed it just stops... i take it ur using a if statment to do change the _x velocity?
    I'm back.

  19. #19
    Mental Deficit Nionicle's Avatar
    Join Date
    Mar 2002
    Location
    Utah, Hyrum
    Posts
    1,348
    hey mr 8th grader.
    I an 11th grader need help under standing your code.
    Can you post the same code for me with an explanation of the different parts.


    thanx bud
    I can only postulate the probability of performing at a paramount level of perfection praised by the pulchritudinous paragon whose only practice is to preserve such a paradigm.

  20. #20
    Jordan Berry
    Join Date
    Oct 2002
    Posts
    307
    onClipEvent (enterFrame) {
    // checking if the wall if hitting the right wall.. To find this out you take the _x - _width/2 and if that is greater than or equal to the _x of the wall then the ball is touching. _width/2 is the radius,
    if (_x>300-_width/2) {
    // if the ball is touching the wall then you move it back so its not touching the ball. Then the ball would just keep on hitting that one wall then mving back and doing this for ever. So now we need to change the xv, the angel a object hits a wall is the same angel the object leaves the wall but reversd. For example if the ball xv is 5 when it hits the wall then it needs to be changed to -5. to do that we would just take the xv and multiply it by -1 so if its a positive number then it turns into a negitive number and if its a negitive number then it turns into a positive number. But when a object hits a wall (remember we are doing elastic collision so the ball bounces) it looses speed so we multiply it by -0.9 so if the xv is 2 then now its -1.8.
    _x = 300-_width/2-1;
    xv = xv*-0.9;
    }
    if (_x<0+_width/2) {
    _x = 0+_width/2+1;
    xv = xv*-0.9;
    }
    if (_y<0+_width/2) {
    _y = 0+_width/2+1;
    yv = yv*-0.9;
    }
    if (_y>300-_width/2) {
    _y = 300-_width/2-.1;
    yv = yv*-0.9;
    }
    // now that we have checked to see if the ball is hitting any of the walls, and if it is we change the velocitie. Now what about gravity and friction? Well to add friction just multiply the xv and yv by .99 couse .99 makes the most relistic friction. Then to add gravity just add .5 to the yv so that way no matter what the bal is going to want to move downword.
    // if the ball is not being draged then the script calculates friction and gravity and then moves the ball(as seen above).
    if (!drag) {
    xv = xv*.99;
    yv = yv*.99 +.5;
    _x = _x += xv;
    _y = _y += yv;
    } else {
    // if the user is dragging the ball then we need to find the throwing power, so you take the oldx and y and subtract it from the new x and y and the distance between is the new xv and yv.
    xv = _x-x;
    yv = _y-y;
    x = _x;
    y = _y;
    }
    }
    Now just open the circle mc and make the circle a button and put:
    on (press) {
    startDrag (this);
    drag = true;
    }
    on (release, releaseOutside) {
    stopDrag ();
    drag = false;
    }

    This is from a tut i wrote earlyer on my site http://xdrunkcowx.tripod.com/ other tuts thier to..
    i know thier are errors on the tut BUT im doing this with my time for free so as long as u can understand it... dont tell me about spelling or grammer errors
    I'm back.

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