Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Bug with line drawing? Shouldn't it go from p1 to p2? (easy)  (Read 7216 times)

0 Members and 2 Guests are viewing this topic.

sofakng

  • Newbie
  • *
  • Posts: 18
    • View Profile
Bug with line drawing? Shouldn't it go from p1 to p2? (easy)
« on: January 28, 2009, 05:14:52 pm »
Here's what I'm doing:

1) Drawing a line from (0, 0) to (11, 10).
2) Drawing a 1x1 sprite at (11, 10).

However, the sprite is drawn AFTER the end of the line.  In other words, the line is not ending at (11, 10).

What's going on here?

Here is my code:

Code: [Select]
     ' Define points
      Dim p1 As Vector2 = New Vector2(0, 0)
      Dim p2 As Vector2 = New Vector2(11, 10)

      ' Create and draw the line
      Dim line As SFML.Graphics.Shape = SFML.Graphics.Shape.Line(p1, p2, 1, White)
      m_renderWindow.Draw(line)

      ' Create and draw the sprite
      Dim sprite As Sprite = New Sprite(New Image("tiles\1x1.bmp"))
      sprite.Position = p2
      m_renderWindow.Draw(sprite)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Bug with line drawing? Shouldn't it go from p1 to p2? (easy)
« Reply #1 on: January 28, 2009, 05:22:48 pm »
Can you show a screenshot?
Laurent Gomila - SFML developer

sofakng

  • Newbie
  • *
  • Posts: 18
    • View Profile
Bug with line drawing? Shouldn't it go from p1 to p2? (easy)
« Reply #2 on: January 28, 2009, 05:53:14 pm »
Thanks for the quick response.

Upon further testing, drawing a line directly through OpenGL produces yet another different result but this one seems to be correct.

Here are screenshots:

Line drawn with SFML:


Line drawn with SFML plus sprite (notice the sprite is after the line):


Line drawn with OpenGL (red) on top of SFML line (white):

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Bug with line drawing? Shouldn't it go from p1 to p2? (easy)
« Reply #3 on: January 28, 2009, 06:10:48 pm »
Oh, so you're talking about a 1 pixel difference. Remember that you manipulate coordinates with float components, and that all this stuff gets converted to integer for displaying into the window.

Moreover, there are a few tricks involving half-pixels adjustments for sprites in SFML.

So, you shouldn't rely on pixel perfect mapping in this context.
Laurent Gomila - SFML developer

sofakng

  • Newbie
  • *
  • Posts: 18
    • View Profile
Bug with line drawing? Shouldn't it go from p1 to p2? (easy)
« Reply #4 on: January 28, 2009, 06:37:57 pm »
Darn floats.  They always cause problems! :)

The reason I found this bug was because I'm trying to draw isometric tiles using the SFML drawing functions but the sides of the tile do not line-up correctly.  I guess it's a little confusing because OpenGL and Flash both seem to render the lines "correctly" (but I suppose correct is a relative term).

I might be able to use pre-generated sprites so my lines are always correct but I was hoping to use the drawing functions for other purposes as well.

quasius

  • Full Member
  • ***
  • Posts: 166
    • View Profile
Bug with line drawing? Shouldn't it go from p1 to p2? (easy)
« Reply #5 on: February 01, 2009, 07:22:20 pm »
Quote from: "sofakng"
Darn floats.  They always cause problems! :)

The reason I found this bug was because I'm trying to draw isometric tiles using the SFML drawing functions but the sides of the tile do not line-up correctly.  I guess it's a little confusing because OpenGL and Flash both seem to render the lines "correctly" (but I suppose correct is a relative term).

I might be able to use pre-generated sprites so my lines are always correct but I was hoping to use the drawing functions for other purposes as well.


You should always be allowing a pixel or so of overlap in your displays to prevent these "texture seams."  Remember OpenGL or any 3D API correctly uses floats, so you have to expect and prepare for rounding variance.
You can possibly ignore this if everything you draw is axis-aligned and you always force it to the int value you want (with checks to make sure it rounds the correct way), but this will never be the case with "isometric" tiles.  Either way, just having all your terrain textures (or any textures that are supposed to touch and match up) overlap by 1 pixel will fix all these issues.