Like the topic says, the following code is based on top down grid style collision detection and movement… So I have this ball which is moving around tiles… let me just show you all the code first then I will mention the problem:-

if (isReady && isMovin) { //trace("speed is " + speed); speedX = Math.cos(angle) * speed * flagX; speedY = Math.sin(angle) * speed * flagY; x_pos = this.x + speedX; y_pos = this.y + speedY; left = Math.floor((x_pos - 9) / tile_size); bottom = Math.floor((y_pos + 9) / tile_size); top = Math.floor((y_pos - 9) / tile_size); right = Math.floor((x_pos + 9) / tile_size); top_left = level[top][left]; bottom_left = level[bottom][left]; top_right = level[top][right]; bottom_right = level[bottom][right]; //no wall encountered carry on if (top_left != 1 && bottom_left != 1 && top_right != 1 && bottom_right != 1) { this.x += speedX; this.y += speedY; } else //code for checking collision with walls { if (top_left == 1 && bottom_left == 1 && speedX < 0) { this.x = (left + 1) * tile_size + 10; //this.y += speedY; speed -= 0.2; flagX = -flagX; } else if (top_right == 1 && bottom_right == 1 && speedX > 0) { this.x = (right - 1) * tile_size + 8; //this.y += speedY; speed -= 0.2; flagX = -flagX; } else { this.x += speedX; } if (top_right == 1 && top_left == 1 && speedY < 0) { this.y = (top + 1) * tile_size + 9; //this.x += speedX; speed -= 0.2; flagY = -flagY; } else if (bottom_left == 1 && bottom_right == 1 && speedY > 0) { this.y = (bottom - 1) * tile_size + 10; //this.x += speedX; speed -= 0.2; flagY = -flagY; } else { this.y += speedY; } } speed -= fric; if (speed <= 0) isMovin = false; }

As you all can see I am only testing the top(similar for bottom, right and left) collision only if both the variables are blocked like top_right and top_left both are equal to one then only I will change the direction.

The problem comes when only one of the variable is one (that is maybe top_right is 1 and top_left is not) then the ball goes straight or movement becomes jittery cause I havent added the code for checking with single variable. My issue is that for any given variable for eg if top_right is one then the ball can be colliding either on bottom of that tile or on the left hand side of that tile, I am not able to diagnose that properly…

Here is pic of that issue:-

So the ball can be moving in direction 1 and it hits left hand side of tile and direction 2 hits bottom of the tile… how to diagnose which direction it hits.

Any help will be appreciated.