# Flickering, need help finding a way to get it out

• 02-29-2012, 08:49 PM
Atynine
Flickering, need help finding a way to get it out
I've been trying to stop my play( a ball/rectangle for now) from flickering whenever he's colliding with something. It happens because, whenever the player collides with something, I move the play to a location 1 pixel above the thing it's colliding with, and set the gravity to 0. Problem is, when the game sees that the player is no longer colliding with the platform/ground anymore, it re-initiates the gravity. I can't have the player sitting directly on the platform, because when I do, it's always colliding with it, which makes jumping impossible, because it'll turn off the gravity before he moves.

Things that have to do with gravity (y = 534 is 1 pixel higher than the collision box)
Code:

```                if(Gravity.isOnGround != true){                         Gravity.getYMomentum();                 }```
Code:

```                if(character.intersects(Ground)){                         y = 534;                         Gravity.isOnGround = true;                         Gravity.timer = 0;                         Gravity.yMomentum = 0;}else{ Gravity.isOnGround = false; }```
Gravity class
Code:

```public class Gravity {         public static boolean isOnGround = false;         static int timer = 0;         public static int yMomentum = 0;         public static void getYMomentum(){                 //After 13 ticks, yMomentum increases                 if(timer <= 13){                         timer++;                 }else{                         yMomentum++;                         timer = 0;                 }         } }```
• 02-29-2012, 09:48 PM
pbrockway2
Re: Flickering, need help finding a way to get it out
You might find it more straight forward if your game follows real physics rather than this 1px hover thing.

A collision should be detected not merely when two things are in contact, but rather when they *first* come into contact. You could remember the state of characters (standing, jumping etc) or rely on the fact that when a character collides with the ground there will be some component of relative motion perpendicular to the ground. (which won't exist after the collision)
• 02-29-2012, 10:00 PM
Atynine
Re: Flickering, need help finding a way to get it out
Quote:

Originally Posted by pbrockway2
You might find it more straight forward if your game follows real physics rather than this 1px hover thing.

A collision should be detected not merely when two things are in contact, but rather when they *first* come into contact. You could remember the state of characters (standing, jumping etc) or rely on the fact that when a character collides with the ground there will be some component of relative motion perpendicular to the ground. (which won't exist after the collision)

Thanks for making me realize that. It lead me onto the way I could fix it. First, I made the player move directly on top of the collision box(not 1 pixel higher), and when I want the player to jump, I simply move him up the 1 pixel needed to clear the collision box. Why didn't I think of that before?
• 02-29-2012, 10:38 PM
pbrockway2
Re: Flickering, need help finding a way to get it out
Great - I'm glad you've got it sorted out.