# Get distance between the edge of screen and a moving sprite

How to get the distance between the edge of the screen and a vector moving sprite? I want to get the distance between my moving sprite and the edge of the screen so that when my sprite touches the edge it will bounce of. I am using LIBGDX to develop a game. It uses java language.

-
We need more info. What API are you using and what programming language? – LiquidFeline May 16 '13 at 6:37
There i edited my post – hey May 16 '13 at 6:40
You are missing some relevant info here that would help giving you an acurate answer. Are you using `OrthographicCamera`? Is your world screen the same size of your viewport screen? Do you understand the difference between world coordinates and window coordinates? – petervaz May 16 '13 at 13:46

This can easily be done just with the bounds of the screen and the position of the sprite. The screen bounds can be stored in two variables, `screenMin` and `screenMax`, where `screenMin` contains the minimum X position of the screen and the minimum Y position of the screen, `screenMax` does likewise with the maximums.

``````leftDistance = spritePos.x - screenMin.x;
rightDistance = screenMax.x - spritePos.x;
topDistance = spritePos.y - screenMin.y;
bottomDistance = screenMax.y - spritePos.y;
``````

So you can find out if it's touching an edge like this (this is not copy/paste code, find the appropriate methods/members for your environment):

``````if(Math.min(leftDistance,rightDistance) < sprite.width/2)
//sprite is touching left or right side

if(Math.min(topDistance,bottomDistance) < sprite.height/2)
//sprite is touching top or bottom
``````
-