Getting Started Help

Jun 21, 2009 at 8:52 AM

Ok, I've been tearing my hair out for a week trying to figure out how to use Farseer with XNA.  I've known how to program for a while, so thats not the issue.  The issue is theres next to no documentation for Farseer.  The manual seems to be put together in a matter of minutes just to get it out of the way, where the "SimpleSample" is anything but "Simple".  It has way too much going on with various gamescreens and parent/child classes and inheritance to really be an example of how Farseer works.  For the future, I suggest that you do a "Hello World" for Farseer.  Even for seasoned programmers, "Hello World" applications can help out a lot with learning new API's and engines and such.

Anyway, after a week, I have the ability to just have some basic bodies... thats fine for now.  But I can't get the Geometries to collide.  Heres what I have in a semi-psuedo-code:

Psuedo-code:

MainGame:

PhysicsSimulator physicsSimulator = new PhysicsSimulator(new Vector2(0, 200));

Object object1 = new Object(this, physicsSimulator);

Object object2 = new Object(this, physicsSimulator);

Update(GameTime gameTime):

physicsSimulator.Update((float)gameTime.ElapsedGameTime.TotalSeconds);

object1.Update();

object2.Update();

 

Object:

Object(Game game, PhysicsSimulator physicsSimulator, Vector2 position)

this.physicsSimulator = physicsSimulator;

this.game = game;

this.position = position;

Initialize()

objectBody = BodyFactory.Instance.CreateRectangleBody (physicsSimulator, width, height, mass);

objectBody.position = position;

objectGeom = GeometryFactory.Instance.CreateRectangleGeom(objectBody, width, height);

Update()

position = objectBody.position;

 

Also, I set one of the objects to static.  The other one falls properly with the gravity, but when it hits the other object, it just goes right through it.

 

Jun 21, 2009 at 10:30 AM

The simple samples are dead simple

Sample one is hello world
All it has is a box and nothing else

It sounds like you need to learn some general c# and XNA first

The GameScreen stuff you are talking about is used in lots of XNA samples around the web and there is a sample on it at the XNA website.

I would suggest going over to www.creators.xna.com and looking at some of the screen and other simple samples there first (if you have any trouble with that then there is a huge community that will help you on their forums)
Once you understand what a screen is you can then look over the simple samples with ease and see how everything works (some samples are really really small)

Also there is a manual for farseer here with almost all the things in it outlined http://www.physicspoweredgames.com/FarseerPhysics/Manual2.1.htm
There is some heavier documentation here http://www.physicspoweredgames.com/FarseerPhysics/API/index.html
There is a wiki with a few things in it that I can't find the link for right now :(
And there is also some very easy small tutorials here http://farseerphysics.codeplex.com/Wiki/View.aspx?title=Tutorials

I think that if you first go and look at a few XNA samples with screens etc... Then you will be able to understand the samples a whole lot better.

Coordinator
Jun 21, 2009 at 10:40 AM

The manual is sufficient in the way that it describes techniques and how Farseer Physics work. It is not a howto of how to create a game, it is simply a manual that gives you the knowledge of how the physics engine interact with your game, potential issues and the like.

As for the samples, they are as simple as they get while still having interaction with keyboard, mouse, graphics and a screen system. They are basically a foundation for simple games for you to build on. They show off the different techniques in Farseer Physics without blending all sorts of code into it - Look at demo[1-10].cs. What you want is an example on how to hook up the physics engine to a XNA game: It can be found on the forums like the one here. I've have it on my todo list that such a sample should be added to our release package, but I simply don't have the time to do everything on my todo list at once.

As for the going through each other problem. You need to add the geometry to the physics simulator too. Geometries provide collision detection and need to be added to the physics simulator to work.

Jun 21, 2009 at 9:50 PM
Edited Jun 21, 2009 at 10:17 PM

Ok-  I guess I misworded it.  XNA is intended to be a "hobbyist" API for game dev.  Farseer is marketed a great all around engine for physics.  Yes- the "simple sample" is rather easy... but when trying to find out the extreme basics, sometimes spelling things out in a cookbook fashion helps a lot with some people regardless of skill levels.

As far as the manual-  its not well organized or thorough.  I understand Farseer doesn't have a full staff, but I have looked around and found theres many people running in to getting the basics started with Farseer.  It took me almost a week to figure out how to get two objects (a falling object and a surface) and have them collide with each other.  I started from the basics and did a "1, 2, 3" of what the manual said, and it just would not collide at all.  I eventually figured out that although the engine allowed the object to "fall", it wouldn't collide until I manually did "physicsSimulator.Add(objBody)" and "physicsSimulator.Add(objGeom)" for both the object and the surface.  I did include "physicsSimulator" in the body/geomfactory arguments, too.  I literally extracted the exact basics from the simpleSamples and they wouldn't collide until I did that where NOWHERE did it have that.  I tried the XNA Video Tutorials, and they did help get a good understanding of the concepts very well, but it seems to be a bit dated in the syntax.

Regardless- what I have now is a completely basic rectangle (100x30) that falls with the gravity and lands on a large static rectangle, aka the surface (800x100).  The game window dimensions is an 800x600 area.  At approximately halfway through the width (just past 400), the falling object hits an edge on the surface and falls off.  Is there a maximum size for Geometries by default?  If so, what am I missing?

I'm slowly getting a hang of Farseer-  yes, its simple and cuts out a ton of collision work and newtonian physics I had to previously program on my own.  Just as I've said-  there just needs to be some better Getting Started / Basics manuals and such.

Edit: I figured out the body/geometry was offset from the sprite for some reason.  I had to set the positions on both objects initially to:

objBody.Position = new Vector2(position.X + texture.Width / 2, position.Y + texture.Height / 2);

This works fine now-  but again, is this as a result of something I'm missing, or is it a known issue?

Jun 21, 2009 at 11:32 PM
Edited Jun 21, 2009 at 11:35 PM

The body's position is in the cenetr of the object, not the upper left. I see your problem: you did not explictly call a geom factory overload that ook a physics simulator. If you look at

objectGeom = GeometryFactory.Instance.CreateRectangleGeom(objectBody, width, height);

i see no mention of a physics sim. If you fix this code then you should not have to add geoms.

Jun 22, 2009 at 5:29 AM

Clarifying what RogueCommanderIX meant in response to:

"I eventually figured out that although the engine allowed the object to "fall", it wouldn't collide until I manually did "physicsSimulator.Add(objBody)" and "physicsSimulator.Add(objGeom)" for both the object and the surface."

your geom code:

objectGeom = GeometryFactory.Instance.CreateRectangleGeom(objectBody, width, height);

should include the physics simulator.

objectGeom = GeometryFactory.Instance.CreateRectangleGeom(physicsSimulator, objectBody, width, height);

The one you used is for premaking geom's for later usage. This was in the Manual.docx if you read it ;)

 

objBody.Position is the center of the body, you should always draw your texture with an origin offset, like in the samples.

 

The physics engine is very easy to use. Make sure you pay attention to the code when you read the samples ;)

 

Jun 22, 2009 at 7:21 AM

I looked at it again, and yes it was my mistake about not using the proper overload.  I'll give you that-  but "objBody.Position is the center of the body, you should always draw your texture with an origin offset, like in the samples." doesn't work:

Draw:

....

_circleBrush.Draw(ScreenManager.SpriteBatch, _circleBody.Position);

_rectangleBrush.Draw(ScreenManager.SpriteBatch, _rectangleBody.Position, _rectangleBody.Rotation);

....

Am also I looking in the wrong place to find out how to do the offset properly?

Jun 22, 2009 at 12:26 PM
Edited Jun 22, 2009 at 12:46 PM

> I've been tearing my hair out for a week trying to figure out how to use Farseer with XNA.  I've known how to program for a while, so thats not the issue.  The issue is theres next to no documentation for Farseer.  The manual seems to be put together in a matter of minutes just to get it out of the way, where the "SimpleSample" is anything but "Simple".

i know how you feel. i had similiar issues with other open-source projects (especially Delta3D ... grrr, never use that >:/ ). to be honest i also had troubles getting the Farseer collisions and drawing (centroids) right. looking at these issues now they were due to knots in my brain and ignorance in believing what the manual said. BUT ... saying that Farseer has "next to no documentation" and "put together in a matter of minutes" is an insult of the developers and makes me angry! there are few projects which have such a good manual, demos and active developers. i think Farseer is very easy and intuitive to use but you have to have at least a little knowledge about physics engines which is unavoidable.

if you take a look at the simple samples you see all you need is create a PhysicsSimulator, a Body and a Geom and you're done (if you ignore the drawing part for a moment). drawing is not really part of a physics engine but hell, if you need it, they even implemented a debug view for XNA and SilverLight in the demo project. i can't think of anything easier than that.

as for your problem...

for your basic issues: take a look at http://farseerphysics.codeplex.com --> Engine Usage --> Others . there are two external simple sample demos. and also take a look at http://farseerphysics.codeplex.com --> API

for the drawing issues: take a look at the tutorial section: http://farseerphysics.codeplex.com --> Tutorials & FAQ --> Why is drawing or collision off center? (Centroids CreatePolygonBody CreatePolygonGeom).

(direct links omitted for pedagogical reasons)

Jun 27, 2009 at 11:16 AM

I'd like to first off apologize for how I approached the questions/difficulties I had... not the right tone, and not the right way to "come in to someone else's home" if I'm asking for help from someone's free code hes giving away.

Anyway-  after a little bit of work, I've for the most part figured out how to implement just about anything I need with Farseer-  theres hiccups here and there that needs some tweaking as all code does... but in general, it definitely is a great engine.  My only problem right now is objects falling into the floor, or other objects with nothing but gravity.  I've increase the vertices and lowered the grid size, but it doesn't help enough.  FS3.0 looks pretty promising with the CCD to fix this issue.

Coordinator
Jun 27, 2009 at 5:53 PM

No problem, I can understand the fustrations that new users have with Farseer Physics. It is a physics engine and by design not very user friendly. We try our best to make it as easy to use as possible and have even sacrificed a lot of code maintainability to do so. If you ever have any problems, just post them here and we will try to answer them to the best of our abilities.

FS3.0 does indeed look promising. I'm working on it as I write together with Matthew, trying to figure out some bugs we made (not on purpose I might add). CCD has yet to be looked at, but when it is done interpenetrations from high velocity geometries will be history. By the way, what kind of objects are falling into the floor?

Small/thin objects are the worst. Usually you have to take mesures to ensure that they collide correctly.

Jul 9, 2009 at 2:24 PM

I know how random12345 feels. The official farseer simple samples are a little more complex than you want when you first look at the engine. What you really want is a <100 line program that just shows you the basics.

Unforntunatly for the simple samples to be extensible enough to be able to add extra samples easily at a later date, it is slightly more complex than a hello world.

I find http://farseersimplesamples.codeplex.com/ fills this gap. I don't know what the affiliation is, but there is a link to it on the home page and it helped me as a first step before moving onto the official samples.

Jul 9, 2009 at 2:29 PM

maybe there needs to be a tutorial or something in the documentation with a simple "hello world". Because i have seen this issue come up a few times now in the discussions.

unless there is already and i don't know about it.

maybe not even a real running app. just a tutorial showing:

e.g.

-how to create a body with a geometry

-how to update the physics simulator

-what properties to use from the body to draw your texture etc.

and nothing more

Jul 9, 2009 at 3:04 PM

Why not try this basic tutorial that someone posted last month?

http://www.techiscool.com/wingwarp/silverstripe/physics-Lesson/

Jul 9, 2009 at 3:34 PM

i think the Demos are pretty easy already. the problem i think is that they use a lot of helper classes which are overloaded. of course you can just focus on the actual code but as a beginner you don't know what the actual code is and get scared be all the drawing methods. what i would do is make a simple XNA application which uses 3D polygon primitives for drawing as they need way fewer code lines than re-implementing all the 2D drawing which XNA is not capabable of in the first place. no input in the first samples. the Demos them self are actually fine i think: 1. creating a Body, 2. creating a Geom, 3. adding gravity, 4. adding a colliding Geom etc.