0

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 = -(1 * coinX) / 3;
    }
    if (coinY > bottomBorder) {
        coinY = ((bottomBorder - coinY) / 3) + bottomBorder;    
    }

    invalidate();
}
koraxis
  • 801
  • 2
  • 12
  • 22
  • My first impressions are it could be something to do with the statement `-(1 *coinX)`. I could be wrong though. What are your coinX and coinY variables cast to? – Dave Moore Jul 02 '13 at 14:57
  • @DaveMoore They are int, but like i said, i dont have problem with that part, thats the left border, my left border edge detection is working without any problem. My problem is with the right one when it bounces from the left. When i fling it towards the right border there is no problem, but when i fling it to left and when it bounces from left to right, then it just gets out of the scree from the right side. – koraxis Jul 02 '13 at 21:20
  • Okay, then I'd say there's could be an issue with your `invalidate()` method that's messing up the edge detection. Try putting a logcat statement into the right border edge detection `if` statement to see if it gets triggered, or, set up a textview or something which will display the value of coinX. – Dave Moore Jul 03 '13 at 11:41
  • @DaveMoore I put logcat, and it doesnt get into the rightborder edge detection when it bounces back from the left side. But if i remove the invalidate() then the coin doesnt move like it should. Its like frame by frame or jump to the last point. Where should i put the invalidate? – koraxis Jul 03 '13 at 12:16
  • I found sth. If i change the order of the 'if' statements ,like for example if i put left border check before the right border check, then this time the opposite situation happens. What can be the reason for this? – koraxis Jul 03 '13 at 12:35
  • Haha great. I presume dx and dy are your x and y vector for movement? – Dave Moore Jul 03 '13 at 13:15
  • Yeah, and i found a solution... It doesnt make any sense to me but it works now, i added another `if` after all the ifs like this : `if (coinX > rightBorder || coinX – koraxis Jul 03 '13 at 13:17
  • Okay, well done for finding a solution. It's just hit me know what's wrong with this. When you fling the coin, the method is called using the fling's vector as parameters. As the method is only run once, the check for each `if` condition is only made once. If the coin touches an edge, and is reflected back with enough velocity to reach the other side, the method isn't called again to check for an edge detection. You'll need to find a way to check that the reflected vector doesn't reach the other side. – Dave Moore Jul 03 '13 at 13:23
  • What happens if you fling it fast enough to bounce 3 times? – Dave Moore Jul 03 '13 at 14:26

0 Answers0