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

Need help getting started with Farseer

Topics: Developer Forum, Project Management Forum, User Forum
May 5, 2009 at 1:52 AM
Hello everyone. I have been programming C# for a while now, but i just started with Farseer. I need help getting started, please. If anyone knows any good tutorials it would be much appreciated. The problem i am having is that i created my Body using BodyFactory and my Geom using GeomFactory, and i want to be able to move the sprite associated with the body around the screen. I tried using "ApplyForce()", but could not figure out how to use it, i dont know if i was just misunderstanding it, or using it wrong. I tried using it the following two ways :

launchedBox.objectBody.ApplyForce(new Vector2(1.0f, 0.0f));
launchedBox.objectBody.Position += launchedBox.objectBody.ApplyForce(new Vector2(1.0f, 0.0f));

the first way did nothing (btw, i am trying to use it in a key catch, the right arrow in this case, and launchedBox is the name of my Object, and objectBody is the name of it's Body)
and the second way said i couldn't convert from a Void to a Vector2
Any help would be appreciated! 
Thank you to whoever helps me resolve this issue!

(Also, like i said, if anyone knows any good tutorials please tell me!)
May 5, 2009 at 2:28 AM
Well assuming your drawing the stuff correctly and updating farseer correctly then the only problem is how much force your applying,
you must apply alot more force
body.applyforce(new vector2(-200, 0))  this will move it left and gradually increase speed as you hold down the arrow.
body.applyimpulse(new vector2(-200, 0)) this will whip your object, because applying an impulse will imediatley set your body in motion instead of building up, so it takes less force.
Also your bodies mass is relevant, set it to 1 then play around with movement.
May 5, 2009 at 11:09 AM
your second line is definitely wrong. Farseer takes care of updating the position itself, there is no need to apply manually.

as for the tutorial: you should really have a look at the included demo samples. the show some intended usage and also provide debug drawing stuff.

do you see the sprite (not moving though)?
try setting the Body position manually! did it change?
(i just wanna check if the sprite is actually associated with the Body position here)

activate gravity! does the Body fall?
(in case the force you are applying is to week, this should do something)

do you call the update methods of the Farseer engine in your game loop?

May 5, 2009 at 11:23 AM
i tried moving it manually, it worked, and i also tried a larger force, about 200 or so. Where can i get the demo? i can't find one in my Farseer directory. I'll try activating gravity as soon as i find out how. Thanks for the speedy replies guys! It's nothin like this on the XNA forums!
May 5, 2009 at 1:25 PM
You can get the demos (samples) here:

They are called "Simple samples" and "Advanced Samples". Try out the simple samples first, and if you want to see a showcase of different performance techniques and features, you can try out the advanced samples.

To activate gravity, you give the PhysicsSimulator a vector2 in the constructor or set the PhysicsSimulator.Gravity property.
May 5, 2009 at 8:36 PM
thanks again! Hopefully i can resolve this issue, i'm trying to make a simple physics-related "game" (obviously it's physics-related or i wouldn't be using Farseer!).
May 6, 2009 at 8:22 AM
There should be a very simple getting started Guide in the documentation to get a person jump started,
I remember not having a clue what to do and searching the interwebs for weeks,
i wasnt aware you had to add references as this was my first engine,
i wasnt aware you had to create a physics simulator
i wasnt aware of how to draw etc. it was very tough for a easily confused person like myself haha.

I dug up an xnatutorial that covered farseer a bit (mostly theory) but it was enough for me to make a static farseer class and add references which unraveled alot.
Anyway my point is,
there should be a very simple guide that says.
Add the correct branch,
add references(perhaps stating whats neccesary for what),

Declare a texture2d, a Vector2, a body, a geom

Specify how the setup for each one to correspond to the size of the texture, then explain the Vector2 origin

Update the physics engine.

Draw the texture with spritebatch according the body.position.

Something that covers the very basics to get your object falling off screen.

Then perhaps explain real quick how you can make a static body so they can see interaction.

I dont know i think it would be a great deal of help, maybe have a seperate section from documentation for tutorials.
May 7, 2009 at 2:59 AM
Edited May 7, 2009 at 3:09 AM
Thanks, i'll look around for them, for the meantime i ended up just definingg my own ApplyForce Class that does what i need (it's crude, but it works, im sure Farseer's is ALOT better though, ha)

EDIT: in Yobiv's post i didn't isee it but towards the end he said "do you call the update methods of the Farseer engine in your game loop?" I had no Idea there were any Update methods! Anyone mind telling me how to use them?
May 7, 2009 at 4:10 AM
whats your physicssimulator name?
lets say Fphysics

May 7, 2009 at 9:28 PM
Edited May 7, 2009 at 9:41 PM
Tlegion - Thanks alot, that did the trick! I have smooth motion, finally! (and 1 definitely was WAY to little) i now have my up, down, left, and right vectors set up, and am moving my little dud around the screen! With a gravity of 98.0665, ha, 10 times the gravity on earth
May 8, 2009 at 4:45 AM

ok, now im having trouble with ApplyTorque() this is what i have - 



- same setup as before with the objectBody

Does this take degrees or radians, because i am not getting any rotation at all no matter how small or large the value, and yes, this time i am updating the physicsSimulator!

May 8, 2009 at 5:52 AM
Edited May 8, 2009 at 5:56 AM

What is your bodies mass, and did you change its moment of intertia?

That is a small force but like you said you tried others.

Check to make sure you are drawing according to the geom or bodies rotation.

Spritebatch.draw(texture, geom.Position, null, color.white, geom.Rotation, spriteOrigin, 1f, spriteeffects.none, 0);

Thats a basic example of how you should set up your draw call, check yours to see if everything is in place.




Just to note, I am not aware of whether or not the applytorque use degrees or radians,

I think it does not take either,

it is not the same as setting the rotation of your object,

its the amount of torque force you are applying to spin it,

similar to how you can applyForce of 200 but its not gonna move it 200 pixels,

you can apply a torque force or impulse and inside of the body class it controls the rotation based on the simulator.

In other words, its not in radians or degrees its just a int to represent force(i think)

May 8, 2009 at 7:03 AM

That is correct.

May 8, 2009 at 1:18 PM
Edited May 8, 2009 at 1:19 PM

In case you did know... the Rotation on the Body is in radians..

When i started using ApplyTorque my rotation wasn't working right because i was treating it as degrees.

Also try a really really big value of torque. If your masses are really big it may not affect it. eg start with 100 and just keep adding zeros (and retrying) at the end till it tells you the number is too big to fit into a float.

Also try setting your masses to 1

unless someone else has a better idea

May 9, 2009 at 6:23 PM

ok, thanks guys, the problem was in my drawing calls, because with the way i set up me class that handles objects in my games, i added a rotation variable to that also, and i was using that one. Alright, all my problems are done now, i even solved one on my own! I was having a problem with collisions, but once again it was a problem with my physicalObject class - every body had it's own physics simulator! ha, so i moved the pisics simulator somewhere else, and now collision works! Thanks again guys, everyone has been VERY helpful!