Ball wont stop bouncing (1 pixel bounces) when it should be at rest

hello!

So I have made a ball that bounces around inside of another ball…
its all good, but after gravity and bounce-absorbency have taken their toll and the ball is just rolling around, it will not stop bouncing (1 pixel bounces) which look quite disconcerting…

I was wondering if anyone could tell me what I am doing wrong?

thanks…
this is in AS3, but hopefully easy to understand…



stage.addEventListener(Event.ENTER_FRAME, playGame);

var gravity:Number = 0.20;
var friction:Number = 0.99;
var bounceRatio:Number = 0.70;
var dpv:Number = 0;
var dpn:Number = 0;
var projX1:Number = 0;
var projY1:Number = 0;
var projX2:Number = 0;
var projY2:Number = 0;

var b:Object = new Object();
b.px = game.ball.x;
b.py = game.ball.y;
b.r = game.ball.width/2;
b.len;
b.vx = 3;
b.vy = -3;
b.ay =0;

var w:Object = new Object();
w.px = 0;
w.py = 0;
w.vx = 0;
w.vy = 0;
w.r = game.wheel.width/2;
w.rot = 0;

var v:Object = new Object();
v.dx = 0;
v.dy = 0;
v.len = 0;
var counter:Number = 0;

var n:Object = new Object();
n.dx = 0;
n.dy = 0;
n.len = 0;
function playGame (e:Event){
	startMove();
	gravityAdjust();
	updateVectors();
	counter++;
	trace(b.vy);
	trace("    " + b.vx);
	//frictionize();
	if(w.r<b.len+b.r){
		extractBall();
		bounce();
	}
	moveBall();
	if(counter%830 == 0){
	b.py = -18;
	}
}

function updateVectors(){
	b.len = Math.sqrt(b.px*b.px +b.py*b.py);
	v.dx = -b.px/b.len;
	v.dy = -b.py/b.len;
}

function extractBall(){
	var pen = w.r - (b.len + b.r);
	b.px -= pen*v.dx;
	b.py -= pen*v.dy;
}

function bounce(){
	n.dx = v.dy;
	n.dy = -v.dx;
	n.len = Math.sqrt(n.dx*n.dx +n.dy*n.dy);
	if(n.len<=0){
		n.dx /= n.len;
		n.dy /= n.len;
	}
	dpv = b.vx*v.dx + b.vy*v.dy;
	dpn = b.vx*n.dx + b.vy*n.dy;
	dpv *= bounceRatio;
	dpn *= bounceRatio;
	projX1 = n.dx*dpn;
	projY1 = n.dy*dpn;
	projX2 = -v.dx*dpv;
	projY2 = -v.dy*dpv;
	b.vx = projX1 + projX2;
	b.vy = projY1 + projY2;
}

function frictionize(){
	b.vx *= friction;
	b.vy *= friction;
}

function gravityAdjust(){
	b.vy += gravity;
}

function startMove(){
	b.px +=b.vx;
	b.py +=b.vy;
}

function moveBall(){
	game.ball.x =b.px;
	game.ball.y =b.py;
}

you can ignore all that variable initiation up at the top :stuck_out_tongue:

thanks in advance!