[solved] Lines not rendering correctly Java2D

Hey JGO, I’m having a graphics glitch when trying to render a line from my player to every tile within 5 spaces.

When the tile is to the right of the player the lines render properly.
When the tile is to the left of the player the lines screw up.

Picture:

Thanks for any help :slight_smile:

EDIT:
Here’s my code:
NOTE: Even without my added code (Just a g2d.drawLine call, the glitch still occurs).


		for (Tile t : tiles) {
			// draw tile image
			g2d.drawImage(grassTile01.getImage(), t.getX(), t.getY(), t.getWidth(), t.getHeight(), null);
		
			// player centerX
			int targetX = playerX + 32 / 2;
			
			// player centerY
			int targetY = playerY + 32 / 2;
			
			// If the tile is within 5 spaces to our right.
			// WORKS PERFECTLY
			if (t.getX() >= targetX && t.getX() <= targetX + 32 * 5) {
				
				// if the tile isn't within 5 spaces above us.
				if (!(t.getY() >= playerY - 32 * 5)) {
					
					// don't render a line.
					continue;
				}
				
				// if the tile isn't within 5 spaces below us.
				if (!(t.getY() <= playerY + 32 * 5)) {
					
					// don't render a line.
					continue;
				}

				// if the tile's made it this far, render the line.
				g2d.drawLine(targetX, targetY, t.getX(), t.getY());
				
				// if the tile is to the left (Testing to fix)
				// DOESNT WANT TO WORK 
			} else if (t.getX() <= targetX) {
				
				// if the tile isn't within 5 spaces to our left.
				if (!(t.getX() >= playerX - 32*5)) {
					
					//don't render a line.
					continue;
				}
				
				// if the tile isn't within 5 spaces below us.
				if (!(t.getY() >=playerY-32*5)){
					
					// don't render a line.
					continue;
				}
				
				// if the tile isn't within 5 spaces above us.
				if (!(t.getY() <= playerY+32*5)){
					
					// don't render a line.
					continue;
				}
				
				// if the tile's made it this far, render the line.
				g2d.drawLine(t.getX(), t.getY(),targetX, targetY);
			}
		}

Thanks for any help guys / gals :slight_smile:

I’m going to take a quick swing at this and, guess that the problem is either with your else if statement or with your operators. Sorry for the vague answer.

Even when I render it without my if statements:


for (Tile t : tiles) {
			// draw tile image
			g2d.drawImage(grassTile01.getImage(), t.getX(), t.getY(), t.getWidth(), t.getHeight(), null);

			// player centerX
			int targetX = playerX + 32 / 2;

			// player centerY
			int targetY = playerY + 32 / 2;

			g2d.drawLine(targetX, targetY, t.getX(), t.getY());
		}

It still fails to properly render all lines to tiles that are to the left of ‘targetX’.
If statements just cancel rendering lines to all tiles outside of the 5 spaces of ‘targetX’.
What am I missing? DX

Again, Not sure if this will help any, But I’d recommend putting your math in brackets. IE: playerY + 32 / 2, Should be (playerY + 32) / 2.

Had no effect on the graphics glitch btw ^__^, thanks though.


playerY + (32 / 2)
// same as
playerY + 32 / 2

The lines are correct, it’s just that you’re drawing the tile images over them.
Draw all the tiles in 1 pass, then do another pass where you draw the lines.

Wow, wouldn’t of ever crossed my mind.

Thanks, woot woot :slight_smile:

Are these rays btw?

Or just general messing about?

If they are rays for say 360 degree vision, the spacing and length is a little off for some rays:D. Check yo trig.

Was just goofing for debugging mode :smiley:

drawing debug things with additive-blending is a simple and nice way to get everything on the screen.

last time i checked that with java2D it was very messy to get it to work; another reason why i moved everything to opengl. :cranky:

edit