Farseer Physics Engine refactoring status

Aug 24, 2008 at 3:48 PM
Edited Sep 15, 2008 at 1:38 PM

This discussion is being used to report to everyone what is being done to the Farseer Physics Engine. I'm not an official developer of the Farseer engine and have a lot of changes in my mind, that I want to implement in Farseer. The implementations will hopefully make sure that Farseer is continueing to be a easy to use, high quality engine for a lot of platforms.

I've compiled a list of things to do. This list will be updated as Farseer is evolving and new changes comes up.

High priority list:
1. Split the different platform specific code regions into seperate projects
2. Automated code cleanup by using tools
3. Change of codestyle to comply with Microsoft coding standards.

Current progress:
1. I've cleaned the mainstream code for all compiler statements to make sure it's platform clean. This means that the mainstream ONLY work on .net 3.0 and might not work on platforms such as XNA, Silverlight and Xbox. It will, however work in Winform, OpenGL, MDX and other platforms/libraries that uses vanilla .net 3.5.

2. Deleted and deactivated some code that was used for debugging. I've also deleted some work that was unfinished.

3. The project is now under source control. This makes sure that all changes to the code is completely reversible. Including the changes in listing 2.

Update 1:
I have manually checked each and every file for places where i can refactor and optimize. I've found some code redundancy that would greatly increase the simplicity of the code. One file even contained 69.7% redundant code.

Update 2:
I have finally created something that seems to work. ;) The mainstream code is now refactored and cleaned. I've tried to keep it simple while still minimizing code redundancy. I have not yet tested the code, I will do it as soon as I get the XNA 3.0 branch up and working.

Update 3:
It's about time to mark the refactoring of the mainstream source as finished. :) There are some changes that will break backwards compatibility, I have a list of them here:

1) Namespace updates. It makes the engine a little more complex, but easier to maintain as everything is logically divided.
2) There used to be a Enum class that contained all the enums. This class has been removed and all Enums are public in the FarseerGames.FarseerPhysics namespace.

I have not changed the public API. Farseer already has a very easy to use API and there is not much I wanted to change.

Farseer should also have a very little performance increase as I removed a lot of temp/debug stuff and redundant calculations. The addition of dp2208's changes should provide a great boost of the performance (Simple code, thanks dp2208! Converted your code to use .net 3.0)

Update 4
Farseer Physics Engine should be almost ready. I did something to kill the previous version and make it really slow. This should now be fixed.
The farseer demos are more simple now. Each demo contains only demo specific code and not game code.

I will have a fully working version of the clean Farseer Physics engine up around the weekend. (With cleaned demos).
I have also split the engine and demos up into platforms. The engine and demos will have an Xbox version in the source control soon.

Update 5
The Farseer code in source control is just about cleaned and the demos are finished.

A picture is worth a thousand words (so here is a few):

Farseer performance:
Click here

Farseer 2.0 performance:
Click here

Those performance tests were done without any drawing and can only be used to compare relative speed. They are by no means real life tests.

I've also updated the samples. This is the complexity results of version vs. 2.0

Project: Farseer
Maintainability Index: 71
Cyclomatic Complexity: 837
Depth of Inheritance: 3
Class Coupling: 106
Lines of Code: 2.361

Project: Farseer 2.0
Maintainability Index: 72
Cyclomatic Complexity: 734
Depth of Inheritance: 3
Class Coupling: 102
Lines of Code: 2.029

Maintainability Index: Higher is better
Cyclomatic Complexity: Lower is better
Depth of Inheritance: Low = simple | high = abstract
Class Coupling: High vs. Low coupling
Lines of Code: No need to explain this :)

In my own tests, I get 2-4 frames more in the stacked boxes (pyramid) test from the samples. It's not a lot, but the goal was TO NOT get less than (or at least the same) ;)

If you are bleeding edge and love to look at source code. Go ahead and download the current source code check-in (#39750). You are welcome to test it and please tell me what you think.
Aug 24, 2008 at 4:41 PM
Edited Aug 24, 2008 at 4:43 PM
Just an FYI for everyone.  I asked genbox to help manage the Farseer Physics code-base.  I simply do not have the time to do it and genbox has a great understanding of where to take things.  I'll be helping as much as I have time, but I'm leaving a lot of the code-refactoring and source control decisions up to him.

-Jeff Weber

And just to be clear, we fully intend to have source control and formal releases for each of the platforms: XNA, Silverlight, Vanilla .Net 3.5.

That is the plan, right genbox? :-)
Aug 24, 2008 at 6:17 PM
Indeed it is crashlander.
Aug 24, 2008 at 9:29 PM
Updated the post with update 2.
Aug 24, 2008 at 10:53 PM
@Crashlander - You might want to have a look at the current source code in the mainstream.
I was lazy and did not create any unit tests/other test before refactoring, so I had no way of testing the functionality of the code. I've introduced a bug somewhere that involves collision detection. I reworked Demo2 from the samples to work with the refactored engine, torque and force seems to work, but when the elements collide, it gets weird. I've not yet tried it with the debugging/debugview enabled.

It would be nice If you took a look at the code. If you find the bug I will send you a beer ;)
Aug 25, 2008 at 12:49 AM
I can, but it might take me a bit.  I don't currently have the VS 2008 Team System Explorer installed.  I assume this what I should be connecting to the source with?

The collision code is touchy.  There is some "interesting" code there. 

Can you tell me, broadly,  what you changed in regard to the collision code?

Aug 25, 2008 at 8:52 AM

Yep, I've been using Visual Studio 2008 Team Explorer to connect to the source.
The current status of the SVN to TFS bridge is stable, but they do not yet support merge and the like. I'll just stick to Team Explorer for now.

Phew, I've changed about every line in of code in the project by now. I have found 3 small bugs and fixed them, but they did not reside in the collision code if I remember correctly.
I'll have a look at the code when I come home, I'll properly go some revision/changesets back to see where the bug was introduced, this might help in the bughunt.


oh, by the way. They bugs was of this kind:

if (geom1 != null || geom1 != null)

Nothing major.

Aug 25, 2008 at 2:18 PM
Okay. Time for a status update on the bug:

I'm on my way to locate the code where the bug resides. I have just checked changeset 37665 and it works. After the 37665 changeset I changed some of the architecture in Farseer. This can be very tricky.

The good news is that all the major refactoring happened in the 37665 changeset. So if I can't find the bug I can just revert the source base back to 37665 ;) Long live source control.
Aug 25, 2008 at 2:43 PM
Bug fixed :)

It was introduced in #37675. Going to continue work.
Aug 25, 2008 at 5:34 PM
Excellent!  Glad you found it because I'm not sure when I could have found the time to look at it.

Aug 25, 2008 at 5:36 PM
I hate those kind of bugs :)

I've updated the original post with Update 3
Aug 25, 2008 at 8:45 PM
Just a little more clarification for those following this thread. 

genbox mentioned in "Update 3" that he re-worked some namespaces and broke backward compatibility.

I told genbox that the next release of Farseer would be the version 2.0 release and that if it made sense go ahead and make breaking changes.  I know this might suck for some people, but the earlier release will still be available and hopefully it won't be to big a headache since it's just namespace changes. (so far :-)

Sometimes you have go break things to make them better!

Aug 26, 2008 at 10:51 AM
First of all, thank you, both of you, for creating this thread to make it clear that Farseer Physics is moving forward!

Thank you Genbox for taking on the job of managing and improving the codebase. Although I haven't looked at the changes yet, I think it sounds great that you are refactoring and tidying up the code.

I really look forward to release 2.0 whenever it's ready. :-)
Aug 26, 2008 at 11:08 AM
Thanks reneq.

Since crashlander is buzy on his job and buzy implementing his game, I'll take over the responsibility of the current source code, bug fixes and the discussions. I'll trying to keep it lively :)

The current source checkin works with XNA 3.0 if you want to try it out.
Farseer Physics Engine 2.0 will rock your world ;) in a practical sense.
Sep 2, 2008 at 7:04 PM
Posted update 4 in original post.
Sep 3, 2008 at 5:18 PM
This is fantastic.  I've been considering adding Farseer to my 2-D engine, even created a class for it and considered a unified interface for all physics engines (so far I have Physics2D implemented but would like more options), but decided against the effort.  With all this refactoring and optimizing — and provided sleeping/wake-up is integrated — I'm definitely going to implement Farseer as soon as you have a release candidate.  Question:  will you only release through SVN, or do you intend to put out another source release?  I would much prefer the latter myself.
Sep 3, 2008 at 5:36 PM
@OmegaThree - You need to remember that diffrent physicsengines work in diffrent ways. But I really think that you should give Farseer a try. You will find it to be very easy to integrate and a wonder to work with.

The current source code checkin (id 38815) is cleaned and is working fully on XNA 3.0, but it will be a while before the Farseer Physics 2.0 release as we are waiting for XNA 3.0 to be released and we also need to implement some new features.

Hang around the discussions and watch the progress. :)
Sep 14, 2008 at 1:03 AM
Updated the original post with Update 5

Some info on the progress and info on some small performance improvements.
Sep 15, 2008 at 11:40 AM
Good to see you are still making progress, genbox.  Results look good.