Porting Farseer XNA to Windows Phone 7

Mar 18, 2010 at 9:22 PM
Edited Mar 18, 2010 at 9:23 PM

I noticed some people have ported Farseer to Windows Phone 7.

What changes did you have to make to get Farseer to compile.

My first attempt was to just add the farseer .csproj.  But that didn't work because you can't upgrade XNA 3.1 projects to 4.0...yet.

So I manually copied all of the Farseer folders and classes into a new XNA Game Studio 4.0 Windows Phone Game Project.

Just about every class had a namespace error.  For some reason the directives don't work the same as they did in VS 2008.

So I just copied everything contained within the #if (XNA) to the top of every class.  But now I have errors anywhere a List.Removeall is used.

The generic List is fine but there is no RemoveAll option for Lists.  Does anyone know what I'm doing wrong.

Thanks

#if

(XNA)

using Microsoft.Xna.Framework;

using Microsoft.Xna.Framework.Content;

#else

using

FarseerGames.FarseerPhysics.Mathematics;

#endif

The #if (XNA) is not working. 

 

 

Mar 18, 2010 at 11:38 PM

XNA doesn't fly for Windows Phone, you have to use WINDOWS_PHONE for it to recognize.

In order to get stopwatch support, you have to add Microsoft.Devices.dll as a reference.

 

RemoveAll is not in the compact framework for the new XNA, so you have to write it

yourself. It's not that hard, just create a static class somewhere in your project that

takes a generic, and make RemoveAll take your list of generics and the generic

predicate.

 

You should be set at that point.

Mar 19, 2010 at 1:35 AM

This is turning out to be much more difficult than I thought.

Per a different thread, I decided to just reference the Farseer .dll in my Windows_Phone XNA 4.0 project.

This is the error I received.:

Warning 3 The primary reference "FarseerPhysics" could not be resolved because it has an indirect dependency on the framework assembly "mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" which could not be resolved in the currently targeted framework. ".NETFramework,Version=v4.0,Profile=Client". To resolve this problem, either remove the reference "FarseerPhysics" or retarget your application to a framework version which contains "mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089". LavaRunnerPhone

It appears the mscorlib version that is used by Farseer in XNA 3.1 needs to be updated, then Farseer can be compiled and then you may be able to use the Farseer .dll in a Windows Phone XNA project.

I'm still trying but this may be deeper than my brain can handle.

Allan

 

 

 

 

 

Mar 19, 2010 at 2:33 AM

Ok.  I'm still trying.

I don't think just referencing Farseer as a .dll will work since it uses an older version of mscorlib.

So I started over and after creating a Windows Phone XNA 4.0 project, I copied over my project classes.  Then I created another project and copied over the Farseer classes.

I added the Conditional compilation symbol XNA in the Build tab in properties for the Farseer project.  So it looks like this:

WINDOWS_PHONE;XNA 

I referenced Microsoft.Devices.dll for XNA and that took care of stopwatch.  Thanks oranjoose.

Now I'm stuck on the .RemoveAll.

Oranjoose, can you provide a code example of a static class that would replace the RemoveAll command?

Because although what you say makes sense, I don't how to write a class that will do that.

Thanks

Allan

Mar 19, 2010 at 6:33 AM

 

public static int RemoveAll(List<T> list, Predicate<T> match)
        {
            int num = 0;
            for (int i = 0; i < list.Count; ++i)
            {
                if (match(list[i]))
                {
                    num++;
                    list.RemoveAt(i);
                    i--;
                }
            }
            return num;
        }

public static int RemoveAll(List<T> list, Predicate<T> match)

        {

            int num = 0;

 

            for (int i = 0; i < list.Count; ++i)

            {

                if (match(list[i]))

                {

                    num++;

                    list.RemoveAt(i);

                    i--;

                }

            }

 

            return num;

        }

Just make sure that the class this is in can cast the generic

I noticed your post about entry points on the creators page. Entry point are strange with Windows Phone

because of the way the system works. That whole void main thing in Program.cs or wherever doesn't work

with this system.

The phone just searches for the first instance of the game class, and its constructor. The constructor of

a class that is of type Game will be found, and that's the entry point. If you're dealing with many instances

of Game, then you'll probably have a problem. Try then to create a separate class and organize them

with a manager that has all the information necessary from the single Game class in your project.

 

Hope that helps 

 

Mar 19, 2010 at 3:20 PM
Edited Mar 20, 2010 at 12:15 PM

Well I got it to compile and run but nothing appears on the emulator screen.

Here is what I had to do:

First you can't add Farseer as a .dll because Farseer references an old and crusty version of mscorlib. :)  XNA 4.0 wants a newer version and it tells you so.

Second, Windows Phone works differently with game entrypoints.  My mistake was creating a new XNA 4.0 project in Windows Phone and copying all of the Farseer classes into that project.  What you have to do is create an XNA 4.0 Game Library and there is a template for that in VS 2010 right below the project option and then copy all of the Farseer classes into that Library.

Third, reference the Farseer library in your game project.

Fourth, Stopwatch is not included by default so you have to add a reference to the Farseer project library in order to access StopWatch.  reference Microsoft.Devices (There are two, choose the top one in the (ProgramFiles)\Reference Assemblies path per Nick Gravelyn's suggestion.

Fifth, Add a compiler directive to the Farseer properties.  Just double click properties in the Farseer project, click the Build tab in the properties window, and in the Conditional compilation symbols add ;XNA. (It should look like this.....Windows_Phone;XNA

Sixth, it compiles and runs with my current project. I commented out all of the RemoveAll statements in the two classes that use those statements. ( SelectiveSweepCollider.cs and SweepAndPruneCollider.cs).  But I am encouraged that it compiled and ran successfully.  

Seventh, After numerous attempts at trying to figure out how to implement the RemoveAll extension method, I finally decided on a simpler solution.  I commented out the #if (!SilverLight) section thus exposing the #else section in both SelectiveSweepCollider.cs and SweepAndPruneCollider.cs.  This exposed the remove code reserved for SilverLight developers.  It ran and I hope that means it's all working fine.  If I had to guess, I would say this means SilverLight also does not contain the RemoveAll and thus a different method was employed to remove stuff.

Video of Farseer XNA on Windows Phone 7:

http://www.screentoaster.com/watch/stUEpWS0VJRFtZSV1UXVJaV1NQ/farseer_xna_on_windows_phone_7

After I made Nick's change with the Microsoft.Devices reference the game ran.  It's pretty sweet to see the GameStateManagement menu system show up on the phone emulator and to see my early farseer project running on a Windows Phone Emulator.  The RemoveAll statements are in the two classes that use those statements. ( SelectiveSweepCollider.cs and SweepAndPruneCollider.cs).

Allan

Mar 19, 2010 at 4:24 PM
Edited Mar 19, 2010 at 4:28 PM
oranjoose wrote:

     Just make sure that the class this is in can cast the generic

Oranjoose,

Thanks for that starting point.  But not having any formal OOP programming training puts me at a disadvantage.  I started from scratch with C# and OOP 18 months ago so I don't know what the quote above means exactly.

I added your code to my main Engine class (which is just a renamed Game1.cs) and of course the compiler does not like the <T>.  It asks me if I am missing a namespace or using directive. 

I do have using System.Collections.Generic in my class so that handles the List command.

Intellisense gives me two options for the <T>,  Generate Class for 'T' and Generate new type.  Now I've seen this notation used before in communication between programmers and I think it is just a way to communicate that the T represents whatever List type you are working with...ie. Texture2D, Vector2 etc. etc.

 Here is the code from SweepAndPruneCollider.cs:  How do I implement your method and alter this code in order to use your method.

 public void ProcessDisposedGeoms()
        {
            if (_xInfoList.RemoveAll(i => i.Geometry.IsDisposed) > 0)
            {
                _xExtentList.RemoveAll(n => n.Info.Geometry.IsDisposed);
            }

            if (_yInfoList.RemoveAll(i => i.Geometry.IsDisposed) > 0)
            {
                _yExtentList.RemoveAll(n => n.Info.Geometry.IsDisposed);
            }

            CollisionPairs.Clear();
        }
Mar 19, 2010 at 8:51 PM

I got my game running with Farseer on Windows Phone using XNA 4.0 but I am not using the RemoveAll commands contained in SelectiveSweepCollider.cs and SweepAndPruneCollider.cs.  I ad to comment out any use of RemoveAll.

 

But it does run.

:)

Allan

Mar 20, 2010 at 7:35 AM

chaneya, oranjoose just gave you the extension method implementation of RemoveAll above... all you gotta do is copy/paste :P

Mar 20, 2010 at 10:09 AM

kensou,

Yeah I tried that.  Unless there is somewhere special I am supposed to paste the extension method that I don't know about.

I get the following compiler errors when I paste the code and it doesn't matter where I paste it.

Error 1 The type or namespace name 'T' could not be found (are you missing a using directive or an assembly reference?) C:\XNA\LavaRunner\Farseer Physics 2.1.3 XNA\PhysicsSimulator.cs 131 42 FarseerPhysicsXNA

I'm sorry but I don't understand how <T> is being used in this context or what to replace <T> with if it is supposed to be replced.  If there is a namespace I'm missing, I don't know what it is.  And I don't know what oranjoose means when he says "Just make sure that the class this is in can cast the generic".  You may as well say to me, "Just make sure you don't cast a brick off that bridge."  I have no idea what "cast the generic" means unless generic broke an arm and needs a cast.

Oddly enough, my game runs with having the RemoveAlls commented out in SelectiveSweepCollider.cs and SweepAndPruneCollider.cs and the physics appears to be working so I'm proceeding.

I'll keep waiting to see if someone can explain what I'm doing wrong with this extension method.

Thanks

Allan

Mar 20, 2010 at 11:08 AM

I added to following to my ToDo list for getting Farseer 2.1.3.0 to run in XNA 4.0 on Windows Phone.

Seventh, After numerous attempts at trying to figure out how to implement oranjoos's extension method, I finally decided on a simpler solution.  I commented out the #if (!SilverLight) section thus exposing the #else section in both SelectiveSweepCollider.cs and SweepAndPruneCollider.cs.  This exposed the remove code reserved for SilverLight developers.  It ran and I hope that means it's all working fine.  If I had to guess, I would say this means SilverLight also does not contain the RemoveAll and thus a different method was employed to remove stuff.

And that's it...it runs.

Allan

 

Mar 20, 2010 at 12:14 PM

Video of Farseer XNA on Windows Phone 7.

http://www.screentoaster.com/watch/stUEpWS0VJRFtZSV1UXVJaV1NQ/farseer_xna_on_windows_phone_7

 

Allan

Jun 7, 2010 at 11:51 PM

does anybody have the code for this zipped up? For some reason I think I have these changes but my screen only shows the SteelBlue backgroun with no menu options..

hmm..