I am making a game, where if you go through the right wall, you pop up on the left. I want to have a function that calculates the distance between two objects, where we can have a line that goes through the wall. For example, if the screen width is 100 and one object was at (0, 90) and another at (0, 10), the distance between them would be 20.
Basically, you need to check if the absolute distance on either plane is larger than half that screen dimension - if so, wrapping across the screen is faster than the standard distance on that axis:
dx = abs(x1 - x2); if (dx > width/2) dx = width - dx; dy = abs(y1 - y2); if (dy > height/2) dy = height - dy; dist = sqrt(dx*dx + dy*dy);