A Flash Developer Resource Site

Results 1 to 9 of 9

Thread: Problem with bounce physics

  1. #1
    The Cheeze to Your Macaroni ColbyCheeze's Avatar
    Join Date
    Dec 2007
    Location
    Texas
    Posts
    244

    Problem with bounce physics

    I am trying to get some decent bounce physics for a game that I am working on however the ball seems to never stop bouncing:
    Ball Bounce Test

    These are defined in another file
    public static const FRICTION:Number = .5;
    public static const WORLD_GRAVITY:Number = .3;
    public static const WORLD_WIND:Number = .05;

    Any ideas on how to fix this? Here is the code:

    Code:
    private function move():void{
    			
    			//APLLY ALL FORCES TO THE GAME OBJECTS
    			//VELOCITY VARIABLE
    			
    			//Apply gravity
    			redCircle.vY += Game.WORLD_GRAVITY;
    			redCircle.vX += Game.WORLD_WIND;
    			
    			//redCircle.springToPoint(stage.mouseX,stage.mouseY);
    			
    			
    			//ADD NEW VELOCITY VECTOR TO COORDS
    			
    			//Circle
    			redCircle.newX += redCircle.vX;
    			redCircle.newY += redCircle.vY;
    			
    			//DO ANY TESTS THAT NEED TO BE DONE
    			
    			//If circle hits ground reverse Y vel
    			//and apply friction
    			if(redCircle.newY >= Game.WORLD_GROUND){
    				redCircle.newY = 400;
    				redCircle.vY *= -1;
    				redCircle.vY *= redCircle.fr;
    				redCircle.vX *= redCircle.fr;
    			}
    			
    			//Wrap circle around to other side
    			//when it hits the edge
    			var halfWidth:int = redCircle.width/2;	
    			if(redCircle.newX > Game.STAGE_WIDTH + halfWidth){
    				redCircle.newX = 0 - halfWidth;
    			}
    			if(redCircle.newX < 0 -  halfWidth){
    				redCircle.newX = Game.STAGE_WIDTH + halfWidth;
    			}
    			
    		}//end move

  2. #2
    FK founder & general loiterer Flashkit's Avatar
    Join Date
    Feb 2000
    Location
    Sydney
    Posts
    1,149
    redCircle.vY *= -1;

    should be something like redCircle.vY *= -0.6 that wil reduce the bounce
    Regards Mark Fennell - Flash Kit Founder, general loiterer
    -------------------------------
    I Hate Zombies - iPhone Game | markfennell.com

  3. #3
    The Cheeze to Your Macaroni ColbyCheeze's Avatar
    Join Date
    Dec 2007
    Location
    Texas
    Posts
    244
    Quote Originally Posted by Flashkit
    redCircle.vY *= -1;

    should be something like redCircle.vY *= -0.6 that wil reduce the bounce
    I did do that. It is afterwards.
    redCircle.vY *= -1;
    redCircle.vY *= redCircle.fr; //redCircle.fr == FRICTION:Number = .5;

  4. #4
    FK founder & general loiterer Flashkit's Avatar
    Join Date
    Feb 2000
    Location
    Sydney
    Posts
    1,149
    do a check for if the vy < say 0.2 vY = 0
    Regards Mark Fennell - Flash Kit Founder, general loiterer
    -------------------------------
    I Hate Zombies - iPhone Game | markfennell.com

  5. #5
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,223
    You need to detect when the ball has become resting on the ground and stop adding gravity in such case.

    *set up flag ballIsResting=false
    *ball hits the floor
    *reduce vY
    *if vY < less then some number set vY=0 and set ballIsResting = true
    *add gravity only if ballIsResting is false

  6. #6
    the cheesy child bounceboy's Avatar
    Join Date
    Dec 2008
    Location
    Australia
    Posts
    323
    funny!!! I had the opposite problem. i wanted something (it wasn't a ball but it still worked) to continuously bounce. I have solved my problem but if you wnat i can give you my lower bounce script:

    onClipEvent(load){
    bounce = 10;
    fullbounce = 10;
    way = "down";
    wtwo = _width/2;
    htwo = _height/2;
    speed = 10;
    lr = 10;
    take = 1.1;
    wide = 550;
    wide0 = 0;
    moview = wtwo;
    }
    onClipEvent(enterFrame){
    if(way=="down"){
    _y+=bounce;
    bounce+=1;
    }else if(way=="up"){
    _y-=bounce;
    bounce-=take;
    }
    if(bounce<=0){
    way = "down";
    }
    if(_root.ground.hitTest(_x,_y+htwo,true)){
    way = "up";
    }
    }

    make the walls or objects you are bouncing off into a movieclip called "ground".

    what you need to do to mine to make the bounce lower every time is make the variable take higher. at the moment it is 1.1 but if you make it higher you'll lose your bounce and if you make it lower you'll gain bounce (which if that happens, something is wrong with the physics!)

    please tell me if this worked!

  7. #7
    Pumpkin Carving 2008 ImprisonedPride's Avatar
    Join Date
    Apr 2006
    Location
    Grand Rapids MI
    Posts
    2,378
    Quote Originally Posted by tonypa View Post
    You need to detect when the ball has become resting on the ground and stop adding gravity in such case.

    *set up flag ballIsResting=false
    *ball hits the floor
    *reduce vY
    *if vY < less then some number set vY=0 and set ballIsResting = true
    *add gravity only if ballIsResting is false

    To do it quickly, false and true equate to 0/1, so just do:

    Code:
    vY *= gravity*ballIsResting;
    The 'Boose':
    ASUS Sabertooth P67 TUF
    Intel Core i7-2600K Quad-Core Sandy Bridge 3.4GHz Overclocked to 4.2GHz
    8GB G.Skill Ripjaws 1600 DDR3
    ASUS ENGTX550 TI DC/DI/1GD5 GeForce GTX 550 Ti (Fermi) 1GB 1GDDR5 (Overclocked to 1.1GHz)
    New addition: OCZ Vertex 240GB SATA III SSD
    WEI Score: 7.6

  8. #8
    Student
    Join Date
    Apr 2001
    Location
    -
    Posts
    4,756
    @bounceboy: its not the first time you are bumping a dated thread- why dont you simply create new threads for the things you want to tell or share?

  9. #9
    the cheesy child bounceboy's Avatar
    Join Date
    Dec 2008
    Location
    Australia
    Posts
    323
    ha! last time i did that everybody complained it was pointless to show so i asked for feedback and... you know where that went!

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