Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (753)
Games in Android Showcase (228)
games submitted by our members
Games in WIP (842)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Platformer Gravity, the Camera and Graphical Flicker...  (Read 1426 times)
0 Members and 1 Guest are viewing this topic.
Offline SeanSeanston

Senior Newbie





« Posted 2012-04-20 14:48:58 »

I've encountered a small but annoying graphical glitch in my platformer's engine.

I'm using Slick.

What happens is:
While the player is jumping and falling back down, moving the camera with it, the game's map tiles sometimes display a slight flicker about 1 pixel high on their bottom side.
Currently, the player is a green rectangle which happens to be the same colour as this flicker, and experimenting with changing the colour of the player has confirmed that it is definitely a small part of the player sprite "bleeding" through. I've made a simple diagram to help picture it better. It does happen on the white squares too, it's just not as noticeable as the brown ones. Also, for some reason it seems as though this problem isn't affecting some brown tiles, seemingly corner tiles but that's probably not important:


What I think is happening:
Through a bit of experimentation and thinking the problem through, I've determined the problem seems to come from the use of floating points to store position/velocity etc. which obviously must result in partial pixels. The main culprit appears to be the addition of the gravity value (multiplied by delta), which is the very small and exact value of 0.00075f when the player leaves the ground. Given the player's position is now being altered by tiny amounts, which then causes the camera to instantly snap based on the player's location, which then causes the game world's objects to be translated relative to the camera's rather exacting floating point coordinates... I imagine that's causing a problem for the tiny 8x8 map tiles, screwing up a line of 1 pixel or so.

As an experiment... I kept everything the way it was, but I rounded the player's drawing coordinates to an int, then did the same thing to the camera coordinates when being used to translate the game world w.r.t. the camera. That seemed to completely get rid of the problem.

My question:
Now, I'm just wondering... I suppose I have to use floats for object coordinates if I want to use gravity like I am while using a framerate independent of logic updates and the idea of using floats instead of ints seems like a useful one, so what should I do to prevent this flickering?
Should I simply keep logical coordinates as floats the way they are, and then cast them to int when it comes to rendering? Is that good practice, or a bit sloppy and error-prone? Or is there something else that would be better?
Offline Magn919

Junior Devvie


Medals: 6
Exp: 5 years



« Reply #1 - Posted 2012-04-20 15:59:11 »

Casting to int on rendering, is likely the best way to do it and shouldn't cause any errors.

This problem often pops up on the slick forum, and most of the time casting to int on render, is the first thing people will tell you to do.
But sometimes making an invisible 1 pixel frame around the image works too, but in your case I'd recommend casting.

For every new problem, a new source of solutions has come to exist.
Offline SeanSeanston

Senior Newbie





« Reply #2 - Posted 2012-04-20 17:32:39 »

Casting to int on rendering, is likely the best way to do it and shouldn't cause any errors.

This problem often pops up on the slick forum, and most of the time casting to int on render, is the first thing people will tell you to do.

Cool then... I'll do that. Thanks.

(Also just realized I forgot to mention the 2nd part of the image was just a Photoshop'd approximation since it was impossible to print screen it fast enough to capture the error, oh well, doesn't matter now persecutioncomplex)
Pages: [1]
  ignore  |  Print  
 
 

 
ivj94 (586 views)
2018-03-24 14:47:39

ivj94 (49 views)
2018-03-24 14:46:31

ivj94 (383 views)
2018-03-24 14:43:53

Solater (63 views)
2018-03-17 05:04:08

nelsongames (110 views)
2018-03-05 17:56:34

Gornova (159 views)
2018-03-02 22:15:33

buddyBro (704 views)
2018-02-28 16:59:18

buddyBro (93 views)
2018-02-28 16:45:17

xxMrPHDxx (494 views)
2017-12-31 17:17:51

xxMrPHDxx (734 views)
2017-12-31 17:15:51
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!