This project has moved and is read-only. For the latest updates, please go here.

Dead Simple Collision Example

Oct 13, 2009 at 9:20 PM

I have read, re-read, and read again the manual and parsed through all the code for the "Simple Samples".  However, for someone who is new to XNA, these "Simple Samples" are built upon a complex construct.  For example, if I want to see a sample of how to create to Geoms and have them bounce off each other when they collide, there is nothing dead simple in any of the "Simple Samples."  Does anyone have something this fundamental? 

Thanks in advance...


Oct 13, 2009 at 10:06 PM

The simple samples are simple in that they show off the very basics of Farseer Physics Engine. Looking away from all the menu, drawing and game structure stuff, the code related to Farseer is also very simple. All you need is contained inside the DemoX.cs files.

For a simple self-contained demo of Farseer Physics Engine, you could take a look at our list of tutorials. I have more "getting-started" samples on my todo list for the next major version of FPE.
Oct 13, 2009 at 10:56 PM

Thanks for the quick reply, genbox, but that is simply not true.

If you copy and paste the majority of the code from say Demo2.cs, then it requires the RectangleBrush, CircleBrush, and ScreenManager classes at the very least.  One cannot simply look at the Demo2.cs code and re-create the same or simliar app/game that ALL the underlying code in the SimpleSamplesXNA actually creates.  If you want to take two bodies, make two geoms, then make those two geoms bounce off of each other when they collide there is currently no straightforward approach.

Also, I've gone through every one of the tutorials on that list and none deal with just simple collision, for example.

I'm not complaining, but I'm just saying that if you try to create some of the simple demos, you have to then re-create/reference all the classes that were create for the demo app's purpose as opposed to JUST the code that you are trying to accomplish.  For example, for total newbies, it would make sense to have the demos broken up into standalone apps, so Demo1, and Demo2, ..., DemoN are all self-contained and don't reference classes that aren't necessary to create the desired effect.

Oct 14, 2009 at 1:30 AM

First of all copying code from demos is defiantly not the way to learn.
Best thing to do if really want to copy code is to type it line by line (using intelligence really makes this fast) as this way you will get in your head what each line does way better than just reading it.

Also these brushes are just debug stuff for Farseer essentially, Farseer of course assumes that you already know how to program at least basically...
And you must know how to render sprites and stuff (You have done the samples from the XNA site right?)

once you have a basic understanding of programming with c# and have made a lot of small little apps, only then are you ready for a physics engine.

A physics engine is not a small thing; Farseer makes it so ridiculously easy it’s not funny.

In short.
Farseer is not a rendering engine.
You can get a physics engine with a 2 box’s in it in about 5 lines of code.

//Not real code but almost

physicssimulator thephysicssimulator = new physicssimulator(vector2 gravity);
Body bod1 = bodyfactory.get.createbox(thephysicssimulator, vector2 size, mass);
Geom geo1 = geomfactory.get.createbox(thephysicssimulator, od1, vector2 size);
Body bod2 = bodyfactory.get.createbox(thephysicssimulator, vector2 size, mass);
Geom geo2 = geomfactory.get.createbox(thephysicssimulator, bod2, vector2 size);


//Now just render it using a sprite (XNA FORUM IF YOU DONT KNOW HOW)
spriteBatch.render(yourSprite1, bod1.position, bod1.rotation);
spriteBatch.render(yourSprite2, bod2.position, bod2.rotation);

And that’s it all done, you now have 2 box’s rendering (using sprites)
and in the physics simulator (they will just fall thou)

Also all of this is in the simple samples (they are called that for a reason)
They really do have the bare minimum and as long as you can ignore the rendering code (which is needed so you can see the demos)
The demos are written in a way that is easy to understand while being easy for the dev's to keep updated and fast to write as this gives them more time to work on the new versions of Farseer (which is what we all want)

The first 3 demos are all focused on simplicity first and foremost if you cannot already make a small game without Farseer then you defiantly cannot make on with it.
I think you should try some of the XNA samples first until you understand how the whole rendering and movement process works first.

Oct 14, 2009 at 2:29 AM

@joemccann: I know how frustrating a new project can be when you don't just have something that show you the very essentials. However, the truth is that this is an open source project and I'm counting on the community to help me make the project better.

As for the simple samples, they take a little getting used to, but when you take the time to learn how they work, they make a good reference on how to do stuff. As I wrote before, a "getting-started" sample that only draw a predefined texture for visual reference is on my todo list. I know there exist at least two of those projects on the forums, but since I'm working on FPE 3.0 that will be fundamentally different from FPE 2.x, I won't bother setting it up now.

Feel free to ask about anything on the forums that is physics engine related. Reading posts here, the manual, FAQ and tutorials makes you an expert in FPE in no time.

Oct 14, 2009 at 3:47 PM

XNA is a fairly advanced framework to use without adding a physics library like Farseer.

You can try checking out: which is simple examples using Silverlight (a framework for creating rich web pages). This should allow you learn Farseer in isolation from XNA.

If you then do some research on XNA by itself, you should be able to come back to the farseer samples with more understanding.


Oct 16, 2009 at 4:18 PM

Thanks for the responses, guys.  I appreciate all of your input and insight.

@DantheKilla:  I have completed many simply XNA tutorials and samples and was able to implement them very quickly.  Adding even a particle library and getting up to speed was no problem and understanding how XNA is not event-driven, but "poll-driven" (for lack of a better term) and how that changes how one programs.  I've been successful in manipulating sounds with various non-traditional input devices, color transitions, scaling of multiple sprites, etc.  For whatever reason, trying to sort out the Farseer library was the only real problem I have encountered.