General Category > General Game and Application Development

Drawing using Integer/Float

(1/5) > >>

Steve Elliott:
At the moment I'm drawing using delta timing and floating point positions for my sprites, but I've heard that can cause some 'smearing' at times so it's best to convert to integers just before positioning a sprite at x, y.

Your thoughts on this? And if you do convert to an integer, do you round up or down or simply pass the floats to integer variables and let the data type lose the remainder?

Qube:
I use floats + delta time and for the perfect pixel sharpness ( like the current game comp ) in AGK I set the image min / max filter to 0. AGK has very good sub pixel rendering :)

Steve Elliott:

--- Quote ---I use floats + delta time and for the perfect pixel sharpness ( like the current game comp ) in AGK I set the image min / max filter to 0. AGK has very good sub pixel rendering :)

--- End quote ---

You're talking about SetDefaultMinFilter( 0 ) and SetDefaultMagFilter( 0 )?  Yes I use those, and yes sub pixel rendering is great, but if I were to switch to another engine I wonder if non AGK people convert to integers?

Derron:
If you need to have it "crisp" and "pixel perfect" then just render them the simple way:
DrawImage(img, int(x), int(y))

BUT - at least in BlitzMax that means you just truncate the fractional part (2.9 => 2, 2.1 => 2). To avoid that, do a simple "round as you learned in school":
DrawImage(img, int(x + 0.5), int(y + 0.5))
This rounds up for >= x.5 and down for < 0.5 (2.9 + 0.5 = 3.4 => 3, 2.1 + 0.5 = 2.6 => 2).
Think you can still do the simple way without much visual issues. Just make sure that you move moving entities to their final target position. Why? If the entity is at x=2.999995 and the target is 3.0 your logic might already think "it reached" and stops moving. When you now render that entity, it gets rendered at eg. "int(2.999995)" so at ... "2" - which is of course not the right position.

Rendering at subpixel positions would lead to blended/anti-aliased sprites if not handled properly (dunno if that MinFilter/MagFilter thing in AGK does disable alpha values).
As you only do this for rendering and not physics, the rounding of numbers is not affecting the movement of your entities.

bye
Ron

Steve Elliott:
Thanks Derron, that makes sense.  But anything over x.5 is going to always round up, surely?
--- Quote ---This rounds up for >= x.5 and down for < 0.5 (2.9 + 0.5 = 3.4 => 3, 2.1 + 0.5 = 2.6 => 2).
--- End quote ---