Circle/Block collision problems

Hello everyone,

so, I’m trying to get this right. A ball with set direction moves until it hits a target block. Upon collision, new direction is calculated, and the ball moves in that direction until it collides again. This all takes place in a 2d environment, and no factors likes friction or gravity come into play either. The blocks are not rotated either.

Somehow, I can’t quite get this right. Every frame, I loop through the blocks, and check if the ball collides with any of them (through simple hitTestObject) and then see if it was hit on a flat surface or a point. In case of a flat surface, the new direction is simple enough. In case of a point, I had an idea, but it does not seem to be working.

Here is my gameLoop function (ENTER_FRAME function, don’t think it matters much? )

private function gameLoop(e:Event):void
{
	var dirRad:Number = ball.direction * Math.PI / 180;
	var yDiff:Number = 0 - Math.sin(dirRad) * ball.speed;
	var xDiff:Number = Math.cos(dirRad) * ball.speed;
	ball.y += yDiff;
	ball.x += xDiff;

	for (var i:uint = 0; i < blocks.length; i++)
	{
		var b:Block = blocks* as Block;
		if (b.hitTestObject(ball))
		{
			if (ball.x > b.x && ball.x < b.x + b.width)
			{
				if (b.y > ball.y) ball.y = b.y - ball.radius;
				else ball.y = b.y + b.height + ball.radius;
				ball.direction = calcDir(0 - ball.direction);
			}

			else if (ball.y > b.y && ball.y < b.y + b.height)
			{
				if (b.x > ball.x) ball.x = b.x - ball.radius;
				else ball.x = b.x + b.width + ball.radius;
				ball.direction = calcDir(90 + (90 - ball.direction));
			}

			else
			{
				var px:uint;
				var py:uint;
				if (ball.x < b.x) px = b.x;
				else px = b.x + b.width;
				if (ball.y < b.y) py = b.y;
				else py = b.y + b.height;
				var dist:Number = Math.pow(ball.x - px, 2) + Math.pow(ball.y - py, 2);
				if (dist < Math.pow(ball.radius, 2))
				{
					var angle:Number = Math.asin((px - ball.x) / 10);
					var degAngle:Number = 90 + (angle * 180 / Math.PI);

					ball.direction = calcDir(degAngle + (degAngle - (ball.direction - 180)));
				}
			}
		}
	}
}

calcDir is a simple function that makes sure the rotation is between 0 and 360:

private function calcDir(input:int):uint
{
	while (input > 360) input -= 360;
	while (input < 0) input += 360;
	return uint(input);
}

In some cases, the ball bounces back fine. In some cases, the ball bounces in a direction that seems totally unlikely. In other cases, the ball seems to get ‘stuck’ in a corner and eventually bounces back in a seemingly random way. What am I doing wrong here?