 A Flash Developer Resource Site

# Thread: Problem with bounce physics

1. ## 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```  Reply With Quote

2. redCircle.vY *= -1;

should be something like redCircle.vY *= -0.6 that wil reduce the bounce  Reply With Quote

3. 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;  Reply With Quote

4. do a check for if the vy < say 0.2 vY = 0  Reply With Quote

5. 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  Reply With Quote

6. 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:

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!  Reply With Quote

7. Originally Posted by tonypa 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;`  Reply With Quote

8. @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?  Reply With Quote

9. ha! last time i did that everybody complained it was pointless to show so i asked for feedback and... you know where that went!  Reply With Quote

#### Posting Permissions

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

 » Home » Movies » Tutorials » Submissions » Board » Links » Reviews » Feedback » Gallery » Fonts » The Lounge » Sound Loops » Sound FX » About FK » Sitemap 