Okay, with those clarifications it makes much more sense to me.

First of all, let me say that everything I put down here is completely untested. Following peabrain's link will probably help you more, but it seemed like a nice exercise, so I'm going to try to write up what I would do (but I haven't tested it). I'm going to completely ignore the practical realities of TiledMap and just go ahead and pretend that whatever functions I want are there.

First, some assumptions. Your circle has radius 1 and it interacts with tiles which are in a square grid with side length 1. This means a circle can, at any given instant, overlap with at most 9 tiles (the tile containing the center and the 8 surrounding it) -- a very managable number for simple iteration. If the circle is moving too fast then "terrible things" could happen between one check for overlap and the next -- for instance, it could go straight through a wall and out the other side in between one collision check and the next. If that's a danger you need fancier techniques; I'll assume that it moves slowly enough (or the collision tests are frequent enough) that the circle has time to overlap "just a little". The largest number of overlaps I can imagine at once would be four tiles, which would look like this:

**Code:**

XXXX ---

XXXX / \

XXXX / \

XXXX| |

----| . |

XXXX| |

XXXX \ /

XXXX \ /

XXXX ---

XXXX|XXXX

XXXX|XXXX

XXXX|XXXX

XXXX|XXXX

(The circle was coming down and to the left when it bumped into two walls at once, touching both near a boundary between tiles. It is touching all 8 tiles.) Simpler versions can be made by hitting only one wall and/or hitting in the middle of a tile not on the "crack" between tiles.

Another situation to watch out for is hitting a corner:

**Code:**

---

/ \

/ \

| |

| . |

| |

\ /

XXXX\ /

XXXX ---

XXXX

XXXX

(And it can also hit corners of two tiles at once, etc.)

Okay, so I've visualized the problem. Now let me start naming things.

**Code:**

Vx, Vy := velocity of the circle (in x and y components, as floats)

Cx, Cy := center of circle (x and y components, as floats)

Ci, Cj := the integer coordinates of the tile containin the center of the circle

IF the tiles started counting at (0,0) AND they size was 1 in the units that Cx and Cy use, then Ci = (int)Cx and Cj = (int)Cy, but both assumptions are unlikely, so there's some scaling and coordinate offset needed there.

**Code:**

Tile[i,j] := the tile at position (i,j). For instance Tile[Ci,Cj] contains the center of the circle.

Tile[i,j].solid() := a boolean telling whether the tile has "tileID of 0" (ie, the circle bounces off).

So just checking for overlap would look like this:

**Code:**

boolean bouncesOffSomething() {

for (int i=Ci-1; i<=Ci+1; i++) {

for (int j=Cj-1; j<Cj+1; j++) {

if (Tile[i,j].solid()) {

return true;

}

}

}

return false;

}

Okay, I think I've got the notation down. Let's see if I can solve the problem. I'll consider the x direction first. An overlap with a tile in the column where i=Ci-1 means that we must have come from the right (Vx was negative) and should now bounce away to the right (Vx now positive). A similar argument (but reversed) applies to overlaps with tiles in the column where i=Ci+1. And the reasoning in the y direction works the same way. If the circle overlaps the cell in the middle (at Ci,Cj) then we're in trouble: it must not have overlapped the last time we detected collisions (because then it would have bounced) and so in between it must have traveled at least distance 1 -- much too far for our assumptions to hold.

Notice that I haven't had to check for particular sides of individual tiles -- that's a fortunate side-affect of moving slowly enough (sampling often enough) that the circle moves substantially less than a tile's width in each step.

So I come up with some logic like this:

**Code:**

void checkForCollisionsAndChangeCourse(BouncingCircle circle) {

if (Tile[Ci,Cj].solid()) throw new MovingTooFastException();

boolean bounceRight = false; // does it bounce and head off to the right?

boolean bounceLeft = false; // does it bounce and head off to the left?

for (int j=Cj-1; j<Cj+1; j++) {

if (Tile[Ci-1,j].solid()) {

bounceRight = true;

}

if (Tile[Ci+1,j].solid()) {

bounceLeft = true;

}

}

boolean bounceUp = false; // does it bounce and head off to the right?

boolean bounceDown = false; // does it bounce and head off to the left?

for (int i=Cj-1; i<Ci+1; i++) {

if (Tile[i,Cj-1].solid()) {

bounceUp = true;

}

if (Tile[i,Cj+1].solid()) {

bounceDown = true;

}

}

if (bounceRight && bounceLeft) throw new ImpossiblePositionException();

if (bounceUp && bounceDown) throw new ImpossiblePositionException();

if (bounceRight || bounceLeft) circle.setVx(circle.getVx()*-1);

if (bounceUp || bounceDown) circle.setVy(circle.getVy()*-1);

}

Anyway, that's what I came up with. It was a fun exercise... if you do try to use it, let me know how it went.