Page view counter

Silverlight Tips of the Day - Blog by Mike Snow

Game Programming with Silverlight

Silverlight Tip of the Day #103 – Use Integers for Layout Calculations

When calculating the positions of objects such as Images make certain to use integer values not floating point values.

For example, take a look at this code that centers a map around a given position:

private void CenterMap(double windowWidth, double windowHeight)
{
    if (null != _parentCanvas)
    {
        int leftPos = (int) ((windowWidth / 2) - _currentX);
        int topPos = (int) ((windowHeight / 2) - _currentY);
 
        _parentCanvas.SetValue(Canvas.LeftProperty, (double) leftPos);
        _parentCanvas.SetValue(Canvas.TopProperty, (double)topPos);
    }
}

Notice I converted the map layout position left and top to be integers. The following screen shot shows an example where double values were used instead of integers. The result is the images are slightly blurred and lines (or seams) appear between the image tiles.

image

Compare the screenshot above that used doubles with the shot below that used integers. In the screen shot below the images are crisp and the terrain tiles have no seams or lines.

image

Thank you,
--Mike Snow

 Subscribe in a reader

Comments

Microsoft Weblogs said:

When calculating the positions of objects such as Images make certain to use integer values not floating

# March 26, 2009 3:29 PM

darktatami said:

Awesome, I'm having a very simular issue with moving around my tile map. I'll do my calculations in ints and then change them to doubles before I move the map. Hopefully this cleans it up a bit, thanks again. :)

# March 27, 2009 10:03 AM

Eric Willeke said:

Try casting to float instead of int. I noticed in the course of doing some other work that a double value that's passed through AgCore isn't the same as went it went in. Investigation led me to realize that agcore.dll took everything in floats instead of doubles, and the difference was rounding error from the cast.

It's been a while since I explored it, but I think something like this demonstrates it... might have to use different properties:

double pi = Math.Pi;

Canvas c = new Canvas();

c.Width = pi;

double pi2 = c.Width;

Debug.Assert( pi == pi2 ); // Fails!

Debug.Assert( pi.Equals( pi2 )); // Fails!

Debug.Assert( (float) pi == (float) pi2 ); // Passes

# March 27, 2009 1:30 PM

WPF and Silverlight tips and tricks | WPF Dev said:

Pingback from  WPF and Silverlight tips and tricks | WPF Dev

# March 27, 2009 7:31 PM

Visual Web Developer Team Blog said:

Most Recent Posts: Silverlight Tip of the Day #106 - Setting Default Browser from within VS Silverlight

# April 6, 2009 6:13 PM

Silverlight Tips of the Day - Blog by Mike Snow said:

The purpose of this post is to create an outline summary all the blogs from my Silverlight Tips of the

# April 6, 2009 6:20 PM

SharpGIS said:

Instead of doing all that rounding yourself, isn't this what we have UseLayoutRounding="True" for ?

# April 17, 2009 1:29 PM

DDtMM said:

If you check the documentation on UseLayoutRounding you'll see it is True by default... yet I've gotten results similar to the top (bad) screenshot.  Maybe it is different in SL3?  I haven't tried.

# June 30, 2009 2:25 AM

NewsPeeps said:

Thank you for submitting this cool story - Trackback from NewsPeeps

# August 8, 2009 6:36 PM

malignate said:

Hi,

I make a silverlight jump and run game at the moment and have the same problem: http://www.gpstudio.de/JnR.png

I found your post and tried your solution, but it does not work. I also tried to add a pixel or two a scale transformation from with scaling in x and y direction of 1.01, but it has some other ugly side effects, especially at the background layer.

Do you have another idea?

Would be nice o get some tipps.

# August 24, 2009 7:55 AM

mike.snow said:

Hi, did you try using UseLayoutRounding ?

Also, it's hard to know what you are doing wrong without seeing the code.

You can post your code here for layout out your map or you can mail me your project and I will take a quick look.

msnow @ microsoft.com

Thanks,

--Mike

# August 24, 2009 12:19 PM