The problem is when I move diagonal it moves fractions of units and therefore my coordinates might be a fraction of a unit say (40,400) might be actually (40.12,400.40) but another problem is my pink square might be moving really fast. Below is a screenshot where you can see I can make it past a pixel perfect passageway with my pink square but the other passageway with the white square is very difficult if not impossible to make it through (I have by zooming in and moving REALLY slow but sometimes it impossible for the reasons listed above) Suppose in the example above I am holding the up and left arrows how can I make it so the pink square slides across the yellow rectangle and detects it can move upward and begins to move up?
The code I am using right now looks like this
float d45 = (float) (1 / Math.sqrt(2));
moveX = -d45;
moveY = d45;
moveX = moveX * t * speed;
moveY = moveY * t * speed;
for (Stuff s : collidables) {
if (overlaps(s, moveX, 0)) {
collidedX.add(s);
x += moveX > 0 ? s.x - width - x : s.x + s.width - x;
}
}
if (collidedX.size < 1) {
x += moveX;
}
for (Stuff s : collidables) {
if (overlaps(s, 0, moveY)) {
collidedY.add(s);
y += moveY > 0 ? s.y - height - y : s.y + s.height - y;
}
}
if (collidedY.size < 1) {
y += moveY;
}
I check for collision twice once on the Y axis and another on the X axis so that it can still slide to the left even if you are pushing the up and left arrow. The problem is it skips over the 100 unit passage to the left of the picture above. If I create a passage that is 110 units wide it will slide up fine but this isn't really cool because I like how it looks where it slides perfectly. Any ideas?