Determining PNG boundaries always throws exception

Apr 17, 2010 at 1:36 PM


I created a WPF application based on the basic video and I couldn't start it with any PNG files I added to the project. Regardless of shape, quality etc. of the image, I always get the following exception:

Unhandled exception: System.IndexOutOfRangeException: Index was out of range.
 at: Spritehand.FarseerHelper.BoundaryHelper.GetNextVertex(UIElement element, Point current, Point last, Double left, Double top, Double width, Double height, BitmapSource bitmap, List`1 childElements)
 at: Spritehand.FarseerHelper.BoundaryHelper.GetPointsForImage(Image element, UIElement containerElement, Boolean positionOnly)
 at: Spritehand.FarseerHelper.BoundaryHelper.GetPointsForElement(UIElement element, UIElement containerElement, Boolean positionOnly)
 at: Spritehand.FarseerHelper.PhysicsSprite..ctor(PhysicsSimulator physicsSim, Canvas parentCanvas, UIElement element, List`1 points, Boolean showDebug, Single defaultFriction, UIElement boundaryElement)
 at: Spritehand.FarseerHelper.PhysicsControllerMain.AddPhysicsBody(PhysicsObjectMain physObject)
 at: Spritehand.PhysicsBehaviors.PhysicsObjectBehavior.controller_Initialized(Object source)
 at: Spritehand.FarseerHelper.PhysicsControllerMain.InitializedHandler.Invoke(Object source)
 at: Spritehand.FarseerHelper.PhysicsControllerMain.InitializeObjects()
 at: Spritehand.FarseerHelper.PhysicsControllerMain._timerInitialize_Completed(Object sender, EventArgs e)
 at: System.Windows.Media.Animation.Clock.FireEvent(EventPrivateKey key)

An observation: a dll that is referenced in the install.bat file (Spritehand.PhysicsBehaviors.Design.dll), isn't prebuild, one has to build it.

Apr 17, 2010 at 2:15 PM

Just as a sanity check, does the WPF demo included in the download work OK for you? The one with the astronaut falling onto the lever (note the astronaut is a PNG).

Apr 17, 2010 at 2:36 PM

Interesting, with the astronaut png it works fine but with the other 4-5 images it crashes. What requirements are there for the image?

Apr 18, 2010 at 1:36 AM

The algorithm that determines the outline of the image is not very forgiving at all. The image needs to have a very clean outline with no stray pixels. So if there are any pixels hanging outside of the main outline, then the boundary detection can be thrown off because it can't trace around the image.


Apr 18, 2010 at 7:02 PM

Well, I downloaded those png images from different (and seemingly professional) sites, and I couldn't notice any stray pixels.

I'm not aware of how the boundary detecting algorithm works but isn't it possible to omit those stray pixels? The algorithm should skip those pixels and let them be ignored on collisions. This is just a mere suggestion, I do not know what other issues it rises.

Apr 21, 2010 at 6:10 PM

I have experience some issues when I let Blend resize images. Make sure you are using the images at the size you have them on disk. I had to manually rezise some images in and then draw my own solid outline around them to get them to work.

Apr 21, 2010 at 7:49 PM

Hmmm, I resized the images, indeed. I'll give it another try, thanks for the tip.

Apr 24, 2010 at 12:14 AM

I also discovered changing the size of the main Layoutroot can cause problems. Check the "Margins" for the layoutroot. If they are negative (which happens when you move the Layoutroot around with the pointer tool [I think]). Just change the sign and everything seems to work again.

Jun 7, 2010 at 5:39 AM

You must have one transparent pixel on all sides for boundary detection to work.


 Check out this cool Silverlight Game based built using this library