Edge Detection on Screen
- by user2056745
I have a edge collision problem with a simple game that i am developing. Its about throwing a coin across the screen. I am using the code below to detect edge collisions so i can make the coin bounce from the edges of the screen.
Everything works as i want except one case. When the coin hits left edge and goes to right edge the system doesnt detect the collision. The rest cases are working perfectly, like hitting the right edge first and then the left edge.
Can someone suggest a solution for it?
public void onMove(float dx, float dy) {
coinX += dx;
coinY += dy;
if (coinX > rightBorder) {
coinX = ((rightBorder - coinX) / 3) + rightBorder;
}
if (coinX < leftBorder) {
coinX = -(coinX) / 3;
}
if (coinY > bottomBorder) {
coinY = ((bottomBorder - coinY) / 3) + bottomBorder;
}
invalidate();
}