1
\$\begingroup\$

I've been working on a game in Java, and recently I decided to try out my game on a netbook just to see how it would run. While I do get a constant and decent FPS, it appears that my lighting rendering decides to freak out a bit. This only happens on the netbook, however, and I'm not sure how I can fix it.

Here's the lighting glitch:

Glitch

I'm doing my lighting simply by drawing black rectangles with certain alpha values over each tile.

As you can see, for some reason there are sometimes vertical and (although not shown) horizontal lines that the alpha rectangles apparently aren't drawing over.

The lines flicker on and off as I move around, and placing two blocks on top of each other prevents the glitch for those blocks.

I'm using Graphics to draw on a JFrame, and the block rendering code looks like this:

g.drawImage(sprite, (int)x, (int)y, null);
g.setColor(new Color(0,0,0,<insert darkness level>));
g.fillRect((int)x,(int)y,32,32);

Someone on another forum thought it could be rounding errors with the block positions, but all the blocks are snapped to a 32x32 grid on creation.

I'm not sure there's anything wrong with the code itself, which is why I'm not sure how to fix this. If someone could help, I'd be really, really grateful, because I'm truly stuck here. Thank you in advance!

\$\endgroup\$
8
  • \$\begingroup\$ Are the lines at the end of a block or at the start? (if you draw one block do the lines appear on the right or the left?) \$\endgroup\$
    – Eejin
    Aug 1, 2014 at 11:01
  • \$\begingroup\$ The lines are on the right side of the blocks. \$\endgroup\$
    – TheBeocro
    Aug 1, 2014 at 11:09
  • \$\begingroup\$ Update the netbook's drivers. This sounds like some internal rounding problem. Does drawing a block of size 33,33 fix the problem? \$\endgroup\$
    – Eejin
    Aug 1, 2014 at 11:10
  • \$\begingroup\$ I figured it was the netbook. Drawing a 33x33 block doesn't work because, as I said, the lines flicker on and off, so now the alpha rectangles overlap and create black lines. \$\endgroup\$
    – TheBeocro
    Aug 1, 2014 at 11:12
  • \$\begingroup\$ I guess I'll just add an option to disable lighting rendering for others who have this problem. Thanks for your help, though! :) \$\endgroup\$
    – TheBeocro
    Aug 1, 2014 at 11:23

2 Answers 2

2
\$\begingroup\$

This sounds to me like a pixel mapping issue. First try varying the resolution to see if the problem appears on other systems and whether a resolution change affects it on the problematic device.

When rendering the sprite needs to be mapped to screen space. Let's say its a 400x400 resolution and your drawing 12 blocks horizontally. That means your 32 width rectangle maps into 400/12 pixels which comes out as 33.33. Not very nice.

But we can't do a third of a pixel, although that would be nice, so we hope that while some are rounded down to 33, the others are rounded up to span 34 pixels thus leaving no gap. However that doesn't happen. So the best way to handle this is to make sure the resolution is always a power of 2 and so are our sprites. That should make them map directly.

Note: I mention this because the sample image seems to be 378x368 pixels.

\$\endgroup\$
3
  • \$\begingroup\$ Your answer is missing a solution: namely, to round all values (up or down) before drawing. \$\endgroup\$
    – ashes999
    Aug 1, 2014 at 13:26
  • \$\begingroup\$ ashes999 - Edit it in, although I think in this case that rounding is happening outside the developers control i.e. in a library/graphics pipeline. That's why the solution I mention is to make sure everything is a power of 2. \$\endgroup\$ Aug 1, 2014 at 13:28
  • \$\begingroup\$ Changing the resolution from 1024x600 to 800x600 didn't change anything. The reason the screenshot is 378x386 is only because I cropped it. The real resolution of the game is 720X540. I don't completely understand what you mean about the pixel mapping, but I don't think that's the issue. (though I could of course be wrong) \$\endgroup\$
    – TheBeocro
    Aug 1, 2014 at 18:49
2
\$\begingroup\$

I know this is old, but I finally discovered the solution. In my game, I am using g.translate(x,y); but I was using a float for the x and y values. So I did this:

g.translate(Math.round(transX),Math.round(transY);

Now there are no more vertical lines! Hopefully this helps anyone else with this problem.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .