Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

share|improve this question
1  
We need more info. What API are you using and what programming language? –  CPP_Person 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
add comment

1 Answer

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
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.