Pixel perfect positioning

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
foo0
Prole
Posts: 35
Joined: Sun Apr 27, 2014 10:25 am

Pixel perfect positioning

Post by foo0 »

When I draw three points, and triangle from those three points, the points don't overlap with triangle corners. When checked the points position in GIMP, they are exactly where they supposed to be, but triangle from the same points has one pixel missing in width and height. Attached screen shot of the triangle and points, and enlarged right-bottom corner of the image. Why they don't overlap perfectly?

Code: Select all

function love.draw()
	local point1 = { x =  10.5, y =  10.5 }
	local point2 = { x = 200.5, y =  10.5 }
	local point3 = { x = 200.5, y = 200.5 }

	love.graphics.setColor(0, 0, 255)
	love.graphics.polygon("fill", point1.x, point1.y, point2.x, point2.y, point3.x, point3.y)

	love.graphics.setColor(255, 255, 255, 128)
	love.graphics.point(point1.x, point1.y)
	love.graphics.point(point2.x, point2.y)
	love.graphics.point(point3.x, point3.y)
end
Attachments
img1.png
img1.png (421 Bytes) Viewed 3761 times
img2.png
img2.png (204 Bytes) Viewed 3761 times
User avatar
Jeeper
Party member
Posts: 611
Joined: Tue Mar 12, 2013 7:11 pm
Contact:

Re: Pixel perfect positioning

Post by Jeeper »

Just a heads-up, you get a lot more help if you post your problems in "Support and development" rather than "general".
foo0
Prole
Posts: 35
Joined: Sun Apr 27, 2014 10:25 am

Re: Pixel perfect positioning

Post by foo0 »

Yeah, my bad. If someone could move the topic to proper section I would appreciate.
User avatar
DaedalusYoung
Party member
Posts: 413
Joined: Sun Jul 14, 2013 8:04 pm

Re: Pixel perfect positioning

Post by DaedalusYoung »

I'm not sure if you need to add 0.5 to the coordinates in 'fill' mode for polygons. Try changing them to integers, and only add 0.5 for the points.
foo0
Prole
Posts: 35
Joined: Sun Apr 27, 2014 10:25 am

Re: Pixel perfect positioning

Post by foo0 »

@DaedalusYoung: I did that, and it doesn't make a difference, the result is identical pixel-wise.

//

If I change love.graphics.polygon mode from "fill" to "line", the points are drawn on the triangle, so we have some consistency here. Filled triangle in the other hand is drawn in a wrong size - it has one pixel missing in width and height, regardless of 0.5 pixel position correction.
Attachments
img3.png
img3.png (894 Bytes) Viewed 3715 times
img4.png
img4.png (286 Bytes) Viewed 3715 times
User avatar
DaedalusYoung
Party member
Posts: 413
Joined: Sun Jul 14, 2013 8:04 pm

Re: Pixel perfect positioning

Post by DaedalusYoung »

I think this is what's happening.

Consider this grid to be your screen. Every square is a pixel, the black lines are the exact coordinates. So the top-left pixel occupies coordinates 0,0 - 0.999,0.999.

I placed red dots on the exact integer coordinates you would draw the triangle.

If you use the 'fill' mode to draw the polygon, it will be drawn exactly in between the coordinates. I assume non-integer values are rounded down, so 0.5,0.5 will be regarded as 0,0.

Because this is in between, it does not exceed the bottom-right most coordinates.

Drawing points, however, does take non-integer values into account, so by adding 0.5 to all, they will be drawn to the right of their intended location, which does exceed the bottom and right values.

Therefore, points are drawn on a different location, because the filled polygon doesn't exceed the boundaries given.

Image
Post Reply

Who is online

Users browsing this forum: aikusuuta and 7 guests