# Better Way to Detect Line Cross

I am developing a very simple game in libGDX for android devices. Simply, the game is a character who runs and jumps over obstacles, every time the character jumps over one of said obstacles, a counter increments and the score is displayed.

I am currently detecting the character jumping over the obstacles by creating two Rectangle objects (invisible) - one small one for the center of the character and the second is a long thin line from the center of the obstacle. I use the 'overlaps()' command to check whether or not the character has passed said line.

This issue I am having is that because I am calling to check on the overlap in my update method, it is called many times in a second and hence sometimes adds two points to the score as the small rectangles are still overlapping on the second round of checking.

I have got the sizes down to a near perfect size with the occurrence of incorrect scores at a minimum, however they do still happen.

So my question is what would a better way be to detect a line crossing / collision?

To me, your basic idea of checking overlapping rectangle is workable; you only miss a small part:

For each of your obstacles, have a two-state flag:

• untouched
• overlapping

When you create the obstacle, have the flag set to 'untouched'. When the player jumps over it, detect if there is an overlap between the two rectangles. If there is, first, check if the state of the flag is 'untouched' if it is, set the flag to 'overlapping' and increment the score counter. That's all :)

Pseudocode:

if ( this.jumpFlag == 'untouched' && character.jumpRect.overlaps(this.jumpOverRect) )
{
game.scoreCount += 1
this.jumpFlag = overlapping
}

• Thank you for this answer, it proved to be the most easy to integrate into the code I already have and it works a dream now. Sep 22, 2016 at 8:39
• @MrPhooky Glad I could help! Sep 22, 2016 at 11:30

I would suggest that you keep track of the position of the obstacle until you have passed it and added a point. I would also suggest against trying to make a perfect size object for a collision, this is because frame rate can vary a lot so for you it might be perfect but for somebody with higher frame rate it can still cause unwanted behaviour.

If you still want to do it with collision however I would suggest keeping track of the upcoming obstacle and checking whenever you have received a point from it already. This way you won't get multiple point for one obstacle because of multiple collision detections.

Hope my answer helped you a bit.

• +1 for position tracking. In a game where you're moving along one axis it's pretty simple to just keep track of the current scoring position (next obstacle) and move it forward to the obstacle after that every time you pass one. If there's concern about passing multiple it's also simple to give points while current obstacle is behind you (eg: while(obstacle.x - player.x < 0)) and keep moving to the next one until you're ahead of the player again. Sep 21, 2016 at 20:24

Your "long thin line" object is basically a funny shaped coin. Make the "long thin line" object wider (to avoid missing it), and simply delete it after it's collected rather than needing it to be as small as possible.

• Thanks for the answer - the issue with this is the line is a part of the obstacle Sprite so it is difficult for me to remove it and then bring it back when the obstacle is moved again. Sep 22, 2016 at 8:40