PDA

View Full Version : [AS3] pool game ball physics bug


clouting
02-17-2009, 11:06 AM
hi ppl.....

have just started making a pool game but have hit a bit of a wall.
I have created some rough designs and just wanted to get the game working so i thought i'd try getting the ball physics to work.

I have used the code from 'ActionScript 3.0 Animation - Making things move' by Friends of Ed because i have found all of their other techniques to be extremely effective, however, with this piece of code there seems to be a bug.

every now and again (particularly on the break off shot) it seems as if some of the balls get stuck together, causing them to behave wrongly.

you can see an example of it here -

http://www.ghosttownclub.co.uk/games/pool_game.swf

just hold down on the big white ball to generate power - release to fire.

Does anyone know how to get the code working? perhaps i have done something wrong.

if not, has anyone else made a pool game? and where did u find the code?

thanks in advance

clouting

nakedkafka
02-17-2009, 12:48 PM
eh, where's the code ?

clouting
02-17-2009, 01:17 PM
eh, where's the code ?

oh yer,

here it is ---

private function getBalls() {
for(var i:uint; i<theRoot.ballArray.length - 1; i++) {
var ballA = theRoot.ballArray[i];

for(var j:uint = i + 1; j<theRoot.ballArray.length; j++) {
var ballB = theRoot.ballArray[j];
checkCollision(ballA, ballB);
}
}
}

private function checkCollision(ball0, ball1):void {
var dx:Number = ball1.x - ball0.x;
var dy:Number = ball1.y - ball0.y;
var dist:Number = Math.sqrt(dx*dx + dy*dy);

if(dist < ball0.radius + ball1.radius) {
// calculate angle, sine and cosine
var angle:Number = Math.atan2(dy, dx);
var sin:Number = Math.sin(angle);
var cos:Number = Math.cos(angle);

// rotate ball0's position
var pos0:Point = new Point(0, 0);

// rotate ball1's position
var pos1:Point = rotate(dx, dy, sin, cos, true);

// rotate ball0's velocity
var vel0:Point = rotate(ball0.vx,
ball0.vy,
sin,
cos,
true);

// rotate ball1's velocity
var vel1:Point = rotate(ball1.vx,
ball1.vy,
sin,
cos,
true);

// collision reaction
var vxTotal:Number = vel0.x - vel1.x;
vel0.x = ((ball0.mass - ball1.mass) * vel0.x +
2 * ball1.mass * vel1.x) /
(ball0.mass + ball1.mass);
vel1.x = vxTotal + vel0.x;

// update position
var absV:Number = Math.abs(vel0.x) + Math.abs(vel1.x);
var overlap:Number = (ball0.radius + ball1.radius)
- Math.abs(pos0.x - pos1.x);
pos0.x += vel0.x / absV * overlap;
pos1.x += vel1.x / absV * overlap;

// rotate positions back
var pos0F:Object = rotate(pos0.x,
pos0.y,
sin,
cos,
false);

var pos1F:Object = rotate(pos1.x,
pos1.y,
sin,
cos,
false);

// adjust positions to actual screen positions
ball1.x = ball0.x + pos1F.x;
ball1.y = ball0.y + pos1F.y;
ball0.x = ball0.x + pos0F.x;
ball0.y = ball0.y + pos0F.y;

// rotate velocities back
var vel0F:Object = rotate(vel0.x,
vel0.y,
sin,
cos,
false);
var vel1F:Object = rotate(vel1.x,
vel1.y,
sin,
cos,
false);
ball0.vx = vel0F.x;
ball0.vy = vel0F.y;
ball1.vx = vel1F.x;
ball1.vy = vel1F.y;

//theRoot.spin = 0.02
}
}

clouting
02-17-2009, 02:43 PM
here is the entire source code.