Separate your presentation code from the game logic and you will simplify things immensely. You'll have code that's easier to understand, easier to change later, and is less prone to errors.
For example, something like:
PHP Code:
if (RIGHT_WALL_X - ball.pos.x < ball.radius) {
// we have a collision on the right wall!!
}
is much easier to manage and understand than your code (which I'm only assuming is doing something similar to what's above):
PHP Code:
if(findImg(1004).polygon[temp.iy] - cos(degtorad(temp.angle)) * BALL_SPEED < findImg(1000).polygon[1] ||
findImg(1004).polygon[temp.iy] - cos(degtorad(temp.angle)) * BALL_SPEED > findImg(1000).polygon[7]){
temp.angle = 180 - temp.angle;
}
In your game loop, you would have something like:
PHP Code:
function gameLoop() {
updateGame(); // updates the model (check for collisions, move ball, etc)
renderGame(); // draws the model in its current state
// .. loop endlessly
}
Use simple units for the model (for example, a ball with a width of 1 unit instead of 16 pixels) and then when you are drawing the game, just convert the units to pixels (or tiles, or whatever). This would also allow you to change the way you render the game without having to mess with the game logic at all.
Also, I would use vectors for something like this, too.